dst2 = new Converter(dynamic)
+ .read(Templates.tList(Templates.TString));
+ System.out.println(dst2.get(0));
+ System.out.println(dst2.get(1));
+ System.out.println(dst2.get(2));
+
+
+
diff -r 000000000000 -r cb825acd883a pom.xml
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/pom.xml Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,193 @@
+
+
+ 4.0.0
+ org.msgpack
+ msgpack
+ MessagePack for Java
+ MessagePack for Java is a binary-based efficient object
+ serialization library in Java.
+ 0.6.12-SNAPSHOT
+ bundle
+ http://msgpack.org/
+
+
+
+ The Apache Software License, Version 2.0
+ http://www.apache.org/licenses/LICENSE-2.0.txt
+ repo
+
+
+
+
+ scm:git:git://github.com/msgpack/msgpack-java.git
+ scm:git:git@github.com:msgpack/msgpack-java.git
+ scm:git:git://github.com/msgpack/msgpack-java.git
+
+
+
+ GitHub
+ https://github.com/msgpack/msgpack-java/issues
+
+
+
+
+ frsyuki
+ Sadayuki Furuhashi
+ frsyuki@users.sourceforge.jp
+
+
+ muga
+ Muga Nishizawa
+ muga.nishizawa@gmail.com
+
+
+
+
+
+ com.googlecode.json-simple
+ json-simple
+ 1.1.1
+
+
+ junit
+ junit
+
+
+
+
+ org.javassist
+ javassist
+ 3.18.1-GA
+ compile
+
+
+ junit
+ junit
+ 4.8.2
+ test
+
+
+
+
+ org.sonatype.oss
+ oss-parent
+ 7
+
+
+
+
+
+ src/main/resources
+
+
+
+
+ src/test/resources
+
+
+
+
+ maven-compiler-plugin
+ 2.3.2
+
+ 1.6
+ 1.6
+ UTF-8
+
+
+
+ org.apache.felix
+ maven-bundle-plugin
+ 2.3.6
+ true
+
+
+ maven-eclipse-plugin
+ 2.5.1
+
+
+ org.apache.maven.plugins
+ maven-source-plugin
+ 2.1.2
+
+
+ attach-sources
+
+ jar
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-scm-plugin
+ 1.6
+
+ false
+
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 2.8.1
+
+ ${project.name} ${project.version} API
+ true
+ en_US
+ UTF-8
+
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.8.1
+
+ -Xmx512M
+
+
+
+ org.codehaus.mojo
+ findbugs-maven-plugin
+
+ true
+ true
+ Max
+
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-jxr-plugin
+ 2.2
+
+
+ org.apache.maven.plugins
+ maven-surefire-report-plugin
+ 2.11
+
+
+
+
+
diff -r 000000000000 -r cb825acd883a src/main/java/org/apache/harmony/beans/Argument.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/apache/harmony/beans/Argument.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,62 @@
+// MODIFIED FOR THE MSGPACK PROJECT
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+//
+
+package org.apache.harmony.beans;
+
+public class Argument {
+
+ private Class> type;
+
+ private Object value;
+
+ private Class>[] interfaces;
+
+ public Argument(Object value) {
+ this.value = value;
+ if (this.value != null) {
+ this.type = value.getClass();
+ this.interfaces = this.type.getInterfaces();
+ }
+ }
+
+ public Argument(Class> type, Object value) {
+ this.type = type;
+ this.value = value;
+ this.interfaces = type.getInterfaces();
+ }
+
+ public Class> getType() {
+ return type;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+
+ public Class>[] getInterfaces() {
+ return interfaces;
+ }
+
+ public void setType(Class> type) {
+ this.type = type;
+ this.interfaces = type.getInterfaces();
+ }
+
+ public void setInterfaces(Class>[] interfaces) {
+ this.interfaces = interfaces;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/apache/harmony/beans/BeansUtils.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/apache/harmony/beans/BeansUtils.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,122 @@
+// MODIFIED FOR THE MSGPACK PROJECT
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+//
+
+package org.apache.harmony.beans;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+public class BeansUtils {
+
+ public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
+
+ public static final String NEW = "new"; //$NON-NLS-1$
+
+ public static final String NEWINSTANCE = "newInstance"; //$NON-NLS-1$
+
+ public static final String NEWARRAY = "newArray"; //$NON-NLS-1$
+
+ public static final String FORNAME = "forName"; //$NON-NLS-1$
+
+ public static final String GET = "get"; //$NON-NLS-1$
+
+ public static final String IS = "is"; //$NON-NLS-1$
+
+ public static final String SET = "set"; //$NON-NLS-1$
+
+ public static final String ADD = "add"; //$NON-NLS-1$
+
+ public static final String PUT = "put"; //$NON-NLS-1$
+
+ public static final String NULL = "null"; //$NON-NLS-1$
+
+ public static final String QUOTE = "\"\""; //$NON-NLS-1$
+
+ public static final int getHashCode(Object obj) {
+ return obj != null ? obj.hashCode() : 0;
+ }
+
+ public static final int getHashCode(boolean bool) {
+ return bool ? 1 : 0;
+ }
+
+ public static String toASCIILowerCase(String string) {
+ char[] charArray = string.toCharArray();
+ StringBuilder sb = new StringBuilder(charArray.length);
+ for (int index = 0; index < charArray.length; index++) {
+ if ('A' <= charArray[index] && charArray[index] <= 'Z') {
+ sb.append((char) (charArray[index] + ('a' - 'A')));
+ } else {
+ sb.append(charArray[index]);
+ }
+ }
+ return sb.toString();
+ }
+
+ public static String toASCIIUpperCase(String string) {
+ char[] charArray = string.toCharArray();
+ StringBuilder sb = new StringBuilder(charArray.length);
+ for (int index = 0; index < charArray.length; index++) {
+ if ('a' <= charArray[index] && charArray[index] <= 'z') {
+ sb.append((char) (charArray[index] - ('a' - 'A')));
+ } else {
+ sb.append(charArray[index]);
+ }
+ }
+ return sb.toString();
+ }
+
+ public static boolean isPrimitiveWrapper(Class> wrapper, Class> base) {
+ return (base == boolean.class) && (wrapper == Boolean.class)
+ || (base == byte.class) && (wrapper == Byte.class)
+ || (base == char.class) && (wrapper == Character.class)
+ || (base == short.class) && (wrapper == Short.class)
+ || (base == int.class) && (wrapper == Integer.class)
+ || (base == long.class) && (wrapper == Long.class)
+ || (base == float.class) && (wrapper == Float.class)
+ || (base == double.class) && (wrapper == Double.class);
+ }
+
+ private static final String EQUALS_METHOD = "equals";
+
+ private static final Class>[] EQUALS_PARAMETERS = new Class>[] { Object.class };
+
+ public static boolean declaredEquals(Class> clazz) {
+ for (Method declaredMethod : clazz.getDeclaredMethods()) {
+ if (EQUALS_METHOD.equals(declaredMethod.getName())
+ && Arrays.equals(declaredMethod.getParameterTypes(),
+ EQUALS_PARAMETERS)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public static String idOfClass(Class> clazz) {
+ Class> theClass = clazz;
+ StringBuilder sb = new StringBuilder();
+ if (theClass.isArray()) {
+ do {
+ sb.append("Array"); //$NON-NLS-1$
+ theClass = theClass.getComponentType();
+ } while (theClass.isArray());
+ }
+ String clazzName = theClass.getName();
+ clazzName = clazzName.substring(clazzName.lastIndexOf('.') + 1);
+ return clazzName + sb.toString();
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/apache/harmony/beans/internal/nls/Messages.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/apache/harmony/beans/internal/nls/Messages.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,240 @@
+// MODIFIED FOR THE MSGPACK PROJECT
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+//
+
+//
+// THE FILE HAS BEEN AUTOGENERATED BY MSGTOOL TOOL.
+// All changes made to this file manually will be overwritten
+// if this tool runs again. Better make changes in the template file.
+//
+
+package org.apache.harmony.beans.internal.nls;
+
+
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+//import org.apache.harmony.kernel.vm.VM;
+
+/**
+ * This class retrieves strings from a resource bundle and returns them,
+ * formatting them with MessageFormat when required.
+ *
+ * It is used by the system classes to provide national language support, by
+ * looking up messages in the
+ * org.apache.harmony.beans.internal.nls.messages
+ *
+ * resource bundle. Note that if this file is not available, or an invalid key
+ * is looked up, or resource bundle support is not available, the key itself
+ * will be returned as the associated message. This means that the KEY
+ * should a reasonable human-readable (english) string.
+ *
+ */
+public class Messages {
+
+ // ResourceBundle holding the system messages.
+ static private ResourceBundle bundle = null;
+
+ /**
+ * Retrieves a message which has no arguments.
+ *
+ * @param msg
+ * String the key to look up.
+ * @return String the message for that key in the system message bundle.
+ */
+ static public String getString(String msg) {
+ if (bundle == null)
+ return msg;
+ try {
+ return bundle.getString(msg);
+ } catch (MissingResourceException e) {
+ return "Missing message: " + msg; //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Retrieves a message which takes 1 argument.
+ *
+ * @param msg
+ * String the key to look up.
+ * @param arg
+ * Object the object to insert in the formatted output.
+ * @return String the message for that key in the system message bundle.
+ */
+ static public String getString(String msg, Object arg) {
+ return getString(msg, new Object[] { arg });
+ }
+
+ /**
+ * Retrieves a message which takes 1 integer argument.
+ *
+ * @param msg
+ * String the key to look up.
+ * @param arg
+ * int the integer to insert in the formatted output.
+ * @return String the message for that key in the system message bundle.
+ */
+ static public String getString(String msg, int arg) {
+ return getString(msg, new Object[] { Integer.toString(arg) });
+ }
+
+ /**
+ * Retrieves a message which takes 1 character argument.
+ *
+ * @param msg
+ * String the key to look up.
+ * @param arg
+ * char the character to insert in the formatted output.
+ * @return String the message for that key in the system message bundle.
+ */
+ static public String getString(String msg, char arg) {
+ return getString(msg, new Object[] { String.valueOf(arg) });
+ }
+
+ /**
+ * Retrieves a message which takes 2 arguments.
+ *
+ * @param msg
+ * String the key to look up.
+ * @param arg1
+ * Object an object to insert in the formatted output.
+ * @param arg2
+ * Object another object to insert in the formatted output.
+ * @return String the message for that key in the system message bundle.
+ */
+ static public String getString(String msg, Object arg1, Object arg2) {
+ return getString(msg, new Object[] { arg1, arg2 });
+ }
+
+ /**
+ * Retrieves a message which takes several arguments.
+ *
+ * @param msg
+ * String the key to look up.
+ * @param args
+ * Object[] the objects to insert in the formatted output.
+ * @return String the message for that key in the system message bundle.
+ */
+ static public String getString(String msg, Object[] args) {
+ String format = msg;
+
+ if (bundle != null) {
+ try {
+ format = bundle.getString(msg);
+ } catch (MissingResourceException e) {
+ }
+ }
+
+ return format(format, args);
+ }
+
+ /**
+ * Generates a formatted text string given a source string containing
+ * "argument markers" of the form "{argNum}" where each argNum must be in
+ * the range 0..9. The result is generated by inserting the toString of each
+ * argument into the position indicated in the string.
+ *
+ * To insert the "{" character into the output, use a single backslash
+ * character to escape it (i.e. "\{"). The "}" character does not need to be
+ * escaped.
+ *
+ * @param format
+ * String the format to use when printing.
+ * @param args
+ * Object[] the arguments to use.
+ * @return String the formatted message.
+ */
+ public static String format(String format, Object[] args) {
+ StringBuilder answer = new StringBuilder(format.length()
+ + (args.length * 20));
+ String[] argStrings = new String[args.length];
+ for (int i = 0; i < args.length; ++i) {
+ if (args[i] == null)
+ argStrings[i] = ""; //$NON-NLS-1$
+ else
+ argStrings[i] = args[i].toString();
+ }
+ int lastI = 0;
+ for (int i = format.indexOf('{', 0); i >= 0; i = format.indexOf('{',
+ lastI)) {
+ if (i != 0 && format.charAt(i - 1) == '\\') {
+ // It's escaped, just print and loop.
+ if (i != 1)
+ answer.append(format.substring(lastI, i - 1));
+ answer.append('{');
+ lastI = i + 1;
+ } else {
+ // It's a format character.
+ if (i > format.length() - 3) {
+ // Bad format, just print and loop.
+ answer.append(format.substring(lastI, format.length()));
+ lastI = format.length();
+ } else {
+ int argnum = (byte) Character.digit(format.charAt(i + 1),
+ 10);
+ if (argnum < 0 || format.charAt(i + 2) != '}') {
+ // Bad format, just print and loop.
+ answer.append(format.substring(lastI, i + 1));
+ lastI = i + 1;
+ } else {
+ // Got a good one!
+ answer.append(format.substring(lastI, i));
+ if (argnum >= argStrings.length)
+ answer.append(""); //$NON-NLS-1$
+ else
+ answer.append(argStrings[argnum]);
+ lastI = i + 3;
+ }
+ }
+ }
+ }
+ if (lastI < format.length())
+ answer.append(format.substring(lastI, format.length()));
+ return answer.toString();
+ }
+//
+// /**
+// * Changes the locale of the messages.
+// *
+// * @param locale
+// * Locale the locale to change to.
+// */
+// static public ResourceBundle setLocale(final Locale locale,
+// final String resource) {
+// try {
+// final ClassLoader loader = VM.bootCallerClassLoader();
+// return (ResourceBundle) AccessController
+// .doPrivileged(new PrivilegedAction() {
+// public Object run() {
+// return ResourceBundle.getBundle(resource, locale,
+// loader != null ? loader : ClassLoader.getSystemClassLoader());
+// }
+// });
+// } catch (MissingResourceException e) {
+// }
+// return null;
+// }
+//
+// static {
+// // Attempt to load the messages.
+// try {
+// bundle = setLocale(Locale.getDefault(),
+// "org.apache.harmony.beans.internal.nls.messages"); //$NON-NLS-1$
+// } catch (Throwable e) {
+// e.printStackTrace();
+// }
+// }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/apache/harmony/beans/internal/nls/messages.properties
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/apache/harmony/beans/internal/nls/messages.properties Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,134 @@
+# MODIFIED FOR THE MSGPACK PROJECT
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# messages for EN locale
+beans.00=no getter for {0} property
+beans.01=no property for name {0} is found
+beans.02=in DefaultPersistenceDelegate.mutatesTo() {0} : {1}
+beans.03=Target Bean class is null
+beans.04=bad property name
+beans.05=Modifier for setter method should be public.
+beans.06=Number of parameters in setter method is not equal to 1.
+beans.07=Parameter type in setter method does not corresponds to predefined.
+beans.08=Number of parameters in getter method is not equal to 0.
+beans.09=Parameter type in getter method does not corresponds to predefined.
+beans.0A=Modifier for getter method should be public.
+beans.0B=Exception in command execution
+beans.0C=source is null
+beans.0D=Error in expression: {0}
+beans.0E=Changes are null
+beans.0F=The new BeanContext can not be set
+beans.10=no node is found for statement with target = {0}
+beans.11=no getter for property {0} found
+beans.12=cannot access property {0} getter
+beans.13=no setter for property {0} found
+beans.14=Exception while finding property descriptor
+beans.15=The listener is null
+beans.16=The provider is null
+beans.17=The child is null
+beans.18=The requestor is null
+beans.19=The service class is null
+beans.1A=The service selector is null
+beans.1B=The service is null
+beans.1C=The event is null
+beans.1D=bean is null
+beans.1E=Illegal class name: {0}
+beans.1F=Method not found: get{0}
+beans.20=Method not found: set{0}
+beans.21=Modifier for indexed getter method should be public.
+beans.22=Number of parameters in getter method is not equal to 1.
+beans.23=Parameter in indexed getter method is not of integer type.
+beans.24=Parameter type in indexed getter method does not correspond to predefined.
+beans.25=Modifier for indexed setter method should be public.
+beans.26=Number of parameters in indexed setter method is not equal to 2.
+beans.27=First parameter type in indexed setter method should be int.
+beans.28=Second parameter type in indexed setter method does not corresponds to predefined.
+beans.29=Membership listener is null
+beans.2A=Target child can not be null
+beans.2B=Resource name can not be null
+beans.2C=The child can not be null
+beans.2D=Invalid resource
+beans.2E=PropertyVetoException was thrown while removing a child: {0}; Original error message:{1}
+beans.2F=Target child is null
+beans.30=PropertyVetoException was thrown while adding a child: {0}; Original error message:{1}
+beans.31=No valid method {0} for {1} found.
+beans.32=Cannot acquire event type from {0} listener.
+beans.33={0} does not return
+beans.34={0} should have a single input parameter
+beans.35=Single parameter does not match to {0} class
+beans.36=No input params are allowed for getListenerMethod
+beans.37=Return type of getListenerMethod is not an array of listeners
+beans.38=Add and remove methods are not available
+beans.39=Cannot generate event set descriptor for name {0}.
+beans.3A=Event type with name {0} is not found.
+beans.3B=skipping expression {0}...
+beans.3C=Unknown method name for array
+beans.3D=First parameter in array getter(setter) is not of Integer type
+beans.3E=Illegal number of arguments in array getter
+beans.3F=Illegal number of arguments in array setter
+beans.40=No constructor for class {0} found
+beans.41=No method with name {0} is found
+beans.42=target is not generated: classname {0} is not found
+beans.43=Cannot convert {0} to char
+beans.44=for property {0} no getter(setter) is found
+beans.45=method name is not generated: error in getMethodName()
+beans.46=Not a valid child
+beans.47=Unable to instantiate property editor
+beans.48=Property editor is not assignable from the PropertyEditor interface
+beans.49=Child cannot implement both BeanContextChild and BeanContextProxy
+beans.4A=newInstance is null
+beans.4B=type is null
+beans.4C=encoder is null
+beans.4D=Invalid method call
+beans.4E=stopClass is not ancestor of beanClass
+beans.4F=search path is null
+beans.50=not an indexed property
+beans.51=Listener method {0} should have parameter of type {1}
+beans.52=listenerMethodName(s) is null
+beans.53=eventSetName is null
+beans.54=listenerType is null
+beans.55=Method is null
+beans.56=Provider does not match
+beans.57=Property type is incompatible with the indexed property type
+beans.58=No such indexed read method
+beans.59=Security violation accessing indexed read method
+beans.5A=Indexed read method is not compatible with indexed write method
+beans.5B=Indexed read method must take a single int argument
+beans.5C=Security violation accessing indexed write method
+beans.5D=No such indexed write method
+beans.5E=Indexed method is not compatible with non indexed method
+beans.5F=Indexed write method must take a two arguments
+beans.60=Indexed write method must take an int as its first argument
+beans.61=Indexed write method is not compatible with indexed read method
+beans.62=Cannot decide which method to call to match {0}
+beans.63=The type of element is mismatch with the type of array
+beans.64=Method not found: {0}
+beans.65=not a child of this context
+beans.66=not the service provider registered with this context
+beans.67=null child
+beans.68=cannot update children during serialization
+beans.69=Validation failed to add the child
+beans.6A=null BeanContextChild proxy
+beans.6B=failed to update child's beanContext property
+beans.6C=Illegal to impl both BeanContextChild and BeanContextProxy
+beans.6D=Not a child of this context
+beans.6E=Validation failed to remove the child
+beans.6F=children changed during serialization!
+beans.70=no more objects to read
+beans.71=Unknown tag of basic type: {0}
+beans.72=Unknown root element: {0}
+beans.73=Unknown basic object: {0}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/MessagePack.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/MessagePack.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,837 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+import java.lang.reflect.Type;
+import java.nio.ByteBuffer;
+import org.msgpack.template.Template;
+import org.msgpack.template.TemplateRegistry;
+import org.msgpack.packer.Packer;
+import org.msgpack.packer.BufferPacker;
+import org.msgpack.packer.MessagePackPacker;
+import org.msgpack.packer.MessagePackBufferPacker;
+import org.msgpack.packer.Unconverter;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.unpacker.BufferUnpacker;
+import org.msgpack.unpacker.MessagePackUnpacker;
+import org.msgpack.unpacker.MessagePackBufferUnpacker;
+import org.msgpack.unpacker.Converter;
+import org.msgpack.type.Value;
+
+/**
+ *
+ * This is basic class to use MessagePack for Java. It creates serializers and
+ * deserializers for objects of classes.
+ *
+ *
+ *
+ * See Quick
+ * Start for Java on MessagePack wiki.
+ *
+ *
+ */
+public class MessagePack {
+ private TemplateRegistry registry;
+
+ /**
+ *
+ * @since 0.6.0
+ */
+ public MessagePack() {
+ registry = new TemplateRegistry(null);
+ }
+
+ /**
+ *
+ * @since 0.6.0
+ * @param msgpack
+ */
+ public MessagePack(MessagePack msgpack) {
+ registry = new TemplateRegistry(msgpack.registry);
+ }
+
+ protected MessagePack(TemplateRegistry registry) {
+ this.registry = registry;
+ }
+
+ /**
+ *
+ * @since 0.6.0
+ * @param cl
+ */
+ public void setClassLoader(final ClassLoader cl) {
+ registry.setClassLoader(cl);
+ }
+
+ /**
+ * Returns serializer that enables serializing objects into
+ * {@link java.io.OutputStream} object.
+ *
+ * @since 0.6.0
+ * @param out
+ * output stream
+ * @return stream-based serializer
+ */
+ public Packer createPacker(OutputStream out) {
+ return new MessagePackPacker(this, out);
+ }
+
+ /**
+ * Returns serializer that enables serializing objects into buffer.
+ *
+ * @since 0.6.0
+ * @return buffer-based serializer
+ */
+ public BufferPacker createBufferPacker() {
+ return new MessagePackBufferPacker(this);
+ }
+
+ /**
+ * Returns serializer that enables serializing objects into buffer.
+ *
+ * @since 0.6.0
+ * @param bufferSize
+ * initial size of buffer
+ * @return buffer-based serializer
+ */
+ public BufferPacker createBufferPacker(int bufferSize) {
+ return new MessagePackBufferPacker(this, bufferSize);
+ }
+
+ /**
+ * Returns deserializer that enables deserializing
+ * {@link java.io.InputStream} object.
+ *
+ * @since 0.6.0
+ * @param in
+ * input stream
+ * @return stream-based deserializer
+ */
+ public Unpacker createUnpacker(InputStream in) {
+ return new MessagePackUnpacker(this, in);
+ }
+
+ /**
+ * Returns empty deserializer that enables deserializing buffer.
+ *
+ * @since 0.6.0
+ * @return buffer-based deserializer
+ */
+ public BufferUnpacker createBufferUnpacker() {
+ return new MessagePackBufferUnpacker(this);
+ }
+
+ /**
+ * Returns deserializer that enables deserializing buffer.
+ *
+ * @since 0.6.0
+ * @param bytes
+ * input byte array
+ * @return buffer-based deserializer
+ */
+ public BufferUnpacker createBufferUnpacker(byte[] bytes) {
+ return createBufferUnpacker().wrap(bytes);
+ }
+
+ /**
+ * Returns deserializer that enables deserializing buffer.
+ *
+ * @since 0.6.0
+ * @param bytes
+ * @param off
+ * @param len
+ * @return buffer-based deserializer
+ */
+ public BufferUnpacker createBufferUnpacker(byte[] bytes, int off, int len) {
+ return createBufferUnpacker().wrap(bytes, off, len);
+ }
+
+ /**
+ * Returns deserializer that enables deserializing buffer.
+ *
+ * @since 0.6.0
+ * @param buffer
+ * input {@link java.nio.ByteBuffer} object
+ * @return buffer-based deserializer
+ */
+ public BufferUnpacker createBufferUnpacker(ByteBuffer buffer) {
+ return createBufferUnpacker().wrap(buffer);
+ }
+
+ /**
+ * Serializes specified object.
+ *
+ * @since 0.6.0
+ * @param v
+ * serialized object
+ * @return output byte array
+ * @throws IOException
+ */
+ public byte[] write(T v) throws IOException {
+ BufferPacker pk = createBufferPacker();
+ if (v == null) {
+ pk.writeNil();
+ } else {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(v.getClass());
+ tmpl.write(pk, v);
+ }
+ return pk.toByteArray();
+ }
+
+ /**
+ * Serializes specified object. It allows serializing object by specified
+ * template.
+ *
+ * @since 0.6.0
+ * @param v
+ * @param template
+ * @return
+ * @throws IOException
+ */
+ public byte[] write(T v, Template template) throws IOException {
+ BufferPacker pk = createBufferPacker();
+ template.write(pk, v);
+ return pk.toByteArray();
+ }
+
+ /**
+ * Serializes specified object to output stream.
+ *
+ * @since 0.6.0
+ * @param out
+ * output stream
+ * @param v
+ * serialized object
+ * @throws IOException
+ */
+ public void write(OutputStream out, T v) throws IOException {
+ Packer pk = createPacker(out);
+ if (v == null) {
+ pk.writeNil();
+ } else {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(v.getClass());
+ tmpl.write(pk, v);
+ }
+ }
+
+ /**
+ * Serializes object to output stream by specified template.
+ *
+ * @since 0.6.0
+ * @param out
+ * output stream
+ * @param v
+ * serialized object
+ * @param template
+ * serializer/deserializer for the object
+ * @throws IOException
+ */
+ public void write(OutputStream out, T v, Template template)
+ throws IOException {
+ Packer pk = createPacker(out);
+ template.write(pk, v);
+ }
+
+ /**
+ * Serializes {@link org.msgpack.type.Value} object to byte array.
+ *
+ * @since 0.6.0
+ * @param v
+ * serialized {@link org.msgpack.type.Value} object
+ * @return output byte array
+ * @throws IOException
+ */
+ public byte[] write(Value v) throws IOException {
+ // FIXME ValueTemplate should do this
+ BufferPacker pk = createBufferPacker();
+ pk.write(v);
+ return pk.toByteArray();
+ }
+
+ /**
+ * Deserializes specified byte array to {@link org.msgpack.type.Value}
+ * object.
+ *
+ * @since 0.6.0
+ * @param bytes
+ * input byte array
+ * @return
+ * @throws IOException
+ */
+ public Value read(byte[] bytes) throws IOException {
+ return read(bytes, 0, bytes.length);
+ }
+
+ /**
+ * Deserializes byte array to {@link org.msgpack.type.Value} object.
+ *
+ * @since 0.6.0
+ * @param bytes
+ * @param off
+ * @param len
+ * @return
+ * @throws IOException
+ */
+ public Value read(byte[] bytes, int off, int len) throws IOException {
+ return createBufferUnpacker(bytes, off, len).readValue();
+ }
+
+ /**
+ * Deserializes {@link java.nio.ByteBuffer} object to
+ * {@link org.msgpack.type.Value} object.
+ *
+ * @since 0.6.0
+ * @param buffer
+ * input buffer
+ * @return
+ * @throws IOException
+ */
+ public Value read(ByteBuffer buffer) throws IOException {
+ return createBufferUnpacker(buffer).readValue();
+ }
+
+ /**
+ * Deserializes input stream to {@link org.msgpack.type.Value} object.
+ *
+ * @since 0.6.0
+ * @param in
+ * input stream
+ * @return deserialized {@link org.msgpack.type.Value} object
+ * @throws IOException
+ */
+ public Value read(InputStream in) throws IOException {
+ return createUnpacker(in).readValue();
+ }
+
+ /**
+ * Deserializes byte array to object.
+ *
+ * @since 0.6.0
+ * @param bytes
+ * input byte array
+ * @param v
+ * @return
+ * @throws IOException
+ */
+ public T read(byte[] bytes, T v) throws IOException {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(v.getClass());
+ return read(bytes, v, tmpl);
+ }
+
+ /**
+ * Deserializes byte array to object according to template.
+ *
+ * @since 0.6.0
+ * @param bytes
+ * input byte array
+ * @param tmpl
+ * template
+ * @return
+ * @throws IOException
+ */
+ public T read(byte[] bytes, Template tmpl) throws IOException {
+ return read(bytes, null, tmpl);
+ }
+
+ /**
+ * Deserializes byte array to object of specified class.
+ *
+ * @since 0.6.0
+ * @param bytes
+ * input byte array
+ * @param c
+ * @return
+ * @throws IOException
+ */
+ public T read(byte[] bytes, Class c) throws IOException {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(c);
+ return read(bytes, null, tmpl);
+ }
+
+ /**
+ * Deserializes byte array to object according to specified template.
+ *
+ * @since 0.6.0
+ * @param bytes
+ * input byte array
+ * @param v
+ * @param tmpl
+ * template
+ * @return
+ * @throws IOException
+ */
+ public T read(byte[] bytes, T v, Template tmpl) throws IOException {
+ BufferUnpacker u = createBufferUnpacker(bytes);
+ return (T) tmpl.read(u, v);
+ }
+
+ /**
+ * Deserializes byte array to object.
+ *
+ * @since 0.6.8
+ * @param bytes
+ * input byte array
+ * @param v
+ * @return
+ * @throws IOException
+ */
+ public T read(byte[] bytes, int off, int len, Class c) throws IOException {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(c);
+ BufferUnpacker u = createBufferUnpacker(bytes, off, len);
+ return (T) tmpl.read(u, null);
+ }
+
+ /**
+ * Deserializes buffer to object.
+ *
+ * @since 0.6.0
+ * @param b
+ * input {@link java.nio.ByteBuffer} object
+ * @param v
+ * @return
+ * @throws IOException
+ */
+ public T read(ByteBuffer b, T v) throws IOException {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(v.getClass());
+ return read(b, v, tmpl);
+ }
+
+ /**
+ * Deserializes buffer to object according to template.
+ *
+ * @since 0.6.0
+ * @param b
+ * input buffer object
+ * @param tmpl
+ * @return
+ * @throws IOException
+ */
+ public T read(ByteBuffer b, Template tmpl) throws IOException {
+ return read(b, null, tmpl);
+ }
+
+ /**
+ * Deserializes buffer to object of specified class.
+ *
+ * @since 0.6.0
+ * @param b
+ * @param c
+ * @return
+ * @throws IOException
+ */
+ public T read(ByteBuffer b, Class c) throws IOException {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(c);
+ return read(b, null, tmpl);
+ }
+
+ /**
+ * Deserializes buffer to object according to template.
+ *
+ * @since 0.6.0
+ * @param b
+ * input buffer object
+ * @param v
+ * @param tmpl
+ * @return
+ * @throws IOException
+ */
+ public T read(ByteBuffer b, T v, Template tmpl) throws IOException {
+ BufferUnpacker u = createBufferUnpacker(b);
+ return tmpl.read(u, v);
+ }
+
+ /**
+ * Deserializes input stream to object.
+ *
+ * @since 0.6.0
+ * @param in
+ * input stream
+ * @param v
+ * @return
+ * @throws IOException
+ */
+ public T read(InputStream in, T v) throws IOException {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(v.getClass());
+ return read(in, v, tmpl);
+ }
+
+ /**
+ * Deserializes input stream to object according to template.
+ *
+ * @since 0.6.0
+ * @param in
+ * input stream
+ * @param tmpl
+ * @return
+ * @throws IOException
+ */
+ public T read(InputStream in, Template tmpl) throws IOException {
+ return read(in, null, tmpl);
+ }
+
+ /**
+ * Deserializes input stream to object of specified class.
+ *
+ * @since 0.6.0
+ * @param in
+ * @param c
+ * @return
+ * @throws IOException
+ */
+ public T read(InputStream in, Class c) throws IOException {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(c);
+ return read(in, null, tmpl);
+ }
+
+ /**
+ * Deserializes input stream to object according to template
+ *
+ * @since 0.6.0
+ * @param in
+ * input stream
+ * @param v
+ * @param tmpl
+ * @return
+ * @throws IOException
+ */
+ public T read(InputStream in, T v, Template tmpl) throws IOException {
+ Unpacker u = createUnpacker(in);
+ return tmpl.read(u, v);
+ }
+
+ /**
+ * Converts specified {@link org.msgpack.type.Value} object to object.
+ *
+ * @since 0.6.0
+ * @param v
+ * @param to
+ * @return
+ * @throws IOException
+ */
+ public T convert(Value v, T to) throws IOException {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(to.getClass());
+ return tmpl.read(new Converter(this, v), to);
+ }
+
+ /**
+ * Converts {@link org.msgpack.type.Value} object to object specified class.
+ *
+ * @since 0.6.0
+ * @param v
+ * @param c
+ * @return
+ * @throws IOException
+ */
+ public T convert(Value v, Class c) throws IOException {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(c);
+ return tmpl.read(new Converter(this, v), null);
+ }
+
+ /**
+ * Converts {@link org.msgpack.type.Value} object to object according to template
+ *
+ * @since 0.6.8
+ * @param v
+ * @param tmpl
+ * @return
+ * @throws IOException
+ */
+ public T convert(Value v, Template tmpl) throws IOException {
+ return tmpl.read(new Converter(this, v), null);
+ }
+
+ /**
+ * Unconverts specified object to {@link org.msgpack.type.Value} object.
+ *
+ * @since 0.6.0
+ * @param v
+ * @return
+ * @throws IOException
+ */
+ public Value unconvert(T v) throws IOException {
+ Unconverter pk = new Unconverter(this);
+ if (v == null) {
+ pk.writeNil();
+ } else {
+ @SuppressWarnings("unchecked")
+ Template tmpl = registry.lookup(v.getClass());
+ tmpl.write(pk, v);
+ }
+ return pk.getResult();
+ }
+
+ /**
+ * Registers {@link org.msgpack.template.Template} object for objects of
+ * specified class. Template object is a pair of serializer and
+ * deserializer for object serialization. It is generated automatically.
+ *
+ * @since 0.6.0
+ * @param type
+ */
+ public void register(Class> type) {
+ registry.register(type);
+ }
+
+ /**
+ * Registers specified {@link org.msgpack.template.Template} object
+ * associated by class.
+ *
+ * @see #register(Class)
+ * @since 0.6.0
+ * @param type
+ * @param template
+ */
+ public void register(Class type, Template template) {
+ registry.register(type, template);
+ }
+
+ /**
+ * Unregisters {@link org.msgpack.template.Template} object for objects of
+ * specified class.
+ *
+ * @since 0.6.0
+ * @param type
+ * @return
+ */
+ public boolean unregister(Class> type) {
+ return registry.unregister(type);
+ }
+
+ /**
+ * Unregisters all {@link org.msgpack.template.Template} objects that have
+ * been registered in advance.
+ *
+ * @since 0.6.0
+ */
+ public void unregister() {
+ registry.unregister();
+ }
+
+ /**
+ * Looks up a {@link org.msgpack.template.Template} object, which is
+ * serializer/deserializer associated by specified class.
+ *
+ * @since 0.6.0
+ * @param type
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ public Template lookup(Class type) {
+ return registry.lookup(type);
+ }
+
+ public Template> lookup(Type type) {
+ return registry.lookup(type);
+ }
+
+ private static final MessagePack globalMessagePack = new MessagePack();
+
+ /**
+ * Serializes specified object and returns the byte array.
+ *
+ * @deprecated {@link MessagePack#write(Object)}
+ * @param v
+ * @return
+ * @throws IOException
+ */
+ @Deprecated
+ public static byte[] pack(Object v) throws IOException {
+ return globalMessagePack.write(v);
+ }
+
+ /**
+ * Serializes specified object to output stream.
+ *
+ * @deprecated {@link MessagePack#write(OutputStream, Object)}
+ * @param out
+ * @param v
+ * @throws IOException
+ */
+ @Deprecated
+ public static void pack(OutputStream out, Object v) throws IOException {
+ globalMessagePack.write(out, v);
+ }
+
+ /**
+ * Serializes object by specified template and return the byte array.
+ *
+ * @deprecated {@link MessagePack#write(Object, Template)}
+ * @param v
+ * @param template
+ * @return
+ * @throws IOException
+ */
+ @Deprecated
+ public static byte[] pack(T v, Template template) throws IOException {
+ return globalMessagePack.write(v, template);
+ }
+
+ /**
+ * Serializes object to output stream. The object is serialized by specified
+ * template.
+ *
+ * @deprecated {@link MessagePack#write(OutputStream, Object, Template)}
+ * @param out
+ * @param v
+ * @param template
+ * @throws IOException
+ */
+ @Deprecated
+ public static void pack(OutputStream out, T v, Template template)
+ throws IOException {
+ globalMessagePack.write(out, v, template);
+ }
+
+ /**
+ * Converts byte array to {@link org.msgpack.type.Value} object.
+ *
+ * @deprecated {@link MessagePack#read(byte[])}
+ * @param bytes
+ * @return
+ * @throws IOException
+ */
+ @Deprecated
+ public static Value unpack(byte[] bytes) throws IOException {
+ return globalMessagePack.read(bytes);
+ }
+
+ @Deprecated
+ public static T unpack(byte[] bytes, Template template) throws IOException {
+ BufferUnpacker u = new MessagePackBufferUnpacker(globalMessagePack).wrap(bytes);
+ return template.read(u, null);
+ }
+
+ @Deprecated
+ public static T unpack(byte[] bytes, Template template, T to) throws IOException {
+ BufferUnpacker u = new MessagePackBufferUnpacker(globalMessagePack).wrap(bytes);
+ return template.read(u, to);
+ }
+
+ /**
+ * Deserializes byte array to object of specified class.
+ *
+ * @deprecated {@link MessagePack#read(byte[], Class)}
+ * @param bytes
+ * @param klass
+ * @return
+ * @throws IOException
+ */
+ @Deprecated
+ public static T unpack(byte[] bytes, Class klass) throws IOException {
+ return globalMessagePack.read(bytes, klass);
+ }
+
+ /**
+ * Deserializes byte array to object.
+ *
+ * @param bytes
+ * @param to
+ * @return
+ * @throws IOException
+ */
+ @Deprecated
+ public static T unpack(byte[] bytes, T to) throws IOException {
+ return globalMessagePack.read(bytes, to);
+ }
+
+ /**
+ * Converts input stream to {@link org.msgpack.type.Value} object.
+ *
+ * @deprecated {@link MessagePack#read(InputStream)}
+ * @param in
+ * @return
+ * @throws IOException
+ */
+ @Deprecated
+ public static Value unpack(InputStream in) throws IOException {
+ return globalMessagePack.read(in);
+ }
+
+ /**
+ * @deprecated
+ * @param in
+ * @param tmpl
+ * @return
+ * @throws IOException
+ * @throws MessageTypeException
+ */
+ @Deprecated
+ public static T unpack(InputStream in, Template tmpl)
+ throws IOException, MessageTypeException {
+ return tmpl.read(new MessagePackUnpacker(globalMessagePack, in), null);
+ }
+
+ /**
+ * @deprecated
+ * @param in
+ * @param tmpl
+ * @param to
+ * @return
+ * @throws IOException
+ * @throws MessageTypeException
+ */
+ @Deprecated
+ public static T unpack(InputStream in, Template tmpl, T to)
+ throws IOException, MessageTypeException {
+ return (T) tmpl.read(new MessagePackUnpacker(globalMessagePack, in), to);
+ }
+
+ /**
+ * Deserializes input stream to object of specified class.
+ *
+ * @deprecated {@link MessagePack#read(InputStream, Class)}
+ * @param in
+ * @param klass
+ * @return
+ * @throws IOException
+ */
+ @Deprecated
+ public static T unpack(InputStream in, Class klass)
+ throws IOException {
+ return globalMessagePack.read(in, klass);
+ }
+
+ /**
+ * Deserializes input stream to object.
+ *
+ * @deprecated {@link MessagePack#read(InputStream, Object)}
+ * @param in
+ * @param to
+ * @return
+ * @throws IOException
+ */
+ @Deprecated
+ public static T unpack(InputStream in, T to) throws IOException {
+ return globalMessagePack.read(in, to);
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/MessagePackable.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/MessagePackable.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,28 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+
+public interface MessagePackable {
+ public void writeTo(Packer pk) throws IOException;
+
+ public void readFrom(Unpacker u) throws IOException;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/MessageTypeException.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/MessageTypeException.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,37 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack;
+
+@SuppressWarnings("serial")
+public class MessageTypeException extends RuntimeException {
+ public MessageTypeException() {
+ super();
+ }
+
+ public MessageTypeException(String message) {
+ super(message);
+ }
+
+ public MessageTypeException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public MessageTypeException(Throwable cause) {
+ super(cause);
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/Beans.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/Beans.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,31 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.msgpack.template.FieldOption;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Beans {
+ FieldOption value() default FieldOption.DEFAULT;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/Delegate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/Delegate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,29 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Delegate {
+ String value();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/Ignore.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/Ignore.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,28 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Ignore {
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/Index.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/Index.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,29 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Index {
+ int value();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/Message.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/Message.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,31 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.msgpack.template.FieldOption;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Message {
+ FieldOption value() default FieldOption.DEFAULT;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/MessagePackBeans.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/MessagePackBeans.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,37 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.msgpack.template.FieldOption;
+
+/**
+ * Annotation for java beans class
+ *
+ * @author takeshita
+ *
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface MessagePackBeans {
+ FieldOption value() default FieldOption.DEFAULT;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/MessagePackDelegate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/MessagePackDelegate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,29 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface MessagePackDelegate {
+ String value();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/MessagePackMessage.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/MessagePackMessage.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,31 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.msgpack.template.FieldOption;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface MessagePackMessage {
+ FieldOption value() default FieldOption.DEFAULT;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/MessagePackOrdinalEnum.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/MessagePackOrdinalEnum.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,28 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface MessagePackOrdinalEnum {
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/NotNullable.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/NotNullable.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,29 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER,
+ ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface NotNullable {
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/Optional.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/Optional.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,28 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Optional {
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/annotation/OrdinalEnum.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/annotation/OrdinalEnum.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,45 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.msgpack.MessageTypeException;
+import org.msgpack.template.OrdinalEnumTemplate;
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface OrdinalEnum {
+
+ /**
+ * Specify whether the ordinal index lookup should be handled strictly or
+ * not when mapping ordinal value to an enum value. By specifying true,
+ * {@link MessageTypeException} will be thrown if the enum specified by the
+ * ordinal value does not exist in this implementation. If false, then the
+ * missing ordinal value treated as null, gracefully handling the lookup.
+ * Default is true.
+ *
+ * @since 0.6.8
+ * @see OrdinalEnumTemplate
+ */
+ boolean strict() default true;
+
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/AbstractInput.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/AbstractInput.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,39 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2011 Muga Nishizawa
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+abstract class AbstractInput implements Input {
+
+ private int readByteCount = 0;
+
+ public int getReadByteCount() {
+ return readByteCount;
+ }
+
+ public void resetReadByteCount() {
+ readByteCount = 0;
+ }
+
+ protected final void incrReadByteCount(int size) {
+ readByteCount += size;
+ }
+
+ protected final void incrReadOneByteCount() {
+ readByteCount += 1;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/BufferReferer.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/BufferReferer.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,25 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+public interface BufferReferer {
+ public void refer(ByteBuffer bb, boolean gift) throws IOException;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/BufferedOutput.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/BufferedOutput.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,219 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+abstract class BufferedOutput implements Output {
+ protected byte[] buffer;
+ protected int filled;
+ protected final int bufferSize;
+ protected ByteBuffer castByteBuffer;
+
+ public BufferedOutput(int bufferSize) {
+ if (bufferSize < 9) {
+ bufferSize = 9;
+ }
+ this.bufferSize = bufferSize;
+ }
+
+ private void allocateNewBuffer() {
+ buffer = new byte[bufferSize];
+ castByteBuffer = ByteBuffer.wrap(buffer);
+ }
+
+ private void reserve(int len) throws IOException {
+ if (buffer == null) {
+ allocateNewBuffer();
+ return;
+ }
+ if (bufferSize - filled < len) {
+ if (!flushBuffer(buffer, 0, filled)) {
+ buffer = new byte[bufferSize];
+ castByteBuffer = ByteBuffer.wrap(buffer);
+ }
+ filled = 0;
+ }
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException {
+ if (buffer == null) {
+ if (bufferSize < len) {
+ flushBuffer(b, off, len);
+ return;
+ }
+ allocateNewBuffer();
+ }
+ if (len <= bufferSize - filled) {
+ System.arraycopy(b, off, buffer, filled, len);
+ filled += len;
+ } else if (len <= bufferSize) {
+ if (!flushBuffer(buffer, 0, filled)) {
+ allocateNewBuffer();
+ }
+ filled = 0;
+ System.arraycopy(b, off, buffer, 0, len);
+ filled = len;
+ } else {
+ flush();
+ flushBuffer(b, off, len);
+ }
+ }
+
+ @Override
+ public void write(ByteBuffer bb) throws IOException {
+ int len = bb.remaining();
+ if (buffer == null) {
+ if (bufferSize < len) {
+ flushByteBuffer(bb);
+ return;
+ }
+ allocateNewBuffer();
+ }
+ if (len <= bufferSize - filled) {
+ bb.get(buffer, filled, len);
+ filled += len;
+ } else if (len <= bufferSize) {
+ if (!flushBuffer(buffer, 0, filled)) {
+ allocateNewBuffer();
+ }
+ filled = 0;
+ bb.get(buffer, 0, len);
+ filled = len;
+ } else {
+ flush();
+ flushByteBuffer(bb);
+ }
+ }
+
+ @Override
+ public void writeByte(byte v) throws IOException {
+ reserve(1);
+ buffer[filled++] = v;
+ }
+
+ @Override
+ public void writeShort(short v) throws IOException {
+ reserve(2);
+ castByteBuffer.putShort(filled, v);
+ filled += 2;
+ }
+
+ @Override
+ public void writeInt(int v) throws IOException {
+ reserve(4);
+ castByteBuffer.putInt(filled, v);
+ filled += 4;
+ }
+
+ @Override
+ public void writeLong(long v) throws IOException {
+ reserve(8);
+ castByteBuffer.putLong(filled, v);
+ filled += 8;
+ }
+
+ @Override
+ public void writeFloat(float v) throws IOException {
+ reserve(4);
+ castByteBuffer.putFloat(filled, v);
+ filled += 4;
+ }
+
+ @Override
+ public void writeDouble(double v) throws IOException {
+ reserve(8);
+ castByteBuffer.putDouble(filled, v);
+ filled += 8;
+ }
+
+ @Override
+ public void writeByteAndByte(byte b, byte v) throws IOException {
+ reserve(2);
+ buffer[filled++] = b;
+ buffer[filled++] = v;
+ }
+
+ @Override
+ public void writeByteAndShort(byte b, short v) throws IOException {
+ reserve(3);
+ buffer[filled++] = b;
+ castByteBuffer.putShort(filled, v);
+ filled += 2;
+ }
+
+ @Override
+ public void writeByteAndInt(byte b, int v) throws IOException {
+ reserve(5);
+ buffer[filled++] = b;
+ castByteBuffer.putInt(filled, v);
+ filled += 4;
+ }
+
+ @Override
+ public void writeByteAndLong(byte b, long v) throws IOException {
+ reserve(9);
+ buffer[filled++] = b;
+ castByteBuffer.putLong(filled, v);
+ filled += 8;
+ }
+
+ @Override
+ public void writeByteAndFloat(byte b, float v) throws IOException {
+ reserve(5);
+ buffer[filled++] = b;
+ castByteBuffer.putFloat(filled, v);
+ filled += 4;
+ }
+
+ @Override
+ public void writeByteAndDouble(byte b, double v) throws IOException {
+ reserve(9);
+ buffer[filled++] = b;
+ castByteBuffer.putDouble(filled, v);
+ filled += 8;
+ }
+
+ @Override
+ public void flush() throws IOException {
+ if (filled > 0) {
+ if (!flushBuffer(buffer, 0, filled)) {
+ buffer = null;
+ }
+ filled = 0;
+ }
+ }
+
+ protected void flushByteBuffer(ByteBuffer bb) throws IOException {
+ if (bb.hasArray()) {
+ byte[] array = bb.array();
+ int offset = bb.arrayOffset();
+ flushBuffer(array, offset + bb.position(), bb.remaining());
+ bb.position(bb.limit());
+ } else {
+ byte[] buf = new byte[bb.remaining()];
+ bb.get(buf);
+ flushBuffer(buf, 0, buf.length);
+ }
+ }
+
+ protected abstract boolean flushBuffer(byte[] buffer, int off, int len)
+ throws IOException;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/ByteBufferOutput.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/ByteBufferOutput.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,148 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.BufferOverflowException;
+
+public class ByteBufferOutput implements Output {
+ public static interface ExpandBufferCallback {
+ ByteBuffer call(ByteBuffer buffer, int len) throws IOException;
+ }
+
+ private ByteBuffer buffer;
+ private ExpandBufferCallback callback;
+
+ public ByteBufferOutput(ByteBuffer buffer) {
+ this(buffer, null);
+ }
+
+ public ByteBufferOutput(ByteBuffer buffer, ExpandBufferCallback callback) {
+ this.buffer = buffer;
+ this.callback = callback;
+ }
+
+ private void reserve(int len) throws IOException {
+ if (len <= buffer.remaining()) {
+ return;
+ }
+ if (callback == null) {
+ throw new BufferOverflowException();
+ }
+ buffer = callback.call(buffer, len);
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException {
+ reserve(len);
+ buffer.put(b, off, len);
+ }
+
+ @Override
+ public void write(ByteBuffer bb) throws IOException {
+ reserve(bb.remaining());
+ buffer.put(bb);
+ }
+
+ @Override
+ public void writeByte(byte v) throws IOException {
+ reserve(1);
+ buffer.put(v);
+ }
+
+ @Override
+ public void writeShort(short v) throws IOException {
+ reserve(2);
+ buffer.putShort(v);
+ }
+
+ @Override
+ public void writeInt(int v) throws IOException {
+ reserve(4);
+ buffer.putInt(v);
+ }
+
+ @Override
+ public void writeLong(long v) throws IOException {
+ reserve(8);
+ buffer.putLong(v);
+ }
+
+ @Override
+ public void writeFloat(float v) throws IOException {
+ reserve(4);
+ buffer.putFloat(v);
+ }
+
+ @Override
+ public void writeDouble(double v) throws IOException {
+ reserve(8);
+ buffer.putDouble(v);
+ }
+
+ @Override
+ public void writeByteAndByte(byte b, byte v) throws IOException {
+ reserve(2);
+ buffer.put(b);
+ buffer.put(v);
+ }
+
+ @Override
+ public void writeByteAndShort(byte b, short v) throws IOException {
+ reserve(3);
+ buffer.put(b);
+ buffer.putShort(v);
+ }
+
+ @Override
+ public void writeByteAndInt(byte b, int v) throws IOException {
+ reserve(5);
+ buffer.put(b);
+ buffer.putInt(v);
+ }
+
+ @Override
+ public void writeByteAndLong(byte b, long v) throws IOException {
+ reserve(9);
+ buffer.put(b);
+ buffer.putLong(v);
+ }
+
+ @Override
+ public void writeByteAndFloat(byte b, float v) throws IOException {
+ reserve(5);
+ buffer.put(b);
+ buffer.putFloat(v);
+ }
+
+ @Override
+ public void writeByteAndDouble(byte b, double v) throws IOException {
+ reserve(9);
+ buffer.put(b);
+ buffer.putDouble(v);
+ }
+
+ @Override
+ public void flush() throws IOException {
+ }
+
+ @Override
+ public void close() {
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/EndOfBufferException.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/EndOfBufferException.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,31 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.io.EOFException;
+
+@SuppressWarnings("serial")
+public class EndOfBufferException extends EOFException {
+ public EndOfBufferException() {
+ super();
+ }
+
+ public EndOfBufferException(String s) {
+ super(s);
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/Input.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/Input.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,47 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.io.IOException;
+import java.io.Closeable;
+
+public interface Input extends Closeable {
+ public int read(byte[] b, int off, int len) throws IOException;
+
+ public boolean tryRefer(BufferReferer ref, int len) throws IOException;
+
+ public byte readByte() throws IOException;
+
+ public void advance();
+
+ public byte getByte() throws IOException;
+
+ public short getShort() throws IOException;
+
+ public int getInt() throws IOException;
+
+ public long getLong() throws IOException;
+
+ public float getFloat() throws IOException;
+
+ public double getDouble() throws IOException;
+
+ public int getReadByteCount();
+
+ public void resetReadByteCount();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/LinkedBufferInput.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/LinkedBufferInput.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,399 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.io.IOException;
+import java.io.EOFException;
+import java.util.LinkedList;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.nio.ByteBuffer;
+
+public class LinkedBufferInput extends AbstractInput {
+ LinkedList link;
+
+ int writable;
+
+ private int nextAdvance;
+
+ private byte[] tmpBuffer;
+
+ private ByteBuffer tmpByteBuffer;
+
+ private final int bufferSize;
+
+ public LinkedBufferInput(int bufferSize) {
+ this.link = new LinkedList();
+ this.writable = -1;
+ this.tmpBuffer = new byte[8];
+ this.tmpByteBuffer = ByteBuffer.wrap(tmpBuffer);
+ this.bufferSize = bufferSize;
+ }
+
+ public int read(byte[] b, int off, int len) throws EOFException {
+ if (link.isEmpty()) {
+ return 0;
+ }
+ int olen = len;
+ while (true) {
+ ByteBuffer bb = link.getFirst();
+ if (len < bb.remaining()) {
+ bb.get(b, off, len);
+ incrReadByteCount(len);
+ return olen;
+ }
+ int rem = bb.remaining();
+ bb.get(b, off, rem);
+ incrReadByteCount(rem);
+ len -= rem;
+ off += rem;
+ if (!removeFirstLink(bb)) {
+ break;
+ }
+ }
+ return olen - len;
+ }
+
+ public boolean tryRefer(BufferReferer ref, int len) throws IOException {
+ ByteBuffer bb = null;
+ try {
+ bb = link.getFirst();
+ } catch(NoSuchElementException e) {}
+ if (bb == null) {
+ throw new EndOfBufferException();
+ } else if (bb.remaining() < len) {
+ return false;
+ }
+ boolean success = false;
+ int pos = bb.position();
+ int lim = bb.limit();
+ try {
+ bb.limit(pos + len);
+ ref.refer(bb, true);
+ incrReadByteCount(len);
+ success = true;
+ } finally {
+ bb.limit(lim);
+ if (success) {
+ bb.position(pos + len);
+ } else {
+ bb.position(pos);
+ }
+ if (bb.remaining() == 0) {
+ removeFirstLink(bb);
+ }
+ }
+ return true;
+ }
+
+ public byte readByte() throws EOFException {
+ ByteBuffer bb = null;
+ try {
+ bb = link.getFirst();
+ } catch(NoSuchElementException e) {}
+ if (bb == null || bb.remaining() == 0) {
+ throw new EndOfBufferException();
+ }
+ byte result = bb.get();
+ incrReadOneByteCount();
+ if (bb.remaining() == 0) {
+ removeFirstLink(bb);
+ }
+ return result;
+ }
+
+ public void advance() {
+ if (link.isEmpty()) {
+ return;
+ }
+ int len = nextAdvance;
+ ByteBuffer bb;
+ while (true) {
+ bb = link.getFirst();
+ if (len < bb.remaining()) {
+ bb.position(bb.position() + len);
+ break;
+ }
+ len -= bb.remaining();
+ bb.position(bb.position() + bb.remaining());
+ if (!removeFirstLink(bb)) {
+ break;
+ }
+ }
+ incrReadByteCount(nextAdvance);
+ nextAdvance = 0;
+ }
+
+ private boolean removeFirstLink(ByteBuffer first) {
+ if (link.size() == 1) {
+ if (writable >= 0) {
+ first.position(0);
+ first.limit(0);
+ writable = first.capacity();
+ return false;
+ } else {
+ link.removeFirst();
+ return false;
+ }
+ } else {
+ link.removeFirst();
+ return true;
+ }
+ }
+
+ private void requireMore(int n) throws EOFException {
+ int off = 0;
+ for (ByteBuffer bb : link) {
+ if (n <= bb.remaining()) {
+ int pos = bb.position();
+ bb.get(tmpBuffer, off, n);
+ bb.position(pos);
+ return;
+ }
+ int rem = bb.remaining();
+ int pos = bb.position();
+ bb.get(tmpBuffer, off, rem);
+ bb.position(pos);
+ n -= rem;
+ off += rem;
+ }
+ throw new EndOfBufferException();
+ }
+
+ private ByteBuffer require(int n) throws EOFException {
+ ByteBuffer bb = null;
+ try {
+ bb = link.getFirst();
+ } catch(NoSuchElementException e) {}
+ if (bb == null) {
+ throw new EndOfBufferException();
+ }
+ if (n <= bb.remaining()) {
+ nextAdvance = n;
+ return bb;
+ } else {
+ requireMore(n);
+ nextAdvance = n;
+ return tmpByteBuffer;
+ }
+ }
+
+ public byte getByte() throws EOFException {
+ ByteBuffer bb = require(1);
+ return bb.get(bb.position());
+ }
+
+ public short getShort() throws EOFException {
+ ByteBuffer bb = require(2);
+ return bb.getShort(bb.position());
+ }
+
+ public int getInt() throws EOFException {
+ ByteBuffer bb = require(4);
+ return bb.getInt(bb.position());
+ }
+
+ public long getLong() throws EOFException {
+ ByteBuffer bb = require(8);
+ return bb.getLong(bb.position());
+ }
+
+ public float getFloat() throws EOFException {
+ ByteBuffer bb = require(4);
+ return bb.getFloat(bb.position());
+ }
+
+ public double getDouble() throws EOFException {
+ ByteBuffer bb = require(8);
+ return bb.getDouble(bb.position());
+ }
+
+ public void feed(byte[] b) {
+ feed(b, 0, b.length, false);
+ }
+
+ public void feed(byte[] b, boolean reference) {
+ feed(b, 0, b.length, reference);
+ }
+
+ public void feed(byte[] b, int off, int len) {
+ feed(b, off, len, false);
+ }
+
+ public void feed(byte[] b, int off, int len, boolean reference) {
+ if (reference) {
+ if (writable > 0 && link.getLast().remaining() == 0) {
+ link.add(link.size()-1, ByteBuffer.wrap(b, off, len));
+ return;
+ }
+ link.addLast(ByteBuffer.wrap(b, off, len));
+ writable = -1;
+ return;
+ }
+
+ ByteBuffer bb = null;
+ try {
+ bb = link.getLast();
+ } catch(NoSuchElementException e) {}
+ if (len <= writable) {
+ int pos = bb.position();
+ bb.position(bb.limit());
+ bb.limit(bb.limit() + len);
+ bb.put(b, off, len);
+ bb.position(pos);
+ writable = bb.capacity() - bb.limit();
+ return;
+ }
+
+ if (writable > 0) {
+ int pos = bb.position();
+ bb.position(bb.limit());
+ bb.limit(bb.limit() + writable);
+ bb.put(b, off, writable);
+ bb.position(pos);
+ off += writable;
+ len -= writable;
+ writable = 0;
+ }
+
+ int sz = Math.max(len, bufferSize);
+ ByteBuffer nb = ByteBuffer.allocate(sz);
+ nb.put(b, off, len);
+ nb.limit(len);
+ nb.position(0);
+ link.addLast(nb);
+ writable = sz - len;
+ }
+
+ public void feed(ByteBuffer b) {
+ feed(b, false);
+ }
+
+ public void feed(ByteBuffer buf, boolean reference) {
+ if (reference) {
+ if (writable > 0 && link.getLast().remaining() == 0) {
+ link.add(link.size()-1, buf);
+ return;
+ }
+ link.addLast(buf);
+ writable = -1;
+ return;
+ }
+
+ int rem = buf.remaining();
+
+ ByteBuffer bb = null;
+ try {
+ bb = link.getLast();
+ } catch(NoSuchElementException e) {}
+ if (rem <= writable) {
+ int pos = bb.position();
+ bb.position(bb.limit());
+ bb.limit(bb.limit() + rem);
+ bb.put(buf);
+ bb.position(pos);
+ writable = bb.capacity() - bb.limit();
+ return;
+ }
+
+ if (writable > 0) {
+ int pos = bb.position();
+ bb.position(bb.limit());
+ bb.limit(bb.limit() + writable);
+ buf.limit(writable);
+ bb.put(buf);
+ bb.position(pos);
+ rem -= writable;
+ buf.limit(buf.limit() + rem);
+ writable = 0;
+ }
+
+ int sz = Math.max(rem, bufferSize);
+ ByteBuffer nb = ByteBuffer.allocate(sz);
+ nb.put(buf);
+ nb.limit(rem);
+ nb.position(0);
+ link.addLast(nb);
+ writable = sz - rem;
+ }
+
+ public void clear() {
+ if (writable >= 0) {
+ ByteBuffer bb = link.getLast();
+ link.clear();
+ bb.position(0);
+ bb.limit(0);
+ link.addLast(bb);
+ writable = bb.capacity();
+ } else {
+ link.clear();
+ writable = -1;
+ }
+ }
+
+ public void copyReferencedBuffer() {
+ if (link.isEmpty()) {
+ return;
+ }
+
+ int size = 0;
+ for(ByteBuffer bb : link) {
+ size += bb.remaining();
+ }
+ if (size == 0) {
+ return;
+ }
+
+ if (writable >= 0) {
+ ByteBuffer last = link.removeLast();
+ byte[] copy = new byte[size - last.remaining()];
+ int off = 0;
+ for(ByteBuffer bb : link) {
+ int len = bb.remaining();
+ bb.get(copy, off, len);
+ off += len;
+ }
+ link.clear();
+ link.add(ByteBuffer.wrap(copy));
+ link.add(last);
+
+ } else {
+ byte[] copy = new byte[size];
+ int off = 0;
+ for(ByteBuffer bb : link) {
+ int len = bb.remaining();
+ bb.get(copy, off, len);
+ off += len;
+ }
+ link.clear();
+ link.add(ByteBuffer.wrap(copy));
+ writable = 0;
+ }
+ }
+
+ public int getSize() {
+ int size = 0;
+ for(ByteBuffer bb : link) {
+ size += bb.remaining();
+ }
+ return size;
+ }
+
+ public void close() {
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/LinkedBufferOutput.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/LinkedBufferOutput.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,76 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.util.LinkedList;
+
+public final class LinkedBufferOutput extends BufferedOutput {
+ private static final class Link {
+ final byte[] buffer;
+ final int offset;
+ final int size;
+
+ Link(byte[] buffer, int offset, int size) {
+ this.buffer = buffer;
+ this.offset = offset;
+ this.size = size;
+ }
+ }
+
+ private LinkedList link;
+ private int size;
+
+ public LinkedBufferOutput(int bufferSize) {
+ super(bufferSize);
+ link = new LinkedList ();
+ }
+
+ public byte[] toByteArray() {
+ byte[] bytes = new byte[size + filled];
+ int off = 0;
+ for (Link l : link) {
+ System.arraycopy(l.buffer, l.offset, bytes, off, l.size);
+ off += l.size;
+ }
+ if (filled > 0) {
+ System.arraycopy(buffer, 0, bytes, off, filled);
+ }
+ return bytes;
+ }
+
+ public int getSize() {
+ return size + filled;
+ }
+
+ @Override
+ protected boolean flushBuffer(byte[] b, int off, int len) {
+ link.add(new Link(b, off, len));
+ size += len;
+ return false;
+ }
+
+ public void clear() {
+ link.clear();
+ size = 0;
+ filled = 0;
+ }
+
+ @Override
+ public void close() {
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/Output.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/Output.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,53 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.io.IOException;
+import java.io.Closeable;
+import java.io.Flushable;
+import java.nio.ByteBuffer;
+
+public interface Output extends Closeable, Flushable {
+ public void write(byte[] b, int off, int len) throws IOException;
+
+ public void write(ByteBuffer bb) throws IOException;
+
+ public void writeByte(byte v) throws IOException;
+
+ public void writeShort(short v) throws IOException;
+
+ public void writeInt(int v) throws IOException;
+
+ public void writeLong(long v) throws IOException;
+
+ public void writeFloat(float v) throws IOException;
+
+ public void writeDouble(double v) throws IOException;
+
+ public void writeByteAndByte(byte b, byte v) throws IOException;
+
+ public void writeByteAndShort(byte b, short v) throws IOException;
+
+ public void writeByteAndInt(byte b, int v) throws IOException;
+
+ public void writeByteAndLong(byte b, long v) throws IOException;
+
+ public void writeByteAndFloat(byte b, float v) throws IOException;
+
+ public void writeByteAndDouble(byte b, double v) throws IOException;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/StreamInput.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/StreamInput.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,114 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.io.IOException;
+import java.io.EOFException;
+
+public class StreamInput extends AbstractInput {
+ private final InputStream in;
+
+ private byte[] castBuffer;
+ private ByteBuffer castByteBuffer;
+ private int filled;
+
+ public StreamInput(InputStream in) {
+ this.in = in;
+ this.castBuffer = new byte[8];
+ this.castByteBuffer = ByteBuffer.wrap(castBuffer);
+ this.filled = 0;
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException {
+ int remain = len;
+ while (remain > 0) {
+ int n = in.read(b, off, remain);
+ if (n <= 0) {
+ throw new EOFException();
+ }
+ incrReadByteCount(n);
+ remain -= n;
+ off += n;
+ }
+ return len;
+ }
+
+ public boolean tryRefer(BufferReferer ref, int size) throws IOException {
+ return false;
+ }
+
+ public byte readByte() throws IOException {
+ int n = in.read();
+ if (n < 0) {
+ throw new EOFException();
+ }
+ incrReadOneByteCount();
+ return (byte) n;
+ }
+
+ public void advance() {
+ incrReadByteCount(filled);
+ filled = 0;
+ }
+
+ private void require(int len) throws IOException {
+ while (filled < len) {
+ int n = in.read(castBuffer, filled, len - filled);
+ if (n < 0) {
+ throw new EOFException();
+ }
+ filled += n;
+ }
+ }
+
+ public byte getByte() throws IOException {
+ require(1);
+ return castBuffer[0];
+ }
+
+ public short getShort() throws IOException {
+ require(2);
+ return castByteBuffer.getShort(0);
+ }
+
+ public int getInt() throws IOException {
+ require(4);
+ return castByteBuffer.getInt(0);
+ }
+
+ public long getLong() throws IOException {
+ require(8);
+ return castByteBuffer.getLong(0);
+ }
+
+ public float getFloat() throws IOException {
+ require(4);
+ return castByteBuffer.getFloat(0);
+ }
+
+ public double getDouble() throws IOException {
+ require(8);
+ return castByteBuffer.getDouble(0);
+ }
+
+ public void close() throws IOException {
+ in.close();
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/io/StreamOutput.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/io/StreamOutput.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,126 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.io;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.DataOutputStream;
+import java.nio.ByteBuffer;
+
+public class StreamOutput implements Output {
+ private DataOutputStream out;
+
+ public StreamOutput(OutputStream out) {
+ this.out = new DataOutputStream(out);
+ }
+
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException {
+ out.write(b, off, len);
+ }
+
+ @Override
+ public void write(ByteBuffer bb) throws IOException {
+ if (bb.hasArray()) {
+ byte[] array = bb.array();
+ int offset = bb.arrayOffset();
+ out.write(array, offset, bb.remaining());
+ bb.position(bb.limit());
+ } else {
+ // int pos = bb.position();
+ byte[] buf = new byte[bb.remaining()];
+ bb.get(buf);
+ out.write(buf);
+ }
+ }
+
+ @Override
+ public void writeByte(byte v) throws IOException {
+ out.write(v);
+ }
+
+ @Override
+ public void writeShort(short v) throws IOException {
+ out.writeShort(v);
+ }
+
+ @Override
+ public void writeInt(int v) throws IOException {
+ out.writeInt(v);
+ }
+
+ @Override
+ public void writeLong(long v) throws IOException {
+ out.writeLong(v);
+ }
+
+ @Override
+ public void writeFloat(float v) throws IOException {
+ out.writeFloat(v);
+ }
+
+ @Override
+ public void writeDouble(double v) throws IOException {
+ out.writeDouble(v);
+ }
+
+ @Override
+ public void writeByteAndByte(byte b, byte v) throws IOException {
+ out.write(b);
+ out.write(v);
+ }
+
+ @Override
+ public void writeByteAndShort(byte b, short v) throws IOException {
+ out.write(b);
+ out.writeShort(v);
+ }
+
+ @Override
+ public void writeByteAndInt(byte b, int v) throws IOException {
+ out.write(b);
+ out.writeInt(v);
+ }
+
+ @Override
+ public void writeByteAndLong(byte b, long v) throws IOException {
+ out.write(b);
+ out.writeLong(v);
+ }
+
+ @Override
+ public void writeByteAndFloat(byte b, float v) throws IOException {
+ out.write(b);
+ out.writeFloat(v);
+ }
+
+ @Override
+ public void writeByteAndDouble(byte b, double v) throws IOException {
+ out.write(b);
+ out.writeDouble(v);
+ }
+
+ @Override
+ public void flush() throws IOException {
+ }
+
+ @Override
+ public void close() throws IOException {
+ out.close();
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/packer/AbstractPacker.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/packer/AbstractPacker.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,259 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.packer;
+
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.io.IOException;
+import org.msgpack.type.Value;
+import org.msgpack.MessagePack;
+import org.msgpack.template.Template;
+
+public abstract class AbstractPacker implements Packer {
+ protected MessagePack msgpack;
+
+ protected AbstractPacker(MessagePack msgpack) {
+ this.msgpack = msgpack;
+ }
+
+ @Override
+ public Packer write(boolean o) throws IOException {
+ writeBoolean(o);
+ return this;
+ }
+
+ @Override
+ public Packer write(byte o) throws IOException {
+ writeByte(o);
+ return this;
+ }
+
+ @Override
+ public Packer write(short o) throws IOException {
+ writeShort(o);
+ return this;
+ }
+
+ @Override
+ public Packer write(int o) throws IOException {
+ writeInt(o);
+ return this;
+ }
+
+ @Override
+ public Packer write(long o) throws IOException {
+ writeLong(o);
+ return this;
+ }
+
+ @Override
+ public Packer write(float o) throws IOException {
+ writeFloat(o);
+ return this;
+ }
+
+ @Override
+ public Packer write(double o) throws IOException {
+ writeDouble(o);
+ return this;
+ }
+
+ @Override
+ public Packer write(Boolean o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeBoolean(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(Byte o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeByte(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(Short o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeShort(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(Integer o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeInt(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(Long o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeLong(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(BigInteger o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeBigInteger(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(Float o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeFloat(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(Double o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeDouble(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(byte[] o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeByteArray(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(byte[] o, int off, int len) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeByteArray(o, off, len);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(ByteBuffer o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeByteBuffer(o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(String o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ writeString(o);
+ }
+ return this;
+ }
+
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ @Override
+ public Packer write(Object o) throws IOException {
+ if (o == null) {
+ writeNil();
+ } else {
+ Template tmpl = msgpack.lookup(o.getClass());
+ tmpl.write(this, o);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(Value v) throws IOException {
+ if (v == null) {
+ writeNil();
+ } else {
+ v.writeTo(this);
+ }
+ return this;
+ }
+
+ @Override
+ public Packer writeArrayEnd() throws IOException {
+ writeArrayEnd(true);
+ return this;
+ }
+
+ @Override
+ public Packer writeMapEnd() throws IOException {
+ writeMapEnd(true);
+ return this;
+ }
+
+ @Override
+ public void close() throws IOException {
+ }
+
+ abstract protected void writeBoolean(boolean v) throws IOException;
+
+ abstract protected void writeByte(byte v) throws IOException;
+
+ abstract protected void writeShort(short v) throws IOException;
+
+ abstract protected void writeInt(int v) throws IOException;
+
+ abstract protected void writeLong(long v) throws IOException;
+
+ abstract protected void writeBigInteger(BigInteger v) throws IOException;
+
+ abstract protected void writeFloat(float v) throws IOException;
+
+ abstract protected void writeDouble(double v) throws IOException;
+
+ protected void writeByteArray(byte[] b) throws IOException {
+ writeByteArray(b, 0, b.length);
+ }
+
+ abstract protected void writeByteArray(byte[] b, int off, int len) throws IOException;
+
+ abstract protected void writeByteBuffer(ByteBuffer bb) throws IOException;
+
+ abstract protected void writeString(String s) throws IOException;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/packer/BufferPacker.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/packer/BufferPacker.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,32 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.packer;
+
+/**
+ * This class is buffer-specific serializer.
+ *
+ * @version 0.6.0
+ * @see {@link org.msgpack.packer.Packer}
+ */
+public interface BufferPacker extends Packer {
+ public int getBufferSize();
+
+ public byte[] toByteArray();
+
+ public void clear();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/packer/MessagePackBufferPacker.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/packer/MessagePackBufferPacker.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,46 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.packer;
+
+import org.msgpack.MessagePack;
+import org.msgpack.io.LinkedBufferOutput;
+
+public class MessagePackBufferPacker extends MessagePackPacker implements BufferPacker {
+ private static final int DEFAULT_BUFFER_SIZE = 512;
+
+ public MessagePackBufferPacker(MessagePack msgpack) {
+ this(msgpack, DEFAULT_BUFFER_SIZE);
+ }
+
+ public MessagePackBufferPacker(MessagePack msgpack, int bufferSize) {
+ super(msgpack, new LinkedBufferOutput(bufferSize));
+ }
+
+ public int getBufferSize() {
+ return ((LinkedBufferOutput) out).getSize();
+ }
+
+ public byte[] toByteArray() {
+ return ((LinkedBufferOutput) out).toByteArray();
+ }
+
+ public void clear() {
+ reset();
+ ((LinkedBufferOutput) out).clear();
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/packer/MessagePackPacker.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/packer/MessagePackPacker.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,334 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.packer;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.math.BigInteger;
+import org.msgpack.io.Output;
+import org.msgpack.io.StreamOutput;
+import org.msgpack.MessagePack;
+import org.msgpack.MessageTypeException;
+
+public class MessagePackPacker extends AbstractPacker {
+ protected final Output out;
+
+ private PackerStack stack = new PackerStack();
+
+ public MessagePackPacker(MessagePack msgpack, OutputStream stream) {
+ this(msgpack, new StreamOutput(stream));
+ }
+
+ protected MessagePackPacker(MessagePack msgpack, Output out) {
+ super(msgpack);
+ this.out = out;
+ }
+
+ @Override
+ protected void writeByte(byte d) throws IOException {
+ if (d < -(1 << 5)) {
+ out.writeByteAndByte((byte) 0xd0, d);
+ } else {
+ out.writeByte(d);
+ }
+ stack.reduceCount();
+ }
+
+ @Override
+ protected void writeShort(short d) throws IOException {
+ if (d < -(1 << 5)) {
+ if (d < -(1 << 7)) {
+ // signed 16
+ out.writeByteAndShort((byte) 0xd1, d);
+ } else {
+ // signed 8
+ out.writeByteAndByte((byte) 0xd0, (byte) d);
+ }
+ } else if (d < (1 << 7)) {
+ // fixnum
+ out.writeByte((byte) d);
+ } else {
+ if (d < (1 << 8)) {
+ // unsigned 8
+ out.writeByteAndByte((byte) 0xcc, (byte) d);
+ } else {
+ // unsigned 16
+ out.writeByteAndShort((byte) 0xcd, d);
+ }
+ }
+ stack.reduceCount();
+ }
+
+ @Override
+ protected void writeInt(int d) throws IOException {
+ if (d < -(1 << 5)) {
+ if (d < -(1 << 15)) {
+ // signed 32
+ out.writeByteAndInt((byte) 0xd2, d);
+ } else if (d < -(1 << 7)) {
+ // signed 16
+ out.writeByteAndShort((byte) 0xd1, (short) d);
+ } else {
+ // signed 8
+ out.writeByteAndByte((byte) 0xd0, (byte) d);
+ }
+ } else if (d < (1 << 7)) {
+ // fixnum
+ out.writeByte((byte) d);
+ } else {
+ if (d < (1 << 8)) {
+ // unsigned 8
+ out.writeByteAndByte((byte) 0xcc, (byte) d);
+ } else if (d < (1 << 16)) {
+ // unsigned 16
+ out.writeByteAndShort((byte) 0xcd, (short) d);
+ } else {
+ // unsigned 32
+ out.writeByteAndInt((byte) 0xce, d);
+ }
+ }
+ stack.reduceCount();
+ }
+
+ @Override
+ protected void writeLong(long d) throws IOException {
+ if (d < -(1L << 5)) {
+ if (d < -(1L << 15)) {
+ if (d < -(1L << 31)) {
+ // signed 64
+ out.writeByteAndLong((byte) 0xd3, d);
+ } else {
+ // signed 32
+ out.writeByteAndInt((byte) 0xd2, (int) d);
+ }
+ } else {
+ if (d < -(1 << 7)) {
+ // signed 16
+ out.writeByteAndShort((byte) 0xd1, (short) d);
+ } else {
+ // signed 8
+ out.writeByteAndByte((byte) 0xd0, (byte) d);
+ }
+ }
+ } else if (d < (1 << 7)) {
+ // fixnum
+ out.writeByte((byte) d);
+ } else {
+ if (d < (1L << 16)) {
+ if (d < (1 << 8)) {
+ // unsigned 8
+ out.writeByteAndByte((byte) 0xcc, (byte) d);
+ } else {
+ // unsigned 16
+ out.writeByteAndShort((byte) 0xcd, (short) d);
+ }
+ } else {
+ if (d < (1L << 32)) {
+ // unsigned 32
+ out.writeByteAndInt((byte) 0xce, (int) d);
+ } else {
+ // unsigned 64
+ out.writeByteAndLong((byte) 0xcf, d);
+ }
+ }
+ }
+ stack.reduceCount();
+ }
+
+ @Override
+ protected void writeBigInteger(BigInteger d) throws IOException {
+ if (d.bitLength() <= 63) {
+ writeLong(d.longValue());
+ stack.reduceCount();
+ } else if (d.bitLength() == 64 && d.signum() == 1) {
+ // unsigned 64
+ out.writeByteAndLong((byte) 0xcf, d.longValue());
+ stack.reduceCount();
+ } else {
+ throw new MessageTypeException(
+ "MessagePack can't serialize BigInteger larger than (2^64)-1");
+ }
+ }
+
+ @Override
+ protected void writeFloat(float d) throws IOException {
+ out.writeByteAndFloat((byte) 0xca, d);
+ stack.reduceCount();
+ }
+
+ @Override
+ protected void writeDouble(double d) throws IOException {
+ out.writeByteAndDouble((byte) 0xcb, d);
+ stack.reduceCount();
+ }
+
+ @Override
+ protected void writeBoolean(boolean d) throws IOException {
+ if (d) {
+ // true
+ out.writeByte((byte) 0xc3);
+ } else {
+ // false
+ out.writeByte((byte) 0xc2);
+ }
+ stack.reduceCount();
+ }
+
+ @Override
+ protected void writeByteArray(byte[] b, int off, int len)
+ throws IOException {
+ if (len < 32) {
+ out.writeByte((byte) (0xa0 | len));
+ } else if (len < 65536) {
+ out.writeByteAndShort((byte) 0xda, (short) len);
+ } else {
+ out.writeByteAndInt((byte) 0xdb, len);
+ }
+ out.write(b, off, len);
+ stack.reduceCount();
+ }
+
+ @Override
+ protected void writeByteBuffer(ByteBuffer bb) throws IOException {
+ int len = bb.remaining();
+ if (len < 32) {
+ out.writeByte((byte) (0xa0 | len));
+ } else if (len < 65536) {
+ out.writeByteAndShort((byte) 0xda, (short) len);
+ } else {
+ out.writeByteAndInt((byte) 0xdb, len);
+ }
+ int pos = bb.position();
+ try {
+ out.write(bb);
+ } finally {
+ bb.position(pos);
+ }
+ stack.reduceCount();
+ }
+
+ @Override
+ protected void writeString(String s) throws IOException {
+ byte[] b;
+ try {
+ // TODO encoding error?
+ b = s.getBytes("UTF-8");
+ } catch (UnsupportedEncodingException ex) {
+ throw new MessageTypeException(ex);
+ }
+ writeByteArray(b, 0, b.length);
+ stack.reduceCount();
+ }
+
+ @Override
+ public Packer writeNil() throws IOException {
+ out.writeByte((byte) 0xc0);
+ stack.reduceCount();
+ return this;
+ }
+
+ @Override
+ public Packer writeArrayBegin(int size) throws IOException {
+ // TODO check size < 0?
+ if (size < 16) {
+ // FixArray
+ out.writeByte((byte) (0x90 | size));
+ } else if (size < 65536) {
+ out.writeByteAndShort((byte) 0xdc, (short) size);
+ } else {
+ out.writeByteAndInt((byte) 0xdd, size);
+ }
+ stack.reduceCount();
+ stack.pushArray(size);
+ return this;
+ }
+
+ @Override
+ public Packer writeArrayEnd(boolean check) throws IOException {
+ if (!stack.topIsArray()) {
+ throw new MessageTypeException(
+ "writeArrayEnd() is called but writeArrayBegin() is not called");
+ }
+
+ int remain = stack.getTopCount();
+ if (remain > 0) {
+ if (check) {
+ throw new MessageTypeException(
+ "writeArrayEnd(check=true) is called but the array is not end: " + remain);
+ }
+ for (int i = 0; i < remain; i++) {
+ writeNil();
+ }
+ }
+ stack.pop();
+ return this;
+ }
+
+ @Override
+ public Packer writeMapBegin(int size) throws IOException {
+ // TODO check size < 0?
+ if (size < 16) {
+ // FixMap
+ out.writeByte((byte) (0x80 | size));
+ } else if (size < 65536) {
+ out.writeByteAndShort((byte) 0xde, (short) size);
+ } else {
+ out.writeByteAndInt((byte) 0xdf, size);
+ }
+ stack.reduceCount();
+ stack.pushMap(size);
+ return this;
+ }
+
+ @Override
+ public Packer writeMapEnd(boolean check) throws IOException {
+ if (!stack.topIsMap()) {
+ throw new MessageTypeException(
+ "writeMapEnd() is called but writeMapBegin() is not called");
+ }
+
+ int remain = stack.getTopCount();
+ if (remain > 0) {
+ if (check) {
+ throw new MessageTypeException(
+ "writeMapEnd(check=true) is called but the map is not end: " + remain);
+ }
+ for (int i = 0; i < remain; i++) {
+ writeNil();
+ }
+ }
+ stack.pop();
+ return this;
+ }
+
+ public void reset() {
+ stack.clear();
+ }
+
+ @Override
+ public void flush() throws IOException {
+ out.flush();
+ }
+
+ @Override
+ public void close() throws IOException {
+ out.close();
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/packer/Packer.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/packer/Packer.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,90 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.packer;
+
+import java.math.BigInteger;
+import java.io.IOException;
+import java.io.Closeable;
+import java.io.Flushable;
+import java.nio.ByteBuffer;
+import org.msgpack.type.Value;
+
+/**
+ * Standard serializer in MessagePack for Java. It allows users to serialize
+ * objects like String , List , Map , byte[] ,
+ * primitive types and so on.
+ *
+ * @version 0.6.0
+ */
+public interface Packer extends Closeable, Flushable {
+ public Packer write(boolean o) throws IOException;
+
+ public Packer write(byte o) throws IOException;
+
+ public Packer write(short o) throws IOException;
+
+ public Packer write(int o) throws IOException;
+
+ public Packer write(long o) throws IOException;
+
+ public Packer write(float o) throws IOException;
+
+ public Packer write(double o) throws IOException;
+
+ public Packer write(Boolean o) throws IOException;
+
+ public Packer write(Byte o) throws IOException;
+
+ public Packer write(Short o) throws IOException;
+
+ public Packer write(Integer o) throws IOException;
+
+ public Packer write(Long o) throws IOException;
+
+ public Packer write(Float o) throws IOException;
+
+ public Packer write(Double o) throws IOException;
+
+ public Packer write(BigInteger o) throws IOException;
+
+ public Packer write(byte[] o) throws IOException;
+
+ public Packer write(byte[] o, int off, int len) throws IOException;
+
+ public Packer write(ByteBuffer o) throws IOException;
+
+ public Packer write(String o) throws IOException;
+
+ public Packer write(Value v) throws IOException;
+
+ public Packer write(Object o) throws IOException;
+
+ public Packer writeNil() throws IOException;
+
+ public Packer writeArrayBegin(int size) throws IOException;
+
+ public Packer writeArrayEnd(boolean check) throws IOException;
+
+ public Packer writeArrayEnd() throws IOException;
+
+ public Packer writeMapBegin(int size) throws IOException;
+
+ public Packer writeMapEnd(boolean check) throws IOException;
+
+ public Packer writeMapEnd() throws IOException;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/packer/PackerStack.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/packer/PackerStack.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,98 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.packer;
+
+import org.msgpack.MessageTypeException;
+
+public final class PackerStack {
+ private int top;
+ private byte[] types;
+ private int[] counts;
+
+ public static final int MAX_STACK_SIZE = 128;
+
+ private static final byte TYPE_INVALID = 0;
+ private static final byte TYPE_ARRAY = 1;
+ private static final byte TYPE_MAP = 2;
+
+ public PackerStack() {
+ this.top = 0;
+ this.types = new byte[MAX_STACK_SIZE];
+ this.counts = new int[MAX_STACK_SIZE];
+ this.types[0] = TYPE_INVALID;
+ }
+
+ public void pushArray(int size) {
+ top++;
+ types[top] = TYPE_ARRAY;
+ counts[top] = size;
+ }
+
+ public void pushMap(int size) {
+ top++;
+ types[top] = TYPE_MAP;
+ counts[top] = size * 2;
+ }
+
+ public void checkCount() {
+ if (counts[top] > 0) {
+ return;
+ }
+
+ if (types[top] == TYPE_ARRAY) {
+ throw new MessageTypeException(
+ "Array is end but writeArrayEnd() is not called");
+
+ } else if (types[top] == TYPE_MAP) {
+ throw new MessageTypeException(
+ "Map is end but writeMapEnd() is not called");
+
+ } else {
+ // empty
+ return;
+ }
+ }
+
+ public void reduceCount() {
+ counts[top]--;
+ }
+
+ public void pop() {
+ top--;
+ }
+
+ public int getDepth() {
+ return top;
+ }
+
+ public int getTopCount() {
+ return counts[top];
+ }
+
+ public boolean topIsArray() {
+ return types[top] == TYPE_ARRAY;
+ }
+
+ public boolean topIsMap() {
+ return types[top] == TYPE_MAP;
+ }
+
+ public void clear() {
+ top = 0;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/packer/Unconverter.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/packer/Unconverter.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,230 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.packer;
+
+import java.io.IOException;
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+
+import org.msgpack.MessagePack;
+import org.msgpack.MessageTypeException;
+import org.msgpack.type.Value;
+import org.msgpack.type.ValueFactory;
+
+public class Unconverter extends AbstractPacker {
+ private PackerStack stack;
+ private Object[] values;
+ private Value result;
+
+ // private Value topContainer;
+
+ public Unconverter() {
+ this(new MessagePack());
+ }
+
+ public Unconverter(MessagePack msgpack) {
+ super(msgpack);
+ this.stack = new PackerStack();
+ this.values = new Object[PackerStack.MAX_STACK_SIZE];
+ }
+
+ public Value getResult() {
+ return result;
+ }
+
+ public void resetResult() {
+ this.result = null;
+ }
+
+ @Override
+ public void writeBoolean(boolean v) throws IOException {
+ put(ValueFactory.createBooleanValue(v));
+ }
+
+ @Override
+ public void writeByte(byte v) throws IOException {
+ put(ValueFactory.createIntegerValue(v));
+ }
+
+ @Override
+ public void writeShort(short v) throws IOException {
+ put(ValueFactory.createIntegerValue(v));
+ }
+
+ @Override
+ public void writeInt(int v) throws IOException {
+ put(ValueFactory.createIntegerValue(v));
+ }
+
+ @Override
+ public void writeBigInteger(BigInteger v) throws IOException {
+ put(ValueFactory.createIntegerValue(v));
+ }
+
+ @Override
+ public void writeLong(long v) throws IOException {
+ put(ValueFactory.createIntegerValue(v));
+ }
+
+ @Override
+ public void writeFloat(float v) throws IOException {
+ put(ValueFactory.createFloatValue(v));
+ }
+
+ @Override
+ public void writeDouble(double v) throws IOException {
+ put(ValueFactory.createFloatValue(v));
+ }
+
+ @Override
+ public void writeByteArray(byte[] b, int off, int len) throws IOException {
+ put(ValueFactory.createRawValue(b, off, len));
+ }
+
+ @Override
+ public void writeByteBuffer(ByteBuffer bb) throws IOException {
+ put(ValueFactory.createRawValue(bb));
+ }
+
+ @Override
+ public void writeString(String s) throws IOException {
+ put(ValueFactory.createRawValue(s));
+ }
+
+ @Override
+ public Packer writeNil() throws IOException {
+ put(ValueFactory.createNilValue());
+ return this;
+ }
+
+ @Override
+ public Packer writeArrayBegin(int size) throws IOException {
+ if (size == 0) {
+ // Value[] array = new Value[size];
+ putContainer(ValueFactory.createArrayValue());
+ stack.pushArray(0);
+ values[stack.getDepth()] = null;
+ } else {
+ Value[] array = new Value[size];
+ putContainer(ValueFactory.createArrayValue(array, true));
+ stack.pushArray(size);
+ values[stack.getDepth()] = array;
+ }
+ return this;
+ }
+
+ @Override
+ public Packer writeArrayEnd(boolean check) throws IOException {
+ if (!stack.topIsArray()) {
+ throw new MessageTypeException(
+ "writeArrayEnd() is called but writeArrayBegin() is not called");
+ }
+
+ int remain = stack.getTopCount();
+ if (remain > 0) {
+ if (check) {
+ throw new MessageTypeException(
+ "writeArrayEnd(check=true) is called but the array is not end");
+ }
+ for (int i = 0; i < remain; i++) {
+ writeNil();
+ }
+ }
+ stack.pop();
+ if (stack.getDepth() <= 0) {
+ this.result = (Value) values[0];
+ }
+ return this;
+ }
+
+ @Override
+ public Packer writeMapBegin(int size) throws IOException {
+ stack.checkCount();
+ if (size == 0) {
+ putContainer(ValueFactory.createMapValue());
+ stack.pushMap(0);
+ values[stack.getDepth()] = null;
+ } else {
+ Value[] array = new Value[size * 2];
+ putContainer(ValueFactory.createMapValue(array, true));
+ stack.pushMap(size);
+ values[stack.getDepth()] = array;
+ }
+ return this;
+ }
+
+ @Override
+ public Packer writeMapEnd(boolean check) throws IOException {
+ if (!stack.topIsMap()) {
+ throw new MessageTypeException(
+ "writeMapEnd() is called but writeMapBegin() is not called");
+ }
+
+ int remain = stack.getTopCount();
+ if (remain > 0) {
+ if (check) {
+ throw new MessageTypeException(
+ "writeMapEnd(check=true) is called but the map is not end");
+ }
+ for (int i = 0; i < remain; i++) {
+ writeNil();
+ }
+ }
+ stack.pop();
+ if (stack.getDepth() <= 0) {
+ this.result = (Value) values[0];
+ }
+ return this;
+ }
+
+ @Override
+ public Packer write(Value v) throws IOException {
+ put(v);
+ return this;
+ }
+
+ private void put(Value v) {
+ if (stack.getDepth() <= 0) {
+ this.result = v;
+ } else {
+ stack.checkCount();
+ Value[] array = (Value[]) values[stack.getDepth()];
+ array[array.length - stack.getTopCount()] = v;
+ stack.reduceCount();
+ }
+ }
+
+ private void putContainer(Value v) {
+ if (stack.getDepth() <= 0) {
+ values[0] = (Object) v;
+ } else {
+ stack.checkCount();
+ Value[] array = (Value[]) values[stack.getDepth()];
+ array[array.length - stack.getTopCount()] = v;
+ stack.reduceCount();
+ }
+ }
+
+ @Override
+ public void flush() throws IOException {
+ }
+
+ @Override
+ public void close() throws IOException {
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/AbstractTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/AbstractTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,33 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+
+public abstract class AbstractTemplate implements Template {
+
+ public void write(Packer pk, T v) throws IOException {
+ write(pk, v, false);
+ }
+
+ public T read(Unpacker u, T to) throws IOException {
+ return read(u, to, false);
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/AnyTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/AnyTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,60 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+
+import org.msgpack.MessageTypeException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+
+public class AnyTemplate extends AbstractTemplate {
+
+ private TemplateRegistry registry;
+
+ public AnyTemplate(TemplateRegistry registry) {
+ this.registry = registry;
+ }
+
+ @SuppressWarnings("unchecked")
+ public void write(Packer pk, T target, boolean required) throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ } else {
+ registry.lookup(target.getClass()).write(pk, target);
+ }
+ }
+
+ public T read(Unpacker u, T to, boolean required) throws IOException,
+ MessageTypeException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ if (to == null) {
+ throw new MessageTypeException("convert into unknown type is invalid");
+ }
+ T o = u.read(to);
+ if (required && o == null) {
+ throw new MessageTypeException("Unexpected nil value");
+ }
+ return o;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/BigDecimalTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/BigDecimalTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,56 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class BigDecimalTemplate extends AbstractTemplate {
+ private BigDecimalTemplate() {
+ }
+
+ public void write(Packer pk, BigDecimal target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write(target.toString());
+ }
+
+ public BigDecimal read(Unpacker u, BigDecimal to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ String temp = u.readString();
+ return new BigDecimal(temp);
+ }
+
+ static public BigDecimalTemplate getInstance() {
+ return instance;
+ }
+
+ static final BigDecimalTemplate instance = new BigDecimalTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/BigIntegerTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/BigIntegerTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,55 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.math.BigInteger;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class BigIntegerTemplate extends AbstractTemplate {
+ private BigIntegerTemplate() {
+ }
+
+ public void write(Packer pk, BigInteger target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write((BigInteger) target);
+ }
+
+ public BigInteger read(Unpacker u, BigInteger to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readBigInteger();
+ }
+
+ static public BigIntegerTemplate getInstance() {
+ return instance;
+ }
+
+ static final BigIntegerTemplate instance = new BigIntegerTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/BooleanArrayTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/BooleanArrayTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,66 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class BooleanArrayTemplate extends AbstractTemplate {
+ private BooleanArrayTemplate() {
+ }
+
+ public void write(Packer pk, boolean[] target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.writeArrayBegin(target.length);
+ for (boolean a : target) {
+ pk.write(a);
+ }
+ pk.writeArrayEnd();
+ }
+
+ public boolean[] read(Unpacker u, boolean[] to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readArrayBegin();
+ if (to == null || to.length != n) {
+ to = new boolean[n];
+ }
+ for (int i = 0; i < n; i++) {
+ to[i] = u.readBoolean();
+ }
+ u.readArrayEnd();
+ return to;
+ }
+
+ static public BooleanArrayTemplate getInstance() {
+ return instance;
+ }
+
+ static final BooleanArrayTemplate instance = new BooleanArrayTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/BooleanTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/BooleanTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,54 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class BooleanTemplate extends AbstractTemplate {
+ private BooleanTemplate() {
+ }
+
+ public void write(Packer pk, Boolean target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write((boolean) target);
+ }
+
+ public Boolean read(Unpacker u, Boolean to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readBoolean();
+ }
+
+ static public BooleanTemplate getInstance() {
+ return instance;
+ }
+
+ static final BooleanTemplate instance = new BooleanTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/ByteArrayTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/ByteArrayTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,54 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class ByteArrayTemplate extends AbstractTemplate {
+ private ByteArrayTemplate() {
+ }
+
+ public void write(Packer pk, byte[] target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write(target);
+ }
+
+ public byte[] read(Unpacker u, byte[] to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readByteArray(); // TODO read to 'to' obj
+ }
+
+ static public ByteArrayTemplate getInstance() {
+ return instance;
+ }
+
+ static final ByteArrayTemplate instance = new ByteArrayTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/ByteBufferTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/ByteBufferTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,56 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class ByteBufferTemplate extends AbstractTemplate {
+ private ByteBufferTemplate() {
+ }
+
+ public void write(Packer pk, ByteBuffer target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write(target);
+ }
+
+ public ByteBuffer read(Unpacker u, ByteBuffer to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readByteBuffer(); // TODO read to 'to' obj?
+ }
+
+ static public ByteBufferTemplate getInstance() {
+ return instance;
+ }
+
+ static final ByteBufferTemplate instance = new ByteBufferTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/ByteTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/ByteTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,53 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class ByteTemplate extends AbstractTemplate {
+ private ByteTemplate() {
+ }
+
+ public void write(Packer pk, Byte target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write((byte) target);
+ }
+
+ public Byte read(Unpacker u, Byte to, boolean required) throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readByte();
+ }
+
+ static public ByteTemplate getInstance() {
+ return instance;
+ }
+
+ static final ByteTemplate instance = new ByteTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/CharacterTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/CharacterTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,62 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.MessageTypeException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+
+/**
+ * CharacterTemplate
+ *
+ * @author watabiki
+ */
+public class CharacterTemplate extends AbstractTemplate {
+
+ private CharacterTemplate() {
+ }
+
+ @Override
+ public void write(Packer pk, Character target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write((int) (char) target);
+ }
+
+ @Override
+ public Character read(Unpacker u, Character to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return (char) u.readInt();
+ }
+
+ static public CharacterTemplate getInstance() {
+ return instance;
+ }
+
+ static final CharacterTemplate instance = new CharacterTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/CollectionTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/CollectionTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,69 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.LinkedList;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class CollectionTemplate extends AbstractTemplate> {
+ private Template elementTemplate;
+
+ public CollectionTemplate(Template elementTemplate) {
+ this.elementTemplate = elementTemplate;
+ }
+
+ public void write(Packer pk, Collection target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ Collection col = target;
+ pk.writeArrayBegin(col.size());
+ for (E e : col) {
+ elementTemplate.write(pk, e);
+ }
+ pk.writeArrayEnd();
+ }
+
+ public Collection read(Unpacker u, Collection to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readArrayBegin();
+ if (to == null) {
+ to = new LinkedList();
+ } else {
+ to.clear();
+ }
+ for (int i = 0; i < n; i++) {
+ E e = elementTemplate.read(u, null);
+ to.add(e);
+ }
+ u.readArrayEnd();
+ return to;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/DateTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/DateTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,56 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.util.Date;
+
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class DateTemplate extends AbstractTemplate {
+ private DateTemplate() {
+ }
+
+ public void write(Packer pk, Date target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write((long) target.getTime());
+ }
+
+ public Date read(Unpacker u, Date to, boolean required) throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ long temp = u.readLong();
+ return new Date(temp);
+ }
+
+ static public DateTemplate getInstance() {
+ return instance;
+ }
+
+ static final DateTemplate instance = new DateTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/DoubleArrayTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/DoubleArrayTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,66 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class DoubleArrayTemplate extends AbstractTemplate {
+ private DoubleArrayTemplate() {
+ }
+
+ public void write(Packer pk, double[] target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.writeArrayBegin(target.length);
+ for (double a : target) {
+ pk.write(a);
+ }
+ pk.writeArrayEnd();
+ }
+
+ public double[] read(Unpacker u, double[] to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readArrayBegin();
+ if (to == null || to.length != n) {
+ to = new double[n];
+ }
+ for (int i = 0; i < n; i++) {
+ to[i] = u.readDouble();
+ }
+ u.readArrayEnd();
+ return to;
+ }
+
+ static public DoubleArrayTemplate getInstance() {
+ return instance;
+ }
+
+ static final DoubleArrayTemplate instance = new DoubleArrayTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/DoubleTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/DoubleTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,54 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class DoubleTemplate extends AbstractTemplate {
+ private DoubleTemplate() {
+ }
+
+ public void write(Packer pk, Double target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write((double) target);
+ }
+
+ public Double read(Unpacker u, Double to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readDouble();
+ }
+
+ static public DoubleTemplate getInstance() {
+ return instance;
+ }
+
+ static final DoubleTemplate instance = new DoubleTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/FieldList.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/FieldList.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,83 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.util.List;
+import java.util.ArrayList;
+
+public class FieldList {
+ public static class Entry {
+ private String name;
+
+ private FieldOption option;
+
+ public Entry() {
+ this(null, FieldOption.IGNORE);
+ }
+
+ public Entry(final String name, final FieldOption option) {
+ this.name = name;
+ this.option = option;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public FieldOption getOption() {
+ return option;
+ }
+
+ public boolean isAvailable() {
+ return option != FieldOption.IGNORE;
+ }
+ }
+
+ private ArrayList list;
+
+ public FieldList() {
+ list = new ArrayList();
+ }
+
+ public void add(final String name) {
+ add(name, FieldOption.DEFAULT);
+ }
+
+ public void add(final String name, final FieldOption option) {
+ list.add(new Entry(name, option));
+ }
+
+ public void put(final int index, final String name) {
+ put(index, name, FieldOption.DEFAULT);
+ }
+
+ public void put(final int index, final String name, final FieldOption option) {
+ if (list.size() < index) {
+ do {
+ list.add(new Entry());
+ } while (list.size() < index);
+ list.add(new Entry(name, option));
+ } else {
+ list.set(index, new Entry(name, option));
+ }
+ }
+
+ public List getList() {
+ return list;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/FieldOption.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/FieldOption.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,22 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+public enum FieldOption {
+ IGNORE, OPTIONAL, NOTNULLABLE, DEFAULT;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/FloatArrayTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/FloatArrayTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,66 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class FloatArrayTemplate extends AbstractTemplate {
+ private FloatArrayTemplate() {
+ }
+
+ public void write(Packer pk, float[] target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.writeArrayBegin(target.length);
+ for (float a : target) {
+ pk.write(a);
+ }
+ pk.writeArrayEnd();
+ }
+
+ public float[] read(Unpacker u, float[] to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readArrayBegin();
+ if (to == null || to.length != n) {
+ to = new float[n];
+ }
+ for (int i = 0; i < n; i++) {
+ to[i] = u.readFloat();
+ }
+ u.readArrayEnd();
+ return to;
+ }
+
+ static public FloatArrayTemplate getInstance() {
+ return instance;
+ }
+
+ static final FloatArrayTemplate instance = new FloatArrayTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/FloatTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/FloatTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,54 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class FloatTemplate extends AbstractTemplate {
+ private FloatTemplate() {
+ }
+
+ public void write(Packer pk, Float target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write((float) target);
+ }
+
+ public Float read(Unpacker u, Float to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readFloat();
+ }
+
+ static public FloatTemplate getInstance() {
+ return instance;
+ }
+
+ static final FloatTemplate instance = new FloatTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/GenericCollectionTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/GenericCollectionTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,55 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+public class GenericCollectionTemplate implements GenericTemplate {
+ @SuppressWarnings("rawtypes")
+ Constructor extends Template> constructor;
+
+ @SuppressWarnings("rawtypes")
+ public GenericCollectionTemplate(TemplateRegistry registry, Class extends Template> tmpl) {
+ try {
+ constructor = tmpl.getConstructor(new Class>[] { Template.class });
+ constructor.newInstance(new Object[] { new AnyTemplate(registry) });
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException(e);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException(e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException(e);
+ } catch (InstantiationException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ public Template build(Template[] params) {
+ try {
+ return constructor.newInstance((Object[]) params);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException(e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException(e);
+ } catch (InstantiationException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/GenericMapTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/GenericMapTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,57 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
+public class GenericMapTemplate implements GenericTemplate {
+ @SuppressWarnings("rawtypes")
+ Constructor extends Template> constructor;
+
+ @SuppressWarnings("rawtypes")
+ public GenericMapTemplate(TemplateRegistry registry, Class extends Template> tmpl) {
+ try {
+ constructor = tmpl.getConstructor(new Class>[] { Template.class, Template.class });
+ constructor.newInstance(new Object[] { new AnyTemplate(registry), new AnyTemplate(registry) });
+ // AnyTemplate.getInstance(registry),
+ // AnyTemplate.getInstance(registry)});
+ } catch (NoSuchMethodException e) {
+ throw new IllegalArgumentException(e);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException(e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException(e);
+ } catch (InstantiationException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ @SuppressWarnings("rawtypes")
+ public Template build(Template[] params) {
+ try {
+ return constructor.newInstance((Object[]) params);
+ } catch (InvocationTargetException e) {
+ throw new IllegalArgumentException(e);
+ } catch (IllegalAccessException e) {
+ throw new IllegalArgumentException(e);
+ } catch (InstantiationException e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/GenericTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/GenericTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,23 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+public interface GenericTemplate {
+ @SuppressWarnings("rawtypes")
+ public Template build(Template[] params);
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/IntegerArrayTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/IntegerArrayTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,69 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class IntegerArrayTemplate extends AbstractTemplate {
+ private IntegerArrayTemplate() {
+ }
+
+ public void write(Packer pk, int[] target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.writeArrayBegin(target.length);
+ for (int a : target) {
+ pk.write(a);
+ }
+ pk.writeArrayEnd();
+ }
+
+ public int[] read(Unpacker u, int[] to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readArrayBegin();
+ int[] array;
+ if (to != null && to.length == n) {
+ array = to;
+ } else {
+ array = new int[n];
+ }
+ for (int i = 0; i < n; i++) {
+ array[i] = u.readInt();
+ }
+ u.readArrayEnd();
+ return array;
+ }
+
+ static public IntegerArrayTemplate getInstance() {
+ return instance;
+ }
+
+ static final IntegerArrayTemplate instance = new IntegerArrayTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/IntegerTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/IntegerTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,54 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class IntegerTemplate extends AbstractTemplate {
+ private IntegerTemplate() {
+ }
+
+ public void write(Packer pk, Integer target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write((int) target);
+ }
+
+ public Integer read(Unpacker u, Integer to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readInt();
+ }
+
+ static public IntegerTemplate getInstance() {
+ return instance;
+ }
+
+ static final IntegerTemplate instance = new IntegerTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/ListTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/ListTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,72 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class ListTemplate extends AbstractTemplate> {
+ private Template elementTemplate;
+
+ public ListTemplate(Template elementTemplate) {
+ this.elementTemplate = elementTemplate;
+ }
+
+ public void write(Packer pk, List target, boolean required)
+ throws IOException {
+ if (!(target instanceof List)) {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ throw new MessageTypeException("Target is not a List but "
+ + target.getClass());
+ }
+ pk.writeArrayBegin(target.size());
+ for (E e : target) {
+ elementTemplate.write(pk, e);
+ }
+ pk.writeArrayEnd();
+ }
+
+ public List read(Unpacker u, List to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readArrayBegin();
+ if (to == null) {
+ to = new ArrayList(n);
+ } else {
+ to.clear();
+ }
+ for (int i = 0; i < n; i++) {
+ E e = elementTemplate.read(u, null);
+ to.add(e);
+ }
+ u.readArrayEnd();
+ return to;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/LongArrayTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/LongArrayTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,66 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class LongArrayTemplate extends AbstractTemplate {
+ private LongArrayTemplate() {
+ }
+
+ public void write(Packer pk, long[] target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.writeArrayBegin(target.length);
+ for (long a : target) {
+ pk.write(a);
+ }
+ pk.writeArrayEnd();
+ }
+
+ public long[] read(Unpacker u, long[] to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readArrayBegin();
+ if (to == null || to.length != n) {
+ to = new long[n];
+ }
+ for (int i = 0; i < n; i++) {
+ to[i] = u.readLong();
+ }
+ u.readArrayEnd();
+ return to;
+ }
+
+ static public LongArrayTemplate getInstance() {
+ return instance;
+ }
+
+ static final LongArrayTemplate instance = new LongArrayTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/LongTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/LongTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,53 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class LongTemplate extends AbstractTemplate {
+ private LongTemplate() {
+ }
+
+ public void write(Packer pk, Long target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write((long) target);
+ }
+
+ public Long read(Unpacker u, Long to, boolean required) throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readLong();
+ }
+
+ static public LongTemplate getInstance() {
+ return instance;
+ }
+
+ static final LongTemplate instance = new LongTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/MapTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/MapTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,78 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.HashMap;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class MapTemplate extends AbstractTemplate> {
+ private Template keyTemplate;
+ private Template valueTemplate;
+
+ public MapTemplate(Template keyTemplate, Template valueTemplate) {
+ this.keyTemplate = keyTemplate;
+ this.valueTemplate = valueTemplate;
+ }
+
+ public void write(Packer pk, Map target, boolean required)
+ throws IOException {
+ if (!(target instanceof Map)) {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ throw new MessageTypeException("Target is not a Map but " + target.getClass());
+ }
+ Map map = (Map) target;
+ pk.writeMapBegin(map.size());
+ for (Map.Entry pair : map.entrySet()) {
+ keyTemplate.write(pk, pair.getKey());
+ valueTemplate.write(pk, pair.getValue());
+ }
+ pk.writeMapEnd();
+ }
+
+ public Map read(Unpacker u, Map to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readMapBegin();
+ Map map;
+ if (to != null) {
+ map = (Map) to;
+ map.clear();
+ } else {
+ map = new HashMap(n);
+ }
+ for (int i = 0; i < n; i++) {
+ K key = keyTemplate.read(u, null);
+ V value = valueTemplate.read(u, null);
+ map.put(key, value);
+ }
+ u.readMapEnd();
+ return map;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/MessagePackableTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/MessagePackableTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,62 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessagePackable;
+import org.msgpack.MessageTypeException;
+
+public class MessagePackableTemplate extends AbstractTemplate {
+ private Class> targetClass;
+
+ MessagePackableTemplate(Class> targetClass) {
+ this.targetClass = targetClass;
+ }
+
+ public void write(Packer pk, MessagePackable target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ target.writeTo(pk);
+ }
+
+ public MessagePackable read(Unpacker u, MessagePackable to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ if (to == null) {
+ try {
+ to = (MessagePackable) targetClass.newInstance();
+ } catch (InstantiationException e) {
+ throw new MessageTypeException(e);
+ } catch (IllegalAccessException e) {
+ throw new MessageTypeException(e);
+ }
+ }
+ to.readFrom(u);
+ return to;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/NotNullableTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/NotNullableTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,35 @@
+package org.msgpack.template;
+
+import java.io.IOException;
+
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+
+public class NotNullableTemplate extends AbstractTemplate {
+
+ private Template tmpl;
+
+ public NotNullableTemplate(Template elementTemplate) {
+ tmpl = elementTemplate;
+ }
+
+ @Override
+ public void write(Packer pk, T v, boolean required) throws IOException {
+ tmpl.write(pk, v, required);
+ }
+
+ @Override
+ public void write(Packer pk, T v) throws IOException {
+ write(pk, v, true);
+ }
+
+ @Override
+ public T read(Unpacker u, T to, boolean required) throws IOException {
+ return tmpl.read(u, to, required);
+ }
+
+ @Override
+ public T read(Unpacker u, T to) throws IOException {
+ return read(u, to, true);
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/ObjectArrayTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/ObjectArrayTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,60 @@
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.lang.reflect.Array;
+
+import org.msgpack.MessageTypeException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+
+@SuppressWarnings({ "rawtypes", "unchecked" })
+public class ObjectArrayTemplate extends AbstractTemplate {
+ protected Class componentClass;
+
+ protected Template componentTemplate;
+
+ public ObjectArrayTemplate(Class componentClass, Template componentTemplate) {
+ this.componentClass = componentClass;
+ this.componentTemplate = componentTemplate;
+ }
+
+ @Override
+ public void write(Packer packer, Object v, boolean required)
+ throws IOException {
+ if (v == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ packer.writeNil();
+ return;
+ }
+ if (!(v instanceof Object[]) ||
+ !componentClass.isAssignableFrom(v.getClass().getComponentType())) {
+ throw new MessageTypeException();
+ }
+
+ Object[] array = (Object[]) v;
+ int length = array.length;
+ packer.writeArrayBegin(length);
+ for (int i = 0; i < length; i++) {
+ componentTemplate.write(packer, array[i], required);
+ }
+ packer.writeArrayEnd();
+ }
+
+ @Override
+ public Object read(Unpacker unpacker, Object to, boolean required)
+ throws IOException {
+ if (!required && unpacker.trySkipNil()) {
+ return null;
+ }
+
+ int length = unpacker.readArrayBegin();
+ Object[] array = (Object[]) Array.newInstance(componentClass, length);
+ for (int i = 0; i < length; i++) {
+ array[i] = componentTemplate.read(unpacker, null, required);
+ }
+ unpacker.readArrayEnd();
+ return array;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/OrdinalEnumTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/OrdinalEnumTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,81 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.util.HashMap;
+
+import org.msgpack.MessageTypeException;
+import org.msgpack.annotation.OrdinalEnum;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+
+public class OrdinalEnumTemplate extends AbstractTemplate {
+ protected T[] entries;
+ protected HashMap reverse;
+ protected boolean strict;
+
+ public OrdinalEnumTemplate(Class targetClass) {
+ entries = targetClass.getEnumConstants();
+ reverse = new HashMap();
+ for (int i = 0; i < entries.length; i++) {
+ reverse.put(entries[i], i);
+ }
+ strict = !targetClass.isAnnotationPresent(OrdinalEnum.class)
+ || targetClass.getAnnotation(OrdinalEnum.class).strict();
+ }
+
+ @Override
+ public void write(Packer pk, T target, boolean required) throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ Integer ordinal = reverse.get(target);
+ if (ordinal == null) {
+ throw new MessageTypeException(
+ new IllegalArgumentException("ordinal: " + ordinal));
+ }
+ pk.write((int) ordinal);
+ }
+
+ @Override
+ public T read(Unpacker pac, T to, boolean required) throws IOException,
+ MessageTypeException {
+ if (!required && pac.trySkipNil()) {
+ return null;
+ }
+
+ int ordinal = pac.readInt();
+
+ if (ordinal < entries.length) {
+ return entries[ordinal];
+ }
+
+ if (!strict) {
+ return null;
+ }
+
+ throw new MessageTypeException(new IllegalArgumentException("ordinal: "
+ + ordinal));
+
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/SetTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/SetTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,73 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class SetTemplate extends AbstractTemplate> {
+ private Template elementTemplate;
+
+ public SetTemplate(Template elementTemplate) {
+ this.elementTemplate = elementTemplate;
+ }
+
+ public void write(Packer pk, Set target, boolean required)
+ throws IOException {
+ if (!(target instanceof Set)) {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ throw new MessageTypeException("Target is not a List but "
+ + target.getClass());
+ }
+ pk.writeArrayBegin(target.size());
+ for (E e : target) {
+ elementTemplate.write(pk, e);
+ }
+ pk.writeArrayEnd();
+ }
+
+ public Set read(Unpacker u, Set to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readArrayBegin();
+ if (to == null) {
+ to = new HashSet(n);
+ } else {
+ to.clear();
+ }
+ for (int i = 0; i < n; i++) {
+ E e = elementTemplate.read(u, null);
+ to.add(e);
+ }
+ u.readArrayEnd();
+ return to;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/ShortArrayTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/ShortArrayTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,66 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class ShortArrayTemplate extends AbstractTemplate {
+ private ShortArrayTemplate() {
+ }
+
+ public void write(Packer pk, short[] target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.writeArrayBegin(target.length);
+ for (short a : target) {
+ pk.write(a);
+ }
+ pk.writeArrayEnd();
+ }
+
+ public short[] read(Unpacker u, short[] to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ int n = u.readArrayBegin();
+ if (to == null || to.length != n) {
+ to = new short[n];
+ }
+ for (int i = 0; i < n; i++) {
+ to[i] = u.readShort();
+ }
+ u.readArrayEnd();
+ return to;
+ }
+
+ static public ShortArrayTemplate getInstance() {
+ return instance;
+ }
+
+ static final ShortArrayTemplate instance = new ShortArrayTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/ShortTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/ShortTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,54 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class ShortTemplate extends AbstractTemplate {
+ private ShortTemplate() {
+ }
+
+ public void write(Packer pk, Short target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write(target);
+ }
+
+ public Short read(Unpacker u, Short to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readShort();
+ }
+
+ static public ShortTemplate getInstance() {
+ return instance;
+ }
+
+ static final ShortTemplate instance = new ShortTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/StringTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/StringTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,54 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+
+public class StringTemplate extends AbstractTemplate {
+ private StringTemplate() {
+ }
+
+ public void write(Packer pk, String target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ pk.write(target);
+ }
+
+ public String read(Unpacker u, String to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readString();
+ }
+
+ static public StringTemplate getInstance() {
+ return instance;
+ }
+
+ static final StringTemplate instance = new StringTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/Template.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/Template.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,32 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+
+public interface Template {
+ public void write(Packer pk, T v) throws IOException;
+
+ public void write(Packer pk, T v, boolean required) throws IOException;
+
+ public T read(Unpacker u, T to) throws IOException;
+
+ public T read(Unpacker u, T to, boolean required) throws IOException;
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/TemplateReference.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/TemplateReference.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,74 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import java.lang.reflect.Type;
+
+import org.msgpack.MessageTypeException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+
+public class TemplateReference extends AbstractTemplate {
+
+ private TemplateRegistry registry;
+
+ private Type targetType;
+
+ private Template actualTemplate;
+
+ public TemplateReference(TemplateRegistry registry, Type targetType) {
+ this.registry = registry;
+ this.targetType = targetType;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void validateActualTemplate() {
+ if (actualTemplate == null) {
+ actualTemplate = (Template) registry.cache.get(targetType);
+ if (actualTemplate == null) {
+ throw new MessageTypeException(
+ "Actual template have not been created");
+ }
+ }
+ }
+
+ @Override
+ public void write(Packer pk, T v, boolean required) throws IOException {
+ validateActualTemplate();
+ actualTemplate.write(pk, v, required);
+ }
+
+ @Override
+ public void write(Packer pk, T v) throws IOException {
+ validateActualTemplate();
+ actualTemplate.write(pk, v, false);
+ }
+
+ @Override
+ public T read(Unpacker u, T to, boolean required) throws IOException {
+ validateActualTemplate();
+ return actualTemplate.read(u, to, required);
+ }
+
+ @Override
+ public T read(Unpacker u, T to) throws IOException {
+ validateActualTemplate();
+ return actualTemplate.read(u, to, false);
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/TemplateRegistry.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/TemplateRegistry.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,623 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+import java.lang.reflect.GenericArrayType;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+import java.lang.reflect.WildcardType;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+
+import org.msgpack.MessagePackable;
+import org.msgpack.MessageTypeException;
+import org.msgpack.template.BigIntegerTemplate;
+import org.msgpack.template.BooleanTemplate;
+import org.msgpack.template.ByteArrayTemplate;
+import org.msgpack.template.ByteTemplate;
+import org.msgpack.template.DoubleArrayTemplate;
+import org.msgpack.template.DoubleTemplate;
+import org.msgpack.template.FieldList;
+import org.msgpack.template.FloatArrayTemplate;
+import org.msgpack.template.FloatTemplate;
+import org.msgpack.template.GenericTemplate;
+import org.msgpack.template.IntegerArrayTemplate;
+import org.msgpack.template.IntegerTemplate;
+import org.msgpack.template.LongArrayTemplate;
+import org.msgpack.template.LongTemplate;
+import org.msgpack.template.ShortArrayTemplate;
+import org.msgpack.template.ShortTemplate;
+import org.msgpack.template.StringTemplate;
+import org.msgpack.template.Template;
+import org.msgpack.template.ValueTemplate;
+import org.msgpack.template.builder.ArrayTemplateBuilder;
+import org.msgpack.template.builder.TemplateBuilder;
+import org.msgpack.template.builder.TemplateBuilderChain;
+import org.msgpack.type.Value;
+
+@SuppressWarnings({ "rawtypes", "unchecked" })
+public class TemplateRegistry {
+
+ private TemplateRegistry parent = null;
+
+ private TemplateBuilderChain chain;
+
+ Map> cache;
+
+ private Map genericCache;
+
+ /**
+ * create TemplateRegistry
object of root.
+ */
+ private TemplateRegistry() {
+ parent = null;
+ chain = createTemplateBuilderChain();
+ genericCache = new HashMap();
+ cache = new HashMap>();
+ registerTemplates();
+ cache = Collections.unmodifiableMap(cache);
+ }
+
+ /**
+ *
+ * @param registry
+ */
+ public TemplateRegistry(TemplateRegistry registry) {
+ if (registry != null) {
+ parent = registry;
+ } else {
+ parent = new TemplateRegistry();
+ }
+ chain = createTemplateBuilderChain();
+ cache = new HashMap>();
+ genericCache = new HashMap();
+ registerTemplatesWhichRefersRegistry();
+ }
+
+ protected TemplateBuilderChain createTemplateBuilderChain() {
+ return new TemplateBuilderChain(this);
+ }
+
+ public void setClassLoader(final ClassLoader cl) {
+ chain = new TemplateBuilderChain(this, cl);
+ }
+
+ private void registerTemplates() {
+ register(boolean.class, BooleanTemplate.getInstance());
+ register(Boolean.class, BooleanTemplate.getInstance());
+ register(byte.class, ByteTemplate.getInstance());
+ register(Byte.class, ByteTemplate.getInstance());
+ register(short.class, ShortTemplate.getInstance());
+ register(Short.class, ShortTemplate.getInstance());
+ register(int.class, IntegerTemplate.getInstance());
+ register(Integer.class, IntegerTemplate.getInstance());
+ register(long.class, LongTemplate.getInstance());
+ register(Long.class, LongTemplate.getInstance());
+ register(float.class, FloatTemplate.getInstance());
+ register(Float.class, FloatTemplate.getInstance());
+ register(double.class, DoubleTemplate.getInstance());
+ register(Double.class, DoubleTemplate.getInstance());
+ register(BigInteger.class, BigIntegerTemplate.getInstance());
+ register(char.class, CharacterTemplate.getInstance());
+ register(Character.class, CharacterTemplate.getInstance());
+ register(boolean[].class, BooleanArrayTemplate.getInstance());
+ register(short[].class, ShortArrayTemplate.getInstance());
+ register(int[].class, IntegerArrayTemplate.getInstance());
+ register(long[].class, LongArrayTemplate.getInstance());
+ register(float[].class, FloatArrayTemplate.getInstance());
+ register(double[].class, DoubleArrayTemplate.getInstance());
+ register(String.class, StringTemplate.getInstance());
+ register(byte[].class, ByteArrayTemplate.getInstance());
+ register(ByteBuffer.class, ByteBufferTemplate.getInstance());
+ register(Value.class, ValueTemplate.getInstance());
+ register(BigDecimal.class, BigDecimalTemplate.getInstance());
+ register(Date.class, DateTemplate.getInstance());
+
+ registerTemplatesWhichRefersRegistry();
+
+ }
+
+ protected void registerTemplatesWhichRefersRegistry() {
+ AnyTemplate anyTemplate = new AnyTemplate(this);
+
+ register(List.class, new ListTemplate(anyTemplate));
+ register(Set.class, new SetTemplate(anyTemplate));
+ register(Collection.class, new CollectionTemplate(anyTemplate));
+ register(Map.class, new MapTemplate(anyTemplate, anyTemplate));
+ registerGeneric(List.class, new GenericCollectionTemplate(this, ListTemplate.class));
+ registerGeneric(Set.class, new GenericCollectionTemplate(this, SetTemplate.class));
+ registerGeneric(Collection.class, new GenericCollectionTemplate(this, CollectionTemplate.class));
+ registerGeneric(Map.class, new GenericMapTemplate(this, MapTemplate.class));
+ }
+
+ public void register(final Class> targetClass) {
+ buildAndRegister(null, targetClass, false, null);
+ }
+
+ public void register(final Class> targetClass, final FieldList flist) {
+ if (flist == null) {
+ throw new NullPointerException("FieldList object is null");
+ }
+
+ buildAndRegister(null, targetClass, false, flist);
+ }
+
+ public synchronized void register(final Type targetType, final Template tmpl) {
+ if (tmpl == null) {
+ throw new NullPointerException("Template object is null");
+ }
+
+ if (targetType instanceof ParameterizedType) {
+ cache.put(((ParameterizedType) targetType).getRawType(), tmpl);
+ } else {
+ cache.put(targetType, tmpl);
+ }
+ }
+
+ public synchronized void registerGeneric(final Type targetType, final GenericTemplate tmpl) {
+ if (targetType instanceof ParameterizedType) {
+ genericCache.put(((ParameterizedType) targetType).getRawType(),
+ tmpl);
+ } else {
+ genericCache.put(targetType, tmpl);
+ }
+ }
+
+ public synchronized boolean unregister(final Type targetType) {
+ Template tmpl = cache.remove(targetType);
+ return tmpl != null;
+ }
+
+ public synchronized void unregister() {
+ cache.clear();
+ }
+
+ public synchronized Template lookup(Type targetType) {
+ Template tmpl;
+
+ if (targetType instanceof ParameterizedType) {
+ // ParameterizedType is not a Class>
+ ParameterizedType paramedType = (ParameterizedType) targetType;
+ tmpl = lookupGenericType(paramedType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+ targetType = paramedType.getRawType();
+ }
+
+ tmpl = lookupGenericArrayType(targetType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ tmpl = lookupCache(targetType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ if (targetType instanceof WildcardType ||
+ targetType instanceof TypeVariable) {
+ // WildcardType is not a Class>
+ tmpl = new AnyTemplate(this);
+ register(targetType, tmpl);
+ return tmpl;
+ }
+
+ Class> targetClass = (Class>) targetType;
+
+ // MessagePackable interface is implemented
+ if (MessagePackable.class.isAssignableFrom(targetClass)) {
+ // FIXME #MN
+ // following processing should be merged into lookAfterBuilding
+ // or lookupInterfaceTypes method in next version
+ tmpl = new MessagePackableTemplate(targetClass);
+ register(targetClass, tmpl);
+ return tmpl;
+ }
+
+ if (targetClass.isInterface()) {
+ // writing interfaces will succeed
+ // reading into interfaces will fail
+ tmpl = new AnyTemplate(this);
+ register(targetType, tmpl);
+ return tmpl;
+ }
+
+ // find matched template builder and build template
+ tmpl = lookupAfterBuilding(targetClass);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ // lookup template of interface type
+ tmpl = lookupInterfaceTypes(targetClass);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ // lookup template of superclass type
+ tmpl = lookupSuperclasses(targetClass);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ // lookup template of interface type of superclasss
+ tmpl = lookupSuperclassInterfaceTypes(targetClass);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ throw new MessageTypeException(
+ "Cannot find template for " + targetClass + " class. " +
+ "Try to add @Message annotation to the class or call MessagePack.register(Type).");
+ }
+
+ private Template lookupGenericType(ParameterizedType paramedType) {
+ Template tmpl = lookupGenericTypeImpl(paramedType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ try {
+ tmpl = parent.lookupGenericTypeImpl(paramedType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+ } catch (NullPointerException e) { // ignore
+ }
+
+ tmpl = lookupGenericInterfaceTypes(paramedType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ tmpl = lookupGenericSuperclasses(paramedType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ return null;
+ }
+
+ private Template lookupGenericTypeImpl(ParameterizedType targetType) {
+ Type rawType = targetType.getRawType();
+ return lookupGenericTypeImpl0(targetType, rawType);
+ }
+
+ private Template lookupGenericTypeImpl0(ParameterizedType targetType, Type rawType) {
+ GenericTemplate gtmpl = genericCache.get(rawType);
+ if (gtmpl == null) {
+ return null;
+ }
+
+ Type[] types = targetType.getActualTypeArguments();
+ Template[] tmpls = new Template[types.length];
+ for (int i = 0; i < types.length; ++i) {
+ tmpls[i] = lookup(types[i]);
+ }
+
+ return gtmpl.build(tmpls);
+ }
+
+ private Template lookupGenericInterfaceTypes(ParameterizedType targetType) {
+ Type rawType = targetType.getRawType();
+ Template tmpl = null;
+
+ try {
+ Class>[] infTypes = ((Class) rawType).getInterfaces();
+ for (Class> infType : infTypes) {
+ tmpl = lookupGenericTypeImpl0(targetType, infType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+ }
+ } catch (ClassCastException e) { // ignore
+ }
+
+ return tmpl;
+ }
+
+ private Template lookupGenericSuperclasses(ParameterizedType targetType) {
+ Type rawType = targetType.getRawType();
+ Template tmpl = null;
+
+ try {
+ Class> superClass = ((Class) rawType).getSuperclass();
+ if (superClass == null) {
+ return null;
+ }
+
+ for (; superClass != Object.class; superClass = superClass.getSuperclass()) {
+ tmpl = lookupGenericTypeImpl0(targetType, superClass);
+ if (tmpl != null) {
+ register(targetType, tmpl);
+ return tmpl;
+ }
+ }
+ } catch (ClassCastException e) { // ignore
+ }
+
+ return tmpl;
+ }
+
+ private Template lookupGenericArrayType(Type targetType) {
+ // TODO GenericArrayType is not a Class> => buildArrayTemplate
+ if (! (targetType instanceof GenericArrayType)) {
+ return null;
+ }
+
+ GenericArrayType genericArrayType = (GenericArrayType) targetType;
+ Template tmpl = lookupGenericArrayTypeImpl(genericArrayType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ try {
+ tmpl = parent.lookupGenericArrayTypeImpl(genericArrayType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+ } catch (NullPointerException e) { // ignore
+ }
+
+ return null;
+ }
+
+ private Template lookupGenericArrayTypeImpl(GenericArrayType genericArrayType) {
+ String genericArrayTypeName = "" + genericArrayType;
+ int dim = genericArrayTypeName.split("\\[").length - 1;
+ if (dim <= 0) {
+ throw new MessageTypeException(
+ String.format("fatal error: type=", genericArrayTypeName));
+ } else if (dim > 1) {
+ throw new UnsupportedOperationException(String.format(
+ "Not implemented template generation of %s", genericArrayTypeName));
+ }
+
+ String genericCompTypeName = "" + genericArrayType.getGenericComponentType();
+ boolean isPrimitiveType = isPrimitiveType(genericCompTypeName);
+ StringBuffer sbuf = new StringBuffer();
+ for (int i = 0; i < dim; i++) {
+ sbuf.append('[');
+ }
+ if (!isPrimitiveType) {
+ sbuf.append('L');
+ sbuf.append(toJvmReferenceTypeName(genericCompTypeName));
+ sbuf.append(';');
+ } else {
+ sbuf.append(toJvmPrimitiveTypeName(genericCompTypeName));
+ }
+
+ String jvmArrayClassName = sbuf.toString();
+ Class jvmArrayClass = null;
+ ClassLoader cl = null;
+ try {
+ cl = Thread.currentThread().getContextClassLoader();
+ if (cl != null) {
+ jvmArrayClass = cl.loadClass(jvmArrayClassName);
+ if (jvmArrayClass != null) {
+ return lookupAfterBuilding(jvmArrayClass);
+ }
+ }
+ } catch (ClassNotFoundException e) {} // ignore
+
+ try {
+ cl = getClass().getClassLoader();
+ if (cl != null) {
+ jvmArrayClass = cl.loadClass(jvmArrayClassName);
+ if (jvmArrayClass != null) {
+ return lookupAfterBuilding(jvmArrayClass);
+ }
+ }
+ } catch (ClassNotFoundException e) {} // ignore
+
+ try {
+ jvmArrayClass = Class.forName(jvmArrayClassName);
+ if (jvmArrayClass != null) {
+ return lookupAfterBuilding(jvmArrayClass);
+ }
+ } catch (ClassNotFoundException e) {} // ignore
+
+ throw new MessageTypeException(String.format(
+ "cannot find template of %s", jvmArrayClassName));
+ }
+
+ private Template lookupCache(Type targetType) {
+ Template tmpl = cache.get(targetType);
+ if (tmpl != null) {
+ return tmpl;
+ }
+
+ try {
+ tmpl = parent.lookupCache(targetType);
+ } catch (NullPointerException e) { // ignore
+ }
+ return tmpl;
+ }
+
+ private Template lookupAfterBuilding(Class targetClass) {
+ TemplateBuilder builder = chain.select(targetClass, true);
+ Template tmpl = null;
+ if (builder != null) {
+ // TODO #MN for Android, we should modify here
+ tmpl = chain.getForceBuilder().loadTemplate(targetClass);
+ if (tmpl != null) {
+ register(targetClass, tmpl);
+ return tmpl;
+ }
+ tmpl = buildAndRegister(builder, targetClass, true, null);
+ }
+ return tmpl;
+ }
+
+ private Template lookupInterfaceTypes(Class targetClass) {
+ Class>[] infTypes = targetClass.getInterfaces();
+ Template tmpl = null;
+ for (Class> infType : infTypes) {
+ tmpl = (Template) cache.get(infType);
+ if (tmpl != null) {
+ register(targetClass, tmpl);
+ return tmpl;
+ } else {
+ try {
+ tmpl = (Template) parent.lookupCache(infType);
+ if (tmpl != null) {
+ register(targetClass, tmpl);
+ return tmpl;
+ }
+ } catch (NullPointerException e) { // ignore
+ }
+ }
+ }
+ return tmpl;
+ }
+
+ private Template lookupSuperclasses(Class targetClass) {
+ Class> superClass = targetClass.getSuperclass();
+ Template tmpl = null;
+ if (superClass != null) {
+ for (; superClass != Object.class; superClass = superClass
+ .getSuperclass()) {
+ tmpl = (Template) cache.get(superClass);
+ if (tmpl != null) {
+ register(targetClass, tmpl);
+ return tmpl;
+ } else {
+ try {
+ tmpl = (Template) parent.lookupCache(superClass);
+ if (tmpl != null) {
+ register(targetClass, tmpl);
+ return tmpl;
+ }
+ } catch (NullPointerException e) { // ignore
+ }
+ }
+ }
+ }
+ return tmpl;
+ }
+
+ private Template lookupSuperclassInterfaceTypes(Class targetClass) {
+ Class> superClass = targetClass.getSuperclass();
+ Template tmpl = null;
+ if (superClass != null) {
+ for (; superClass != Object.class; superClass = superClass.getSuperclass()) {
+ tmpl = (Template) lookupInterfaceTypes(superClass);
+ if (tmpl != null) {
+ register(targetClass, tmpl);
+ return tmpl;
+ } else {
+ try {
+ tmpl = (Template) parent.lookupCache(superClass);
+ if (tmpl != null) {
+ register(targetClass, tmpl);
+ return tmpl;
+ }
+ } catch (NullPointerException e) { // ignore
+ }
+ }
+ }
+ }
+ return tmpl;
+ }
+
+ private synchronized Template buildAndRegister(TemplateBuilder builder,
+ final Class targetClass, final boolean hasAnnotation,
+ final FieldList flist) {
+ Template newTmpl = null;
+ Template oldTmpl = null;
+ try {
+ if (cache.containsKey(targetClass)) {
+ oldTmpl = cache.get(targetClass);
+ }
+ newTmpl = new TemplateReference(this, targetClass);
+ cache.put(targetClass, newTmpl);
+ if (builder == null) {
+ builder = chain.select(targetClass, hasAnnotation);
+ }
+ newTmpl = flist != null ?
+ builder.buildTemplate(targetClass, flist) : builder.buildTemplate(targetClass);
+ return newTmpl;
+ } catch (Exception e) {
+ if (oldTmpl != null) {
+ cache.put(targetClass, oldTmpl);
+ } else {
+ cache.remove(targetClass);
+ }
+ newTmpl = null;
+ if (e instanceof MessageTypeException) {
+ throw (MessageTypeException) e;
+ } else {
+ throw new MessageTypeException(e);
+ }
+ } finally {
+ if (newTmpl != null) {
+ cache.put(targetClass, newTmpl);
+ }
+ }
+ }
+
+ private static boolean isPrimitiveType(String genericCompTypeName) {
+ return (genericCompTypeName.equals("byte")
+ || genericCompTypeName.equals("short")
+ || genericCompTypeName.equals("int")
+ || genericCompTypeName.equals("long")
+ || genericCompTypeName.equals("float")
+ || genericCompTypeName.equals("double")
+ || genericCompTypeName.equals("boolean")
+ || genericCompTypeName.equals("char"));
+ }
+
+ private static String toJvmReferenceTypeName(String typeName) {
+ // delete "class " from class name
+ // e.g. "class Foo" to "Foo" by this method
+ return typeName.substring(6);
+ }
+
+ private static String toJvmPrimitiveTypeName(String typeName) {
+ if (typeName.equals("byte")) {
+ return "B";
+ } else if (typeName.equals("short")) {
+ return "S";
+ } else if (typeName.equals("int")) {
+ return "I";
+ } else if (typeName.equals("long")) {
+ return "J";
+ } else if (typeName.equals("float")) {
+ return "F";
+ } else if (typeName.equals("double")) {
+ return "D";
+ } else if (typeName.equals("boolean")) {
+ return "Z";
+ } else if (typeName.equals("char")) {
+ return "C";
+ } else {
+ throw new MessageTypeException(String.format(
+ "fatal error: type=%s", typeName));
+ }
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/Templates.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/Templates.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,154 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.Map;
+import java.util.Collection;
+import java.util.Date;
+import java.math.BigInteger;
+import java.math.BigDecimal;
+import org.msgpack.type.Value;
+
+@SuppressWarnings({ "rawtypes", "unchecked" })
+public final class Templates {
+ public static final Template TValue = ValueTemplate.getInstance();
+
+ public static final Template TByte = ByteTemplate.getInstance();
+
+ public static final Template TShort = ShortTemplate.getInstance();
+
+ public static final Template TInteger = IntegerTemplate.getInstance();
+
+ public static final Template TLong = LongTemplate.getInstance();
+
+ public static final Template TCharacter = CharacterTemplate.getInstance();
+
+ public static final Template TBigInteger = BigIntegerTemplate.getInstance();
+
+ public static final Template TBigDecimal = BigDecimalTemplate.getInstance();
+
+ public static final Template TFloat = FloatTemplate.getInstance();
+
+ public static final Template TDouble = DoubleTemplate.getInstance();
+
+ public static final Template TBoolean = BooleanTemplate.getInstance();
+
+ public static final Template TString = StringTemplate.getInstance();
+
+ public static final Template TByteArray = ByteArrayTemplate.getInstance();
+
+ public static final Template TByteBuffer = ByteBufferTemplate.getInstance();
+
+ public static final Template TDate = DateTemplate.getInstance();
+
+ public static Template tNotNullable(Template innerTemplate) {
+ return new NotNullableTemplate(innerTemplate);
+ }
+
+ public static Template> tList(Template elementTemplate) {
+ return new ListTemplate(elementTemplate);
+ }
+
+ public static Template> tMap(Template keyTemplate, Template valueTemplate) {
+ return new MapTemplate(keyTemplate, valueTemplate);
+ }
+
+ public static Template> tCollection(Template elementTemplate) {
+ return new CollectionTemplate(elementTemplate);
+ }
+
+ public static Template tOrdinalEnum(Class enumClass) {
+ return new OrdinalEnumTemplate(enumClass);
+ }
+
+ // public static Template tClass(Class target) {
+ // // TODO
+ // }
+
+ @Deprecated
+ public static Template tByte() {
+ return TByte;
+ }
+
+ @Deprecated
+ public static Template tShort() {
+ return TShort;
+ }
+
+ @Deprecated
+ public static Template tInteger() {
+ return TInteger;
+ }
+
+ @Deprecated
+ public static Template tLong() {
+ return TLong;
+ }
+
+ @Deprecated
+ public static Template tCharacter() {
+ return TCharacter;
+ }
+
+ @Deprecated
+ public static Template tBigInteger() {
+ return TBigInteger;
+ }
+
+ @Deprecated
+ public static Template tBigDecimal() {
+ return TBigDecimal;
+ }
+
+ @Deprecated
+ public static Template tFloat() {
+ return TFloat;
+ }
+
+ @Deprecated
+ public static Template tDouble() {
+ return TDouble;
+ }
+
+ @Deprecated
+ public static Template tBoolean() {
+ return TBoolean;
+ }
+
+ @Deprecated
+ public static Template tString() {
+ return TString;
+ }
+
+ @Deprecated
+ public static Template tByteArray() {
+ return TByteArray;
+ }
+
+ @Deprecated
+ public static Template tByteBuffer() {
+ return TByteBuffer;
+ }
+
+ @Deprecated
+ public static Template tDate() {
+ return TDate;
+ }
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/ValueTemplate.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/ValueTemplate.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,55 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template;
+
+import java.io.IOException;
+import org.msgpack.packer.Packer;
+import org.msgpack.unpacker.Unpacker;
+import org.msgpack.MessageTypeException;
+import org.msgpack.type.Value;
+
+public class ValueTemplate extends AbstractTemplate {
+ private ValueTemplate() {
+ }
+
+ public void write(Packer pk, Value target, boolean required)
+ throws IOException {
+ if (target == null) {
+ if (required) {
+ throw new MessageTypeException("Attempted to write null");
+ }
+ pk.writeNil();
+ return;
+ }
+ target.writeTo(pk);
+ }
+
+ public Value read(Unpacker u, Value to, boolean required)
+ throws IOException {
+ if (!required && u.trySkipNil()) {
+ return null;
+ }
+ return u.readValue();
+ }
+
+ static public ValueTemplate getInstance() {
+ return instance;
+ }
+
+ static final ValueTemplate instance = new ValueTemplate();
+}
diff -r 000000000000 -r cb825acd883a src/main/java/org/msgpack/template/builder/AbstractTemplateBuilder.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/org/msgpack/template/builder/AbstractTemplateBuilder.java Sat Oct 18 15:06:15 2014 +0900
@@ -0,0 +1,293 @@
+//
+// MessagePack for Java
+//
+// Copyright (C) 2009 - 2013 FURUHASHI Sadayuki
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+package org.msgpack.template.builder;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+import java.lang.reflect.GenericArrayType;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.msgpack.annotation.Beans;
+import org.msgpack.annotation.Ignore;
+import org.msgpack.annotation.Index;
+import org.msgpack.annotation.Message;
+import org.msgpack.annotation.MessagePackBeans;
+import org.msgpack.annotation.MessagePackMessage;
+import org.msgpack.annotation.MessagePackOrdinalEnum;
+import org.msgpack.annotation.NotNullable;
+import org.msgpack.annotation.Optional;
+import org.msgpack.annotation.OrdinalEnum;
+import org.msgpack.template.FieldList;
+import org.msgpack.template.FieldOption;
+import org.msgpack.template.Template;
+import org.msgpack.template.TemplateRegistry;
+import org.msgpack.template.builder.TemplateBuildException;
+
+public abstract class AbstractTemplateBuilder implements TemplateBuilder {
+
+ protected TemplateRegistry registry;
+
+ protected AbstractTemplateBuilder(TemplateRegistry registry) {
+ this.registry = registry;
+ }
+
+ @Override
+ public Template buildTemplate(final Type targetType)
+ throws TemplateBuildException {
+ @SuppressWarnings("unchecked")
+ Class targetClass = (Class) targetType;
+ checkClassValidation(targetClass);
+ FieldOption fieldOption = getFieldOption(targetClass);
+ FieldEntry[] entries = toFieldEntries(targetClass, fieldOption);
+ return buildTemplate(targetClass, entries);
+ }
+
+ @Override
+ public Template buildTemplate(final Class