diff --git a/msgpack-core/src/main/java/org/msgpack/core/MessageBufferPacker.java b/msgpack-core/src/main/java/org/msgpack/core/MessageBufferPacker.java index 02b94f773..640b35f9d 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessageBufferPacker.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessageBufferPacker.java @@ -15,31 +15,27 @@ // package org.msgpack.core; +import org.msgpack.core.buffer.ArrayBufferOutput; import org.msgpack.core.buffer.MessageBuffer; import org.msgpack.core.buffer.MessageBufferOutput; -import org.msgpack.core.buffer.ArrayBufferOutput; import java.io.IOException; import java.util.List; +/** + * MessagePacker that is useful to produce byte array output + */ public class MessageBufferPacker extends MessagePacker { - public MessageBufferPacker() - { - this(new ArrayBufferOutput()); - } - - public MessageBufferPacker(ArrayBufferOutput out) + public MessageBufferPacker(MessagePack.PackerConfig config) { - super(out); + this(new ArrayBufferOutput(), config); } - @Override - public MessageBufferPacker setSmallStringOptimizationThreshold(int bytes) + public MessageBufferPacker(ArrayBufferOutput out, MessagePack.PackerConfig config) { - super.setSmallStringOptimizationThreshold(bytes); - return this; + super(out, config); } public MessageBufferOutput reset(MessageBufferOutput out) @@ -51,23 +47,28 @@ public MessageBufferOutput reset(MessageBufferOutput out) return super.reset(out); } + private ArrayBufferOutput getArrayBufferOut() + { + return (ArrayBufferOutput) out; + } + public void clear() { - ((ArrayBufferOutput) out).clear(); + getArrayBufferOut().clear(); } public byte[] toByteArray() { - return ((ArrayBufferOutput) out).toByteArray(); + return getArrayBufferOut().toByteArray(); } public MessageBuffer toMessageBuffer() { - return ((ArrayBufferOutput) out).toMessageBuffer(); + return getArrayBufferOut().toMessageBuffer(); } public List toBufferList() { - return ((ArrayBufferOutput) out).toBufferList(); + return getArrayBufferOut().toBufferList(); } } diff --git a/msgpack-core/src/main/java/org/msgpack/core/MessageFormat.java b/msgpack-core/src/main/java/org/msgpack/core/MessageFormat.java index 5a20518dc..d57c446f2 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessageFormat.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessageFormat.java @@ -17,6 +17,7 @@ import org.msgpack.core.annotations.VisibleForTesting; import org.msgpack.value.ValueType; +import org.msgpack.core.MessagePack.Code; /** * Describes the list of the message format types defined in the MessagePack specification. @@ -65,94 +66,6 @@ public enum MessageFormat MAP32(ValueType.MAP), NEGFIXINT(ValueType.INTEGER); - /** - * The prefix code set of MessagePack. See also https://github.com/msgpack/msgpack/blob/master/spec.md for details. - */ - public static final class Code - { - public static final boolean isFixInt(byte b) - { - int v = b & 0xFF; - return v <= 0x7f || v >= 0xe0; - } - - public static final boolean isPosFixInt(byte b) - { - return (b & POSFIXINT_MASK) == 0; - } - - public static final boolean isNegFixInt(byte b) - { - return (b & NEGFIXINT_PREFIX) == NEGFIXINT_PREFIX; - } - - public static final boolean isFixStr(byte b) - { - return (b & (byte) 0xe0) == Code.FIXSTR_PREFIX; - } - - public static final boolean isFixedArray(byte b) - { - return (b & (byte) 0xf0) == Code.FIXARRAY_PREFIX; - } - - public static final boolean isFixedMap(byte b) - { - return (b & (byte) 0xf0) == Code.FIXMAP_PREFIX; - } - - public static final boolean isFixedRaw(byte b) - { - return (b & (byte) 0xe0) == Code.FIXSTR_PREFIX; - } - - public static final byte POSFIXINT_MASK = (byte) 0x80; - - public static final byte FIXMAP_PREFIX = (byte) 0x80; - public static final byte FIXARRAY_PREFIX = (byte) 0x90; - public static final byte FIXSTR_PREFIX = (byte) 0xa0; - - public static final byte NIL = (byte) 0xc0; - public static final byte NEVER_USED = (byte) 0xc1; - public static final byte FALSE = (byte) 0xc2; - public static final byte TRUE = (byte) 0xc3; - public static final byte BIN8 = (byte) 0xc4; - public static final byte BIN16 = (byte) 0xc5; - public static final byte BIN32 = (byte) 0xc6; - public static final byte EXT8 = (byte) 0xc7; - public static final byte EXT16 = (byte) 0xc8; - public static final byte EXT32 = (byte) 0xc9; - public static final byte FLOAT32 = (byte) 0xca; - public static final byte FLOAT64 = (byte) 0xcb; - public static final byte UINT8 = (byte) 0xcc; - public static final byte UINT16 = (byte) 0xcd; - public static final byte UINT32 = (byte) 0xce; - public static final byte UINT64 = (byte) 0xcf; - - public static final byte INT8 = (byte) 0xd0; - public static final byte INT16 = (byte) 0xd1; - public static final byte INT32 = (byte) 0xd2; - public static final byte INT64 = (byte) 0xd3; - - public static final byte FIXEXT1 = (byte) 0xd4; - public static final byte FIXEXT2 = (byte) 0xd5; - public static final byte FIXEXT4 = (byte) 0xd6; - public static final byte FIXEXT8 = (byte) 0xd7; - public static final byte FIXEXT16 = (byte) 0xd8; - - public static final byte STR8 = (byte) 0xd9; - public static final byte STR16 = (byte) 0xda; - public static final byte STR32 = (byte) 0xdb; - - public static final byte ARRAY16 = (byte) 0xdc; - public static final byte ARRAY32 = (byte) 0xdd; - - public static final byte MAP16 = (byte) 0xde; - public static final byte MAP32 = (byte) 0xdf; - - public static final byte NEGFIXINT_PREFIX = (byte) 0xe0; - } - private static final MessageFormat[] formatTable = new MessageFormat[256]; private final ValueType valueType; diff --git a/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java b/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java index f1bee0774..59bbbad48 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java @@ -19,6 +19,8 @@ import org.msgpack.core.buffer.ChannelBufferInput; import org.msgpack.core.buffer.ChannelBufferOutput; import org.msgpack.core.buffer.InputStreamBufferInput; +import org.msgpack.core.buffer.MessageBufferInput; +import org.msgpack.core.buffer.MessageBufferOutput; import org.msgpack.core.buffer.OutputStreamBufferOutput; import java.io.InputStream; @@ -28,8 +30,6 @@ import java.nio.charset.Charset; import java.nio.charset.CodingErrorAction; -import static org.msgpack.core.Preconditions.checkArgument; - /** * This class has MessagePack prefix code definitions and packer/unpacker factory methods. */ @@ -37,92 +37,188 @@ public class MessagePack { public static final Charset UTF8 = Charset.forName("UTF-8"); - private static MessagePackFactory defaultFactory = new MessagePackFactory(); - /** - * Sets the default configuration used for the static constructor methods of this MessagePack class. + * The prefix code set of MessagePack. See also https://github.com/msgpack/msgpack/blob/master/spec.md for details. */ - public static void setDefaultFactory(MessagePackFactory newDefaultFactory) + public static final class Code { - defaultFactory = newDefaultFactory; + public static final boolean isFixInt(byte b) + { + int v = b & 0xFF; + return v <= 0x7f || v >= 0xe0; + } + + public static final boolean isPosFixInt(byte b) + { + return (b & POSFIXINT_MASK) == 0; + } + + public static final boolean isNegFixInt(byte b) + { + return (b & NEGFIXINT_PREFIX) == NEGFIXINT_PREFIX; + } + + public static final boolean isFixStr(byte b) + { + return (b & (byte) 0xe0) == Code.FIXSTR_PREFIX; + } + + public static final boolean isFixedArray(byte b) + { + return (b & (byte) 0xf0) == Code.FIXARRAY_PREFIX; + } + + public static final boolean isFixedMap(byte b) + { + return (b & (byte) 0xf0) == Code.FIXMAP_PREFIX; + } + + public static final boolean isFixedRaw(byte b) + { + return (b & (byte) 0xe0) == Code.FIXSTR_PREFIX; + } + + public static final byte POSFIXINT_MASK = (byte) 0x80; + + public static final byte FIXMAP_PREFIX = (byte) 0x80; + public static final byte FIXARRAY_PREFIX = (byte) 0x90; + public static final byte FIXSTR_PREFIX = (byte) 0xa0; + + public static final byte NIL = (byte) 0xc0; + public static final byte NEVER_USED = (byte) 0xc1; + public static final byte FALSE = (byte) 0xc2; + public static final byte TRUE = (byte) 0xc3; + public static final byte BIN8 = (byte) 0xc4; + public static final byte BIN16 = (byte) 0xc5; + public static final byte BIN32 = (byte) 0xc6; + public static final byte EXT8 = (byte) 0xc7; + public static final byte EXT16 = (byte) 0xc8; + public static final byte EXT32 = (byte) 0xc9; + public static final byte FLOAT32 = (byte) 0xca; + public static final byte FLOAT64 = (byte) 0xcb; + public static final byte UINT8 = (byte) 0xcc; + public static final byte UINT16 = (byte) 0xcd; + public static final byte UINT32 = (byte) 0xce; + public static final byte UINT64 = (byte) 0xcf; + + public static final byte INT8 = (byte) 0xd0; + public static final byte INT16 = (byte) 0xd1; + public static final byte INT32 = (byte) 0xd2; + public static final byte INT64 = (byte) 0xd3; + + public static final byte FIXEXT1 = (byte) 0xd4; + public static final byte FIXEXT2 = (byte) 0xd5; + public static final byte FIXEXT4 = (byte) 0xd6; + public static final byte FIXEXT8 = (byte) 0xd7; + public static final byte FIXEXT16 = (byte) 0xd8; + + public static final byte STR8 = (byte) 0xd9; + public static final byte STR16 = (byte) 0xda; + public static final byte STR32 = (byte) 0xdb; + + public static final byte ARRAY16 = (byte) 0xdc; + public static final byte ARRAY32 = (byte) 0xdd; + + public static final byte MAP16 = (byte) 0xde; + public static final byte MAP32 = (byte) 0xdf; + + public static final byte NEGFIXINT_PREFIX = (byte) 0xe0; } - public static MessagePackFactory getDefaultFactory() + private MessagePack() { - return defaultFactory; + // Prohibit instantiation of this class } - private MessagePack() - { } + /** + * Create a packer that outputs the packed data to the specified output + * + * @param out + * @return + */ + public static MessagePacker newDefaultPacker(MessageBufferOutput out) + { + return new PackerConfig().newPacker(out); + } /** - * Equivalent to getDefaultFactory().newPacker(out). + * Create a packer that outputs the packed data to a target output stream * * @param out * @return */ public static MessagePacker newDefaultPacker(OutputStream out) { - return defaultFactory.newPacker(out); + return new PackerConfig().newPacker(out); } /** - * Equivalent to getDefaultFactory().newPacker(channel). + * Create a packer that outputs the packed data to a channel * * @param channel * @return */ public static MessagePacker newDefaultPacker(WritableByteChannel channel) { - return defaultFactory.newPacker(channel); + return new PackerConfig().newPacker(channel); } /** - * Equivalent to getDefaultFactory().newBufferPacker() + * Create a packer for storing packed data into a byte array * - * @param channel * @return */ public static MessageBufferPacker newDefaultBufferPacker() { - return defaultFactory.newBufferPacker(); + return new PackerConfig().newBufferPacker(); + } + + /** + * Create an unpacker that reads the data from a given input + * + * @param in + * @return + */ + public static MessageUnpacker newDefaultUnpacker(MessageBufferInput in) + { + return new UnpackerConfig().newUnpacker(in); } /** - * Equivalent to getDefaultFactory().newUnpacker(in). + * Create an unpacker that reads the data from a given input stream * * @param in * @return */ public static MessageUnpacker newDefaultUnpacker(InputStream in) { - return defaultFactory.newUnpacker(in); + return new UnpackerConfig().newUnpacker(in); } /** - * Equivalent to getDefaultFactory().newUnpacker(channel). + * Create an unpacker that reads the data from a given channel * * @param channel * @return */ public static MessageUnpacker newDefaultUnpacker(ReadableByteChannel channel) { - return defaultFactory.newUnpacker(channel); + return new UnpackerConfig().newUnpacker(channel); } /** - * Equivalent to getDefaultFactory().newUnpacker(contents). + * Create an unpacker that reads the data from a given byte array * * @param contents * @return */ public static MessageUnpacker newDefaultUnpacker(byte[] contents) { - return defaultFactory.newUnpacker(contents); + return new UnpackerConfig().newUnpacker(contents); } /** - * Equivalent to getDefaultFactory().newUnpacker(contents, offset, length). + * Create an unpacker that reads the data from a given byte array [offset, offset+length) * * @param contents * @param offset @@ -131,6 +227,158 @@ public static MessageUnpacker newDefaultUnpacker(byte[] contents) */ public static MessageUnpacker newDefaultUnpacker(byte[] contents, int offset, int length) { - return defaultFactory.newUnpacker(contents, offset, length); + return new UnpackerConfig().newUnpacker(contents, offset, length); + } + + /** + * MessagePacker configuration. + */ + public static class PackerConfig + { + /** + * Use String.getBytes() for converting Java Strings that are smaller than this threshold into UTF8. + * Note that this parameter is subject to change. + */ + public int smallStringOptimizationThreshold = 512; + + /** + * When the next payload size exceeds this threshold, MessagePacker will call MessageBufferOutput.flush() before + * packing the data. + */ + public int bufferFlushThreshold = 8192; + + /** + * Create a packer that outputs the packed data to a given output + * + * @param out + * @return + */ + public MessagePacker newPacker(MessageBufferOutput out) + { + return new MessagePacker(out, this); + } + + /** + * Create a packer that outputs the packed data to a given output stream + * + * @param out + * @return + */ + public MessagePacker newPacker(OutputStream out) + { + return newPacker(new OutputStreamBufferOutput(out)); + } + + /** + * Create a packer that outputs the packed data to a given output channel + * + * @param channel + * @return + */ + public MessagePacker newPacker(WritableByteChannel channel) + { + return newPacker(new ChannelBufferOutput(channel)); + } + + /** + * Create a packer for storing packed data into a byte array + * + * @return + */ + public MessageBufferPacker newBufferPacker() + { + return new MessageBufferPacker(this); + } + } + + /** + * MessageUnpacker configuration. + */ + public static class UnpackerConfig + { + /** + * Allow unpackBinaryHeader to read str format family (default:true) + */ + public boolean allowReadingStringAsBinary = true; + + /** + * Allow unpackRawStringHeader and unpackString to read bin format family (default: true) + */ + public boolean allowReadingBinaryAsString = true; + + /** + * Action when encountered a malformed input + */ + public CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE; + + /** + * Action when an unmappable character is found + */ + public CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE; + + /** + * unpackString size limit. (default: Integer.MAX_VALUE) + */ + public int stringSizeLimit = Integer.MAX_VALUE; + + /** + * + */ + public int stringDecoderBufferSize = 8192; + + /** + * Create an unpacker that reads the data from a given input + * + * @param in + * @return + */ + public MessageUnpacker newUnpacker(MessageBufferInput in) + { + return new MessageUnpacker(in, this); + } + + /** + * Create an unpacker that reads the data from a given input stream + * + * @param in + * @return + */ + public MessageUnpacker newUnpacker(InputStream in) + { + return newUnpacker(new InputStreamBufferInput(in)); + } + + /** + * Create an unpacker that reads the data from a given channel + * + * @param channel + * @return + */ + public MessageUnpacker newUnpacker(ReadableByteChannel channel) + { + return newUnpacker(new ChannelBufferInput(channel)); + } + + /** + * Create an unpacker that reads the data from a given byte array + * + * @param contents + * @return + */ + public MessageUnpacker newUnpacker(byte[] contents) + { + return newUnpacker(new ArrayBufferInput(contents)); + } + + /** + * Create an unpacker that reads the data from a given byte array [offset, offset+size) + * + * @param contents + * @return + */ + public MessageUnpacker newUnpacker(byte[] contents, int offset, int length) + { + return newUnpacker(new ArrayBufferInput(contents, offset, length)); + } } } diff --git a/msgpack-core/src/main/java/org/msgpack/core/MessagePackFactory.java b/msgpack-core/src/main/java/org/msgpack/core/MessagePackFactory.java deleted file mode 100644 index f76df5490..000000000 --- a/msgpack-core/src/main/java/org/msgpack/core/MessagePackFactory.java +++ /dev/null @@ -1,203 +0,0 @@ -// -// MessagePack for Java -// -// 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.core; - -import org.msgpack.core.buffer.ArrayBufferInput; -import org.msgpack.core.buffer.ChannelBufferInput; -import org.msgpack.core.buffer.ChannelBufferOutput; -import org.msgpack.core.buffer.InputStreamBufferInput; -import org.msgpack.core.buffer.OutputStreamBufferOutput; -import org.msgpack.core.buffer.MessageBufferInput; -import org.msgpack.core.buffer.MessageBufferOutput; - -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.channels.WritableByteChannel; -import java.nio.channels.ReadableByteChannel; - -import java.nio.charset.CodingErrorAction; -import static org.msgpack.core.Preconditions.checkArgument; - -public class MessagePackFactory -{ - private int packerSmallStringOptimizationThreshold = 512; - - private boolean unpackAllowStringAsBinary = true; - private boolean unpackAllowBinaryAsString = true; - private CodingErrorAction unpackActionOnMalformedString = CodingErrorAction.REPLACE; - private CodingErrorAction unpackActionOnUnmappableString = CodingErrorAction.REPLACE; - private int unpackStringSizeLimit = Integer.MAX_VALUE; - private int unpackStringDecoderBufferSize = 8192; - - private int inputBufferSize = 16*1024; - private int outputBufferSize = 16*1024; - - public MessagePacker newPacker(OutputStream out) - { - return newPacker(new OutputStreamBufferOutput(out)); - } - - public MessagePacker newPacker(WritableByteChannel channel) - { - return newPacker(new ChannelBufferOutput(channel)); - } - - public MessagePacker newPacker(MessageBufferOutput output) - { - return new MessagePacker(output) - .setSmallStringOptimizationThreshold(packerSmallStringOptimizationThreshold); - } - - public MessageBufferPacker newBufferPacker() - { - return new MessageBufferPacker() - .setSmallStringOptimizationThreshold(packerSmallStringOptimizationThreshold); - } - - public MessageUnpacker newUnpacker(byte[] contents) - { - return newUnpacker(contents, 0, contents.length); - } - - public MessageUnpacker newUnpacker(byte[] contents, int offset, int length) - { - return newUnpacker(new ArrayBufferInput(contents, offset, length)); - } - - public MessageUnpacker newUnpacker(InputStream in) - { - return newUnpacker(new InputStreamBufferInput(in)); - } - - public MessageUnpacker newUnpacker(ReadableByteChannel channel) - { - return newUnpacker(new ChannelBufferInput(channel)); - } - - public MessageUnpacker newUnpacker(MessageBufferInput input) - { - return new MessageUnpacker(input) - .setAllowStringAsBinary(unpackAllowStringAsBinary) - .setAllowBinaryAsString(unpackAllowBinaryAsString) - .setActionOnMalformedString(unpackActionOnMalformedString) - .setActionOnUnmappableString(unpackActionOnUnmappableString) - .setStringSizeLimit(unpackStringSizeLimit) - .setStringDecoderBufferSize(unpackStringDecoderBufferSize); - } - - /** - * Use String.getBytes() for strings smaller than this threshold. - * Note that this parameter is subject to change. - */ - public MessagePackFactory packerSmallStringOptimizationThreshold(int bytes) - { - this.packerSmallStringOptimizationThreshold = bytes; - return this; - } - - public MessagePackFactory unpackAllowStringAsBinary(boolean enabled) - { - this.unpackAllowStringAsBinary = enabled; - return this; - } - - public MessagePackFactory unpackAllowBinaryAsString(boolean enabled) - { - this.unpackAllowBinaryAsString = enabled; - return this; - } - - public MessagePackFactory unpackActionOnMalformedString(CodingErrorAction action) - { - this.unpackActionOnMalformedString = action; - return this; - } - - public MessagePackFactory unpackActionOnUnmappableString(CodingErrorAction action) - { - this.unpackActionOnUnmappableString = action; - return this; - } - - public MessagePackFactory unpackStringSizeLimit(int bytes) - { - this.unpackStringSizeLimit = bytes; - return this; - } - - public MessagePackFactory unpackStringDecoderBufferSize(int bytes) - { - this.unpackStringDecoderBufferSize = bytes; - return this; - } - - public MessagePackFactory inputBufferSize(int bytes) - { - this.inputBufferSize = bytes; - return this; - } - - public MessagePackFactory outputBufferSize(int bytes) - { - this.inputBufferSize = bytes; - return this; - } - - private int getPackerSmallStringOptimizationThreshold() - { - return packerSmallStringOptimizationThreshold; - } - - private boolean getUnpackAllowStringAsBinary() - { - return unpackAllowStringAsBinary; - } - - private boolean getUnpackAllowBinaryAsString() - { - return unpackAllowBinaryAsString; - } - - private CodingErrorAction getUnpackActionOnMalformedString() - { - return unpackActionOnMalformedString; - } - - private CodingErrorAction getUnpackActionOnUnmappableString() - { - return unpackActionOnUnmappableString; - } - - private int getUnpackStringSizeLimit() - { - return unpackStringSizeLimit; - } - - private int getUnpackStringDecoderBufferSize() - { - return unpackStringDecoderBufferSize; - } - - private int getInputBufferSize() - { - return inputBufferSize; - } - - private int getOutputBufferSize() - { - return outputBufferSize; - } -} diff --git a/msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java b/msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java index b3fcff5a8..c99653fc9 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java @@ -27,42 +27,41 @@ import java.nio.charset.CharacterCodingException; import java.nio.charset.CharsetEncoder; import java.nio.charset.CoderResult; -import java.nio.charset.CodingErrorAction; - -import static org.msgpack.core.MessageFormat.Code.ARRAY16; -import static org.msgpack.core.MessageFormat.Code.ARRAY32; -import static org.msgpack.core.MessageFormat.Code.BIN16; -import static org.msgpack.core.MessageFormat.Code.BIN32; -import static org.msgpack.core.MessageFormat.Code.BIN8; -import static org.msgpack.core.MessageFormat.Code.EXT16; -import static org.msgpack.core.MessageFormat.Code.EXT32; -import static org.msgpack.core.MessageFormat.Code.EXT8; -import static org.msgpack.core.MessageFormat.Code.FALSE; -import static org.msgpack.core.MessageFormat.Code.FIXARRAY_PREFIX; -import static org.msgpack.core.MessageFormat.Code.FIXEXT1; -import static org.msgpack.core.MessageFormat.Code.FIXEXT16; -import static org.msgpack.core.MessageFormat.Code.FIXEXT2; -import static org.msgpack.core.MessageFormat.Code.FIXEXT4; -import static org.msgpack.core.MessageFormat.Code.FIXEXT8; -import static org.msgpack.core.MessageFormat.Code.FIXMAP_PREFIX; -import static org.msgpack.core.MessageFormat.Code.FIXSTR_PREFIX; -import static org.msgpack.core.MessageFormat.Code.FLOAT32; -import static org.msgpack.core.MessageFormat.Code.FLOAT64; -import static org.msgpack.core.MessageFormat.Code.INT16; -import static org.msgpack.core.MessageFormat.Code.INT32; -import static org.msgpack.core.MessageFormat.Code.INT64; -import static org.msgpack.core.MessageFormat.Code.INT8; -import static org.msgpack.core.MessageFormat.Code.MAP16; -import static org.msgpack.core.MessageFormat.Code.MAP32; -import static org.msgpack.core.MessageFormat.Code.NIL; -import static org.msgpack.core.MessageFormat.Code.STR16; -import static org.msgpack.core.MessageFormat.Code.STR32; -import static org.msgpack.core.MessageFormat.Code.STR8; -import static org.msgpack.core.MessageFormat.Code.TRUE; -import static org.msgpack.core.MessageFormat.Code.UINT16; -import static org.msgpack.core.MessageFormat.Code.UINT32; -import static org.msgpack.core.MessageFormat.Code.UINT64; -import static org.msgpack.core.MessageFormat.Code.UINT8; + +import static org.msgpack.core.MessagePack.Code.ARRAY16; +import static org.msgpack.core.MessagePack.Code.ARRAY32; +import static org.msgpack.core.MessagePack.Code.BIN16; +import static org.msgpack.core.MessagePack.Code.BIN32; +import static org.msgpack.core.MessagePack.Code.BIN8; +import static org.msgpack.core.MessagePack.Code.EXT16; +import static org.msgpack.core.MessagePack.Code.EXT32; +import static org.msgpack.core.MessagePack.Code.EXT8; +import static org.msgpack.core.MessagePack.Code.FALSE; +import static org.msgpack.core.MessagePack.Code.FIXARRAY_PREFIX; +import static org.msgpack.core.MessagePack.Code.FIXEXT1; +import static org.msgpack.core.MessagePack.Code.FIXEXT16; +import static org.msgpack.core.MessagePack.Code.FIXEXT2; +import static org.msgpack.core.MessagePack.Code.FIXEXT4; +import static org.msgpack.core.MessagePack.Code.FIXEXT8; +import static org.msgpack.core.MessagePack.Code.FIXMAP_PREFIX; +import static org.msgpack.core.MessagePack.Code.FIXSTR_PREFIX; +import static org.msgpack.core.MessagePack.Code.FLOAT32; +import static org.msgpack.core.MessagePack.Code.FLOAT64; +import static org.msgpack.core.MessagePack.Code.INT16; +import static org.msgpack.core.MessagePack.Code.INT32; +import static org.msgpack.core.MessagePack.Code.INT64; +import static org.msgpack.core.MessagePack.Code.INT8; +import static org.msgpack.core.MessagePack.Code.MAP16; +import static org.msgpack.core.MessagePack.Code.MAP32; +import static org.msgpack.core.MessagePack.Code.NIL; +import static org.msgpack.core.MessagePack.Code.STR16; +import static org.msgpack.core.MessagePack.Code.STR32; +import static org.msgpack.core.MessagePack.Code.STR8; +import static org.msgpack.core.MessagePack.Code.TRUE; +import static org.msgpack.core.MessagePack.Code.UINT16; +import static org.msgpack.core.MessagePack.Code.UINT32; +import static org.msgpack.core.MessagePack.Code.UINT64; +import static org.msgpack.core.MessagePack.Code.UINT8; import static org.msgpack.core.Preconditions.checkNotNull; /** @@ -85,7 +84,9 @@ public class MessagePacker implements Closeable { - private int smallStringOptimizationThreshold = 512; + private final int smallStringOptimizationThreshold; + + private final int bufferFlushThreshold; protected MessageBufferOutput out; @@ -109,19 +110,16 @@ public class MessagePacker * @param out MessageBufferOutput. Use {@link org.msgpack.core.buffer.OutputStreamBufferOutput}, {@link org.msgpack.core.buffer.ChannelBufferOutput} or * your own implementation of {@link org.msgpack.core.buffer.MessageBufferOutput} interface. */ - public MessagePacker(MessageBufferOutput out) + public MessagePacker(MessageBufferOutput out, MessagePack.PackerConfig config) { this.out = checkNotNull(out, "MessageBufferOutput is null"); + // We must copy the configuration parameters here since the config object is mutable + this.smallStringOptimizationThreshold = config.smallStringOptimizationThreshold; + this.bufferFlushThreshold = config.bufferFlushThreshold; this.position = 0; this.totalFlushBytes = 0; } - public MessagePacker setSmallStringOptimizationThreshold(int bytes) - { - this.smallStringOptimizationThreshold = bytes; - return this; - } - /** * Reset output. This method doesn't close the old resource. * @@ -154,10 +152,7 @@ public void flush() throws IOException { if (position > 0) { - out.writeBuffer(position); - buffer = null; - totalFlushBytes += position; - position = 0; + flushBuffer(); } out.flush(); } @@ -173,18 +168,24 @@ public void close() } } - private void ensureCapacity(int mimimumSize) + private void flushBuffer() + throws IOException + { + out.writeBuffer(position); + buffer = null; + totalFlushBytes += position; + position = 0; + } + + private void ensureCapacity(int minimumSize) throws IOException { if (buffer == null) { - buffer = out.next(mimimumSize); + buffer = out.next(minimumSize); } - else if (position + mimimumSize >= buffer.size()) { - out.writeBuffer(position); - buffer = null; - totalFlushBytes += position; - position = 0; - buffer = out.next(mimimumSize); + else if (position + minimumSize >= buffer.size()) { + flushBuffer(); + buffer = out.next(minimumSize); } } @@ -430,10 +431,12 @@ public MessagePacker packDouble(double v) return this; } - private void packStringByGetBytes(String s) + private void packStringWithGetBytes(String s) throws IOException { + // JVM performs various optimizations (memory allocation, reusing encoder etc.) when String.getBytes is used byte[] bytes = s.getBytes(MessagePack.UTF8); + // Write the length and payload of small string to the buffer so that it avoids an extra flush of buffer packRawStringHeader(bytes.length); addPayload(bytes); } @@ -483,8 +486,8 @@ public MessagePacker packString(String s) return this; } else if (s.length() < smallStringOptimizationThreshold) { - // Write the length and payload of small string to the buffer so that it avoids an extra flush of buffer - packStringByGetBytes(s); + // Using String.getBytes is generally faster for small strings + packStringWithGetBytes(s); return this; } else if (s.length() < (1 << 8)) { @@ -506,7 +509,7 @@ else if (s.length() < (1 << 8)) { // move 1 byte backward to expand 3-byte header region to 3 bytes buffer.putBytes(position + 3, buffer.array(), buffer.arrayOffset() + position + 2, written); - // write 3-byte header header + // write 3-byte header buffer.putByte(position++, STR16); buffer.putShort(position, (short) written); position += 2; @@ -516,7 +519,7 @@ else if (s.length() < (1 << 8)) { } } else if (s.length() < (1 << 16)) { - // ensure capacity for 3-byte raw string header + the maximum string size (+ 2 bytes for falback code) + // ensure capacity for 3-byte raw string header + the maximum string size (+ 2 bytes for fallback code) ensureCapacity(3 + s.length() * UTF_8_MAX_CHAR_SIZE + 2); // keep 3-byte header region and write raw string int written = encodeStringToBufferAt(position + 3, s); @@ -551,7 +554,7 @@ else if (s.length() < (1 << 16)) { // 384KB, which is OK size to keep in memory. // fallback - packStringByGetBytes(s); + packStringWithGetBytes(s); return this; } @@ -703,7 +706,7 @@ public MessagePacker writePayload(byte[] src) public MessagePacker writePayload(byte[] src, int off, int len) throws IOException { - if (buffer.size() - position < len || len > 8192) { + if (buffer.size() - position < len || len > bufferFlushThreshold) { flush(); // call flush before write out.write(src, off, len); totalFlushBytes += len; @@ -744,7 +747,7 @@ public MessagePacker addPayload(byte[] src) public MessagePacker addPayload(byte[] src, int off, int len) throws IOException { - if (buffer.size() - position < len || len > 8192) { + if (buffer.size() - position < len || len > bufferFlushThreshold) { flush(); // call flush before add out.add(src, off, len); totalFlushBytes += len; diff --git a/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java b/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java index ab7b3e496..a7cf970eb 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java @@ -15,7 +15,7 @@ // package org.msgpack.core; -import org.msgpack.core.MessageFormat.Code; +import org.msgpack.core.MessagePack.Code; import org.msgpack.core.buffer.MessageBuffer; import org.msgpack.core.buffer.MessageBufferInput; import org.msgpack.value.ImmutableValue; @@ -24,7 +24,6 @@ import org.msgpack.value.Variable; import java.io.Closeable; -import java.io.EOFException; import java.io.IOException; import java.math.BigInteger; import java.nio.ByteBuffer; @@ -33,13 +32,11 @@ import java.nio.charset.CharsetDecoder; import java.nio.charset.CoderResult; import java.nio.charset.CodingErrorAction; -import java.nio.charset.MalformedInputException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import static org.msgpack.core.Preconditions.checkArgument; import static org.msgpack.core.Preconditions.checkNotNull; /** @@ -74,19 +71,15 @@ public class MessageUnpacker { private static final MessageBuffer EMPTY_BUFFER = MessageBuffer.wrap(new byte[0]); - private static final byte HEAD_BYTE_REQUIRED = (byte) 0xc1; - - private boolean allowStringAsBinary = true; - private boolean allowBinaryAsString = true; - private CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE; - private CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE; - private int stringSizeLimit = Integer.MAX_VALUE; - private int stringDecoderBufferSize = 8192; + private final boolean allowReadingStringAsBinary; + private final boolean allowReadingBinaryAsString; + private final CodingErrorAction actionOnMalformedString; + private final CodingErrorAction actionOnUnmappableString; + private final int stringSizeLimit; + private final int stringDecoderBufferSize; private MessageBufferInput in; - private byte headByte = HEAD_BYTE_REQUIRED; - /** * Points to the current buffer to read */ @@ -103,15 +96,15 @@ public class MessageUnpacker private long totalReadBytes; /** - * Extra buffer for fixed-length data at the buffer boundary. + * An extra buffer for reading a small number value across the input buffer boundary. * At most 8-byte buffer (for readLong used by uint 64 and UTF-8 character decoding) is required. */ - private final MessageBuffer castBuffer = MessageBuffer.allocate(8); + private final MessageBuffer numberBuffer = MessageBuffer.allocate(8); /** - * Variable by ensureHeader method. Caller of the method should use this variable to read from returned MessageBuffer. + * After calling prepareNumberBuffer(), the caller should use this variable to read from the returned MessageBuffer. */ - private int readCastBufferPosition; + private int nextReadPosition; /** * For decoding String in unpackString. @@ -133,45 +126,16 @@ public class MessageUnpacker * * @param in */ - public MessageUnpacker(MessageBufferInput in) + public MessageUnpacker(MessageBufferInput in, MessagePack.UnpackerConfig config) { this.in = checkNotNull(in, "MessageBufferInput is null"); - } - - public MessageUnpacker setAllowStringAsBinary(boolean enabled) - { - this.allowStringAsBinary = enabled; - return this; - } - - public MessageUnpacker setAllowBinaryAsString(boolean enabled) - { - this.allowBinaryAsString = enabled; - return this; - } - - public MessageUnpacker setActionOnMalformedString(CodingErrorAction action) - { - this.actionOnMalformedString = action; - return this; - } - - public MessageUnpacker setActionOnUnmappableString(CodingErrorAction action) - { - this.actionOnUnmappableString = action; - return this; - } - - public MessageUnpacker setStringSizeLimit(int bytes) - { - this.stringSizeLimit = bytes; - return this; - } - - public MessageUnpacker setStringDecoderBufferSize(int bytes) - { - this.stringDecoderBufferSize = bytes; - return this; + // We need to copy the configuration parameters since the config object is mutable + this.allowReadingStringAsBinary = config.allowReadingStringAsBinary; + this.allowReadingBinaryAsString = config.allowReadingBinaryAsString; + this.actionOnMalformedString = config.actionOnMalformedString; + this.actionOnUnmappableString = config.actionOnUnmappableString; + this.stringSizeLimit = config.stringSizeLimit; + this.stringDecoderBufferSize = config.stringDecoderBufferSize; } /** @@ -201,52 +165,73 @@ public long getTotalReadBytes() return totalReadBytes + position; } - private void nextBuffer() + /** + * Get the next buffer without changing the position + * + * @return + * @throws IOException + */ + private MessageBuffer getNextBuffer() throws IOException { MessageBuffer next = in.next(); if (next == null) { throw new MessageInsufficientBufferException(); } + assert (buffer != null); totalReadBytes += buffer.size(); - buffer = next; + return next; + } + + private void nextBuffer() + throws IOException + { + buffer = getNextBuffer(); position = 0; } - private MessageBuffer readCastBuffer(int length) + /** + * Returns a short size buffer (upto 8 bytes) to read a number value + * + * @param readLength + * @return + * @throws IOException + * @throws MessageInsufficientBufferException If no more buffer can be acquired from the input source for reading the specified data length + */ + private MessageBuffer prepareNumberBuffer(int readLength) throws IOException { int remaining = buffer.size() - position; - if (remaining >= length) { - readCastBufferPosition = position; - position += length; // here assumes following buffer.getXxx never throws exception - return buffer; + if (remaining >= readLength) { + // When the data is contained inside the default buffer + nextReadPosition = position; + position += readLength; // here assumes following buffer.getXxx never throws exception + return buffer; // Return the default buffer } else { - // TODO loop this method until castBuffer is filled - MessageBuffer next = in.next(); - if (next == null) { - throw new MessageInsufficientBufferException(); - } + // When the default buffer doesn't contain the whole length - totalReadBytes += buffer.size(); + // TODO loop this method until castBuffer is filled + MessageBuffer next = getNextBuffer(); if (remaining > 0) { - // TODO this doesn't work if MessageBuffer is allocated by newDirectBuffer. - // add copy method to MessageBuffer to solve this issue. - castBuffer.putBytes(0, buffer.array(), buffer.arrayOffset() + position, remaining); - castBuffer.putBytes(remaining, next.array(), next.arrayOffset(), length - remaining); + // TODO This doesn't work if MessageBuffer is allocated by newDirectBuffer. + // Add copy method to MessageBuffer to solve this issue. + + // Copy the data fragment from the current buffer + numberBuffer.putBytes(0, buffer.array(), buffer.arrayOffset() + position, remaining); + numberBuffer.putBytes(remaining, next.array(), next.arrayOffset(), readLength - remaining); buffer = next; - position = length - remaining; - readCastBufferPosition = 0; + position = readLength - remaining; + nextReadPosition = 0; - return castBuffer; + return numberBuffer; // Return the numberBuffer } else { buffer = next; - position = length; - readCastBufferPosition = 0; + position = readLength; + nextReadPosition = 0; return buffer; } } @@ -328,36 +313,36 @@ private byte readByte() private short readShort() throws IOException { - MessageBuffer castBuffer = readCastBuffer(2); - return castBuffer.getShort(readCastBufferPosition); + MessageBuffer numberBuffer = prepareNumberBuffer(2); + return numberBuffer.getShort(nextReadPosition); } private int readInt() throws IOException { - MessageBuffer castBuffer = readCastBuffer(4); - return castBuffer.getInt(readCastBufferPosition); + MessageBuffer numberBuffer = prepareNumberBuffer(4); + return numberBuffer.getInt(nextReadPosition); } private long readLong() throws IOException { - MessageBuffer castBuffer = readCastBuffer(8); - return castBuffer.getLong(readCastBufferPosition); + MessageBuffer numberBuffer = prepareNumberBuffer(8); + return numberBuffer.getLong(nextReadPosition); } private float readFloat() throws IOException { - MessageBuffer castBuffer = readCastBuffer(4); - return castBuffer.getFloat(readCastBufferPosition); + MessageBuffer numberBuffer = prepareNumberBuffer(4); + return numberBuffer.getFloat(nextReadPosition); } private double readDouble() throws IOException { - MessageBuffer castBuffer = readCastBuffer(8); - return castBuffer.getDouble(readCastBufferPosition); + MessageBuffer numberBuffer = prepareNumberBuffer(8); + return numberBuffer.getDouble(nextReadPosition); } /** @@ -549,7 +534,7 @@ public Variable unpackValue(Variable var) MessageFormat mf = getNextFormat(); switch (mf.getValueType()) { case NIL: - unpackNil(); + readByte(); var.setNilValue(); return var; case BOOLEAN: @@ -581,9 +566,6 @@ public Variable unpackValue(Variable var) int size = unpackArrayHeader(); List list = new ArrayList(size); for (int i = 0; i < size; i++) { - //Variable e = new Variable(); - //unpackValue(e); - //list.add(e); list.add(unpackValue()); } var.setArrayValue(list); @@ -593,10 +575,6 @@ public Variable unpackValue(Variable var) int size = unpackMapHeader(); Map map = new HashMap(); for (int i = 0; i < size; i++) { - //Variable k = new Variable(); - //unpackValue(k); - //Variable v = new Variable(); - //unpackValue(v); Value k = unpackValue(); Value v = unpackValue(); map.put(k, v); @@ -924,7 +902,12 @@ private void resetDecoder() else { decoder.reset(); } - decodeStringBuffer = new StringBuilder(); + if (decodeStringBuffer == null) { + decodeStringBuffer = new StringBuilder(); + } + else { + decodeStringBuffer.setLength(0); + } } public String unpackString() @@ -949,7 +932,6 @@ public String unpackString() int bufferRemaining = buffer.size() - position; if (bufferRemaining >= rawRemaining) { decodeStringBuffer.append(decodeStringFastPath(rawRemaining)); - rawRemaining = 0; break; } else if (bufferRemaining == 0) { @@ -1019,7 +1001,7 @@ else if (bufferRemaining == 0) { } private void handleCoderError(CoderResult cr) - throws CharacterCodingException + throws CharacterCodingException { if ((cr.isMalformed() && actionOnMalformedString == CodingErrorAction.REPORT) || (cr.isUnmappable() && actionOnUnmappableString == CodingErrorAction.REPORT)) { @@ -1118,27 +1100,27 @@ public ExtensionTypeHeader unpackExtensionTypeHeader() return new ExtensionTypeHeader(type, 16); } case Code.EXT8: { - MessageBuffer castBuffer = readCastBuffer(2); - int u8 = castBuffer.getByte(readCastBufferPosition); + MessageBuffer numberBuffer = prepareNumberBuffer(2); + int u8 = numberBuffer.getByte(nextReadPosition); int length = u8 & 0xff; - byte type = castBuffer.getByte(readCastBufferPosition + 1); + byte type = numberBuffer.getByte(nextReadPosition + 1); return new ExtensionTypeHeader(type, length); } case Code.EXT16: { - MessageBuffer castBuffer = readCastBuffer(3); - int u16 = castBuffer.getShort(readCastBufferPosition); + MessageBuffer numberBuffer = prepareNumberBuffer(3); + int u16 = numberBuffer.getShort(nextReadPosition); int length = u16 & 0xffff; - byte type = castBuffer.getByte(readCastBufferPosition + 2); + byte type = numberBuffer.getByte(nextReadPosition + 2); return new ExtensionTypeHeader(type, length); } case Code.EXT32: { - MessageBuffer castBuffer = readCastBuffer(5); - int u32 = castBuffer.getInt(readCastBufferPosition); + MessageBuffer numberBuffer = prepareNumberBuffer(5); + int u32 = numberBuffer.getInt(nextReadPosition); if (u32 < 0) { throw overflowU32Size(u32); } int length = u32; - byte type = castBuffer.getByte(readCastBufferPosition + 4); + byte type = numberBuffer.getByte(nextReadPosition + 4); return new ExtensionTypeHeader(type, length); } } @@ -1188,7 +1170,7 @@ public int unpackRawStringHeader() return len; } - if (allowBinaryAsString) { + if (allowReadingBinaryAsString) { len = tryReadBinaryHeader(b); if (len >= 0) { return len; @@ -1209,7 +1191,7 @@ public int unpackBinaryHeader() return len; } - if (allowStringAsBinary) { + if (allowReadingStringAsBinary) { len = tryReadStringHeader(b); if (len >= 0) { return len; diff --git a/msgpack-core/src/main/java/org/msgpack/core/Preconditions.java b/msgpack-core/src/main/java/org/msgpack/core/Preconditions.java index d8f43bcb7..e44d97d15 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/Preconditions.java +++ b/msgpack-core/src/main/java/org/msgpack/core/Preconditions.java @@ -31,6 +31,7 @@ */ package org.msgpack.core; +import org.msgpack.core.annotations.Nullable; import org.msgpack.core.annotations.VisibleForTesting; /** diff --git a/msgpack-core/src/main/java/org/msgpack/core/Nullable.java b/msgpack-core/src/main/java/org/msgpack/core/annotations/Nullable.java similarity index 91% rename from msgpack-core/src/main/java/org/msgpack/core/Nullable.java rename to msgpack-core/src/main/java/org/msgpack/core/annotations/Nullable.java index b2e7585a5..9e7c94fab 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/Nullable.java +++ b/msgpack-core/src/main/java/org/msgpack/core/annotations/Nullable.java @@ -13,11 +13,11 @@ // See the License for the specific language governing permissions and // limitations under the License. // -package org.msgpack.core; +package org.msgpack.core.annotations; /** * Annotates a field which can be null */ -@interface Nullable +public @interface Nullable { } diff --git a/msgpack-core/src/main/java/org/msgpack/core/buffer/ArrayBufferInput.java b/msgpack-core/src/main/java/org/msgpack/core/buffer/ArrayBufferInput.java index a777b8a73..36f0ad3c1 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/buffer/ArrayBufferInput.java +++ b/msgpack-core/src/main/java/org/msgpack/core/buffer/ArrayBufferInput.java @@ -17,7 +17,6 @@ import java.io.IOException; -import static org.msgpack.core.Preconditions.checkArgument; import static org.msgpack.core.Preconditions.checkNotNull; /** @@ -41,8 +40,7 @@ public ArrayBufferInput(byte[] arr) public ArrayBufferInput(byte[] arr, int offset, int length) { - checkArgument(offset + length <= arr.length); - this.buffer = MessageBuffer.wrap(checkNotNull(arr, "input array is null")).slice(offset, length); + this(MessageBuffer.wrap(checkNotNull(arr, "input array is null")).slice(offset, length)); } /** @@ -87,5 +85,4 @@ public void close() buffer = null; isRead = false; } - } diff --git a/msgpack-core/src/main/java/org/msgpack/core/buffer/ChannelBufferInput.java b/msgpack-core/src/main/java/org/msgpack/core/buffer/ChannelBufferInput.java index 781b7afb9..f00cb0c30 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/buffer/ChannelBufferInput.java +++ b/msgpack-core/src/main/java/org/msgpack/core/buffer/ChannelBufferInput.java @@ -29,7 +29,7 @@ public class ChannelBufferInput implements MessageBufferInput { private ReadableByteChannel channel; - private final MessageBuffer m; + private final MessageBuffer buffer; public ChannelBufferInput(ReadableByteChannel channel) { @@ -40,7 +40,7 @@ public ChannelBufferInput(ReadableByteChannel channel, int bufferSize) { this.channel = checkNotNull(channel, "input channel is null"); checkArgument(bufferSize > 0, "buffer size must be > 0: " + bufferSize); - this.m = MessageBuffer.allocate(bufferSize); + this.buffer = MessageBuffer.allocate(bufferSize); } /** @@ -61,7 +61,7 @@ public ReadableByteChannel reset(ReadableByteChannel channel) public MessageBuffer next() throws IOException { - ByteBuffer b = m.sliceAsByteBuffer(); + ByteBuffer b = buffer.sliceAsByteBuffer(); while (b.remaining() > 0) { int ret = channel.read(b); if (ret == -1) { @@ -69,7 +69,7 @@ public MessageBuffer next() } } b.flip(); - return b.remaining() == 0 ? null : m.slice(0, b.limit()); + return b.remaining() == 0 ? null : buffer.slice(0, b.limit()); } @Override @@ -78,5 +78,4 @@ public void close() { channel.close(); } - } diff --git a/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferBE.java b/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferBE.java index 3f5d24976..1326b396e 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferBE.java +++ b/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferBE.java @@ -15,8 +15,6 @@ // package org.msgpack.core.buffer; -import java.nio.ByteBuffer; - import static org.msgpack.core.Preconditions.checkArgument; /** diff --git a/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferInput.java b/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferInput.java index 5925557cf..46eea243e 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferInput.java +++ b/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferInput.java @@ -26,13 +26,12 @@ public interface MessageBufferInput { /** * Get a next buffer to read. - * - * When this method is called twice, the formally allocated buffer can be safely discarded. + *

+ * When this method is called, the formally allocated buffer can be safely discarded. * * @return the next MessageBuffer, or return null if no more buffer is available. * @throws IOException when error occurred when reading the data */ - public MessageBuffer next() + MessageBuffer next() throws IOException; - } diff --git a/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferOutput.java b/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferOutput.java index 92eb760a9..024414bae 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferOutput.java +++ b/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferOutput.java @@ -30,11 +30,11 @@ public interface MessageBufferOutput * If the previously allocated buffer is not flushed yet, this next method should discard * it without writing it. * - * @param mimimumSize the mimium required buffer size to allocate + * @param minimumSize the mimium required buffer size to allocate * @return * @throws IOException */ - public MessageBuffer next(int mimimumSize) + MessageBuffer next(int minimumSize) throws IOException; /** @@ -45,7 +45,7 @@ public MessageBuffer next(int mimimumSize) * @param length the size of buffer to flush * @throws IOException */ - public void writeBuffer(int length) + void writeBuffer(int length) throws IOException; /** @@ -58,7 +58,7 @@ public void writeBuffer(int length) * @return * @throws IOException */ - public void write(byte[] buffer, int offset, int length) + void write(byte[] buffer, int offset, int length) throws IOException; /** @@ -71,6 +71,6 @@ public void write(byte[] buffer, int offset, int length) * @return * @throws IOException */ - public void add(byte[] buffer, int offset, int length) + void add(byte[] buffer, int offset, int length) throws IOException; } diff --git a/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferU.java b/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferU.java index 151b6cd2e..1e8783738 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferU.java +++ b/msgpack-core/src/main/java/org/msgpack/core/buffer/MessageBufferU.java @@ -16,10 +16,8 @@ package org.msgpack.core.buffer; import java.nio.ByteBuffer; -import java.nio.ByteOrder; import static org.msgpack.core.Preconditions.checkArgument; -import static org.msgpack.core.Preconditions.checkNotNull; /** * Universal MessageBuffer implementation supporting Java6 and Android. diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableArrayValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableArrayValueImpl.java index 09fc18e52..3e1b732c2 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableArrayValueImpl.java +++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableArrayValueImpl.java @@ -196,7 +196,8 @@ private static void appendString(StringBuilder sb, Value value) { if (value.isRawValue()) { sb.append(value.toJson()); - } else { + } + else { sb.append(value.toString()); } } diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBigIntegerValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBigIntegerValueImpl.java index b1c7c0b10..c6fe39386 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBigIntegerValueImpl.java +++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableBigIntegerValueImpl.java @@ -38,16 +38,16 @@ public class ImmutableBigIntegerValueImpl { public static MessageFormat mostSuccinctMessageFormat(IntegerValue v) { - if(v.isInByteRange()) { + if (v.isInByteRange()) { return MessageFormat.INT8; } - else if(v.isInShortRange()) { + else if (v.isInShortRange()) { return MessageFormat.INT16; } - else if(v.isInIntRange()) { + else if (v.isInIntRange()) { return MessageFormat.INT32; } - else if(v.isInLongRange()) { + else if (v.isInLongRange()) { return MessageFormat.INT64; } else { @@ -55,7 +55,6 @@ else if(v.isInLongRange()) { } } - private final BigInteger value; public ImmutableBigIntegerValueImpl(BigInteger value) diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableDoubleValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableDoubleValueImpl.java index b7fa39397..2aae1633a 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableDoubleValueImpl.java +++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableDoubleValueImpl.java @@ -130,7 +130,8 @@ public String toJson() { if (Double.isNaN(value) || Double.isInfinite(value)) { return "null"; - } else { + } + else { return Double.toString(value); } } diff --git a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableMapValueImpl.java b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableMapValueImpl.java index 3df98d619..dc55d783f 100644 --- a/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableMapValueImpl.java +++ b/msgpack-core/src/main/java/org/msgpack/value/impl/ImmutableMapValueImpl.java @@ -172,7 +172,8 @@ private static void appendJsonKey(StringBuilder sb, Value key) { if (key.isRawValue()) { sb.append(key.toJson()); - } else { + } + else { ImmutableStringValueImpl.appendJsonString(sb, key.toString()); } } @@ -202,7 +203,8 @@ private static void appendString(StringBuilder sb, Value value) { if (value.isRawValue()) { sb.append(value.toJson()); - } else { + } + else { sb.append(value.toString()); } } diff --git a/msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java b/msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java index 9a8242591..0b0e50123 100644 --- a/msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java +++ b/msgpack-core/src/test/java/org/msgpack/core/example/MessagePackExample.java @@ -17,7 +17,8 @@ import org.msgpack.core.MessageFormat; import org.msgpack.core.MessagePack; -import org.msgpack.core.MessagePackFactory; +import org.msgpack.core.MessagePack.PackerConfig; +import org.msgpack.core.MessagePack.UnpackerConfig; import org.msgpack.core.MessagePacker; import org.msgpack.core.MessageUnpacker; import org.msgpack.value.ArrayValue; @@ -245,23 +246,21 @@ else if (iv.isInLongRange()) { public static void configuration() throws IOException { - // Build a conifiguration - MessagePackFactory factory = new MessagePackFactory() - .unpackActionOnMalformedString(CodingErrorAction.REPLACE) // Drop malformed and unmappable UTF-8 characters - .unpackActionOnUnmappableString(CodingErrorAction.REPLACE) - .outputBufferSize(8192 * 2); - // Create a that uses this configuration - - // Pack data ByteArrayOutputStream out = new ByteArrayOutputStream(); - MessagePacker packer = factory.newPacker(out); + PackerConfig packerConfig = new PackerConfig(); + packerConfig.smallStringOptimizationThreshold = 256; // String + MessagePacker packer = packerConfig.newPacker(out); + packer.packInt(10); packer.packBoolean(true); packer.close(); // Unpack data + UnpackerConfig unpackerConfig = new UnpackerConfig(); + unpackerConfig.stringDecoderBufferSize = 16 * 1024; // If your data contains many large strings (the default is 8k) + byte[] packedData = out.toByteArray(); - MessageUnpacker unpacker = factory.newUnpacker(packedData); + MessageUnpacker unpacker = unpackerConfig.newUnpacker(packedData); int i = unpacker.unpackInt(); // 10 boolean b = unpacker.unpackBoolean(); // true unpacker.close(); diff --git a/msgpack-core/src/test/scala/org/msgpack/core/MessageFormatTest.scala b/msgpack-core/src/test/scala/org/msgpack/core/MessageFormatTest.scala index a6a71e2d9..be9d270cd 100644 --- a/msgpack-core/src/test/scala/org/msgpack/core/MessageFormatTest.scala +++ b/msgpack-core/src/test/scala/org/msgpack/core/MessageFormatTest.scala @@ -15,7 +15,7 @@ // package org.msgpack.core -import org.msgpack.core.MessageFormat.Code +import org.msgpack.core.MessagePack.Code import org.msgpack.value.ValueType import org.scalatest.exceptions.TestFailedException diff --git a/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala b/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala index 8c2339f7d..112c3e5a7 100644 --- a/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala +++ b/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala @@ -20,7 +20,8 @@ import java.math.BigInteger import java.nio.CharBuffer import java.nio.charset.{CodingErrorAction, UnmappableCharacterException} -import org.msgpack.core.MessageFormat.Code +import org.msgpack.core.MessagePack.Code +import org.msgpack.core.MessagePack.{UnpackerConfig, PackerConfig} import org.msgpack.value.{Value, Variable} import scala.util.Random @@ -60,14 +61,13 @@ class MessagePackTest extends MessagePackSpec { } "detect fixarray values" in { - - val packer = new MessagePackFactory().newBufferPacker() + val packer = MessagePack.newDefaultBufferPacker() packer.packArrayHeader(0) packer.close val bytes = packer.toByteArray - new MessagePackFactory().newUnpacker(bytes).unpackArrayHeader() shouldBe 0 + MessagePack.newDefaultUnpacker(bytes).unpackArrayHeader() shouldBe 0 try { - new MessagePackFactory().newUnpacker(bytes).unpackMapHeader() + MessagePack.newDefaultUnpacker(bytes).unpackMapHeader() fail("Shouldn't reach here") } catch { @@ -76,14 +76,13 @@ class MessagePackTest extends MessagePackSpec { } "detect fixmap values" in { - - val packer = new MessagePackFactory().newBufferPacker() + val packer = MessagePack.newDefaultBufferPacker() packer.packMapHeader(0) packer.close val bytes = packer.toByteArray - new MessagePackFactory().newUnpacker(bytes).unpackMapHeader() shouldBe 0 + MessagePack.newDefaultUnpacker(bytes).unpackMapHeader() shouldBe 0 try { - new MessagePackFactory().newUnpacker(bytes).unpackArrayHeader() + MessagePack.newDefaultUnpacker(bytes).unpackArrayHeader() fail("Shouldn't reach here") } catch { @@ -149,17 +148,23 @@ class MessagePackTest extends MessagePackSpec { } - def check[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A, factory: MessagePackFactory = new MessagePackFactory()): Unit = { + def check[A]( + v: A, + pack: MessagePacker => Unit, + unpack: MessageUnpacker => A, + packerConfig: PackerConfig = new PackerConfig(), + unpackerConfig: UnpackerConfig = new UnpackerConfig() + ): Unit = { var b: Array[Byte] = null try { val bs = new ByteArrayOutputStream() - val packer = factory.newPacker(bs) + val packer = packerConfig.newPacker(bs) pack(packer) packer.close() b = bs.toByteArray - val unpacker = factory.newUnpacker(b) + val unpacker = unpackerConfig.newUnpacker(b) val ret = unpack(unpacker) ret shouldBe v } @@ -173,17 +178,22 @@ class MessagePackTest extends MessagePackSpec { } } - def checkException[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A, - factory: MessagePackFactory = new MessagePackFactory()): Unit = { + def checkException[A]( + v: A, + pack: MessagePacker => Unit, + unpack: MessageUnpacker => A, + packerConfig: PackerConfig = new PackerConfig(), + unpaackerConfig: UnpackerConfig = new UnpackerConfig() + ): Unit = { var b: Array[Byte] = null val bs = new ByteArrayOutputStream() - val packer = factory.newPacker(bs) + val packer = packerConfig.newPacker(bs) pack(packer) packer.close() b = bs.toByteArray - val unpacker = factory.newUnpacker(b) + val unpacker = unpaackerConfig.newUnpacker(b) val ret = unpack(unpacker) fail("cannot not reach here") @@ -198,9 +208,6 @@ class MessagePackTest extends MessagePackSpec { } } - - - "pack/unpack primitive values" taggedAs ("prim") in { forAll { (v: Boolean) => check(v, _.packBoolean(v), _.unpackBoolean) } forAll { (v: Byte) => check(v, _.packByte(v), _.unpackByte) } @@ -329,9 +336,9 @@ class MessagePackTest extends MessagePackSpec { //val unmappableChar = Array[Char](new Character(0xfc0a).toChar) // Report error on unmappable character - val factory = new MessagePackFactory() - .unpackActionOnMalformedString(CodingErrorAction.REPORT) - .unpackActionOnUnmappableString(CodingErrorAction.REPORT); + val unpackerConfig = new UnpackerConfig() + unpackerConfig.actionOnMalformedString = CodingErrorAction.REPORT + unpackerConfig.actionOnUnmappableString = CodingErrorAction.REPORT for (bytes <- Seq(unmappable)) { When("unpacking") @@ -341,20 +348,12 @@ class MessagePackTest extends MessagePackSpec { packer.writePayload(bytes) }, _.unpackString(), - factory) + new PackerConfig(), + unpackerConfig) } catch { case e: MessageStringCodingException => // OK } - - // When("packing") - // try { - // val s = new String(unmappableChar) - // checkException(s, _.packString(s), _.unpackString()) - // } - // catch { - // case e:MessageStringCodingException => // OK - // } } } diff --git a/msgpack-core/src/test/scala/org/msgpack/core/MessagePackerTest.scala b/msgpack-core/src/test/scala/org/msgpack/core/MessagePackerTest.scala index 51d411f5f..2fbe461d1 100644 --- a/msgpack-core/src/test/scala/org/msgpack/core/MessagePackerTest.scala +++ b/msgpack-core/src/test/scala/org/msgpack/core/MessagePackerTest.scala @@ -16,8 +16,8 @@ package org.msgpack.core import java.io.{ByteArrayOutputStream, File, FileInputStream, FileOutputStream} -import java.nio.ByteBuffer +import org.msgpack.core.MessagePack.{UnpackerConfig, PackerConfig} import org.msgpack.core.buffer.{ChannelBufferOutput, OutputStreamBufferOutput} import org.msgpack.value.ValueFactory import xerial.core.io.IOUtil @@ -27,13 +27,10 @@ import scala.util.Random /** * */ -class MessagePackerTest - extends MessagePackSpec { - - val factory = new MessagePackFactory() +class MessagePackerTest extends MessagePackSpec { def verifyIntSeq(answer: Array[Int], packed: Array[Byte]) { - val unpacker = factory.newUnpacker(packed) + val unpacker = MessagePack.newDefaultUnpacker(packed) val b = Array.newBuilder[Int] while (unpacker.hasNext) { b += unpacker.unpackInt() @@ -69,7 +66,7 @@ class MessagePackerTest val b = new ByteArrayOutputStream - val packer = factory.newPacker(b) + val packer = MessagePack.newDefaultPacker(b) intSeq foreach packer.packInt packer.close verifyIntSeq(intSeq, b.toByteArray) @@ -102,7 +99,7 @@ class MessagePackerTest block("no-buffer-reset") { val out = new ByteArrayOutputStream - IOUtil.withResource(factory.newPacker(out)) { packer => + IOUtil.withResource(MessagePack.newDefaultPacker(out)) { packer => for (i <- 0 until N) { val outputStream = new ByteArrayOutputStream() @@ -118,7 +115,7 @@ class MessagePackerTest block("buffer-reset") { val out = new ByteArrayOutputStream - IOUtil.withResource(factory.newPacker(out)) { packer => + IOUtil.withResource(MessagePack.newDefaultPacker(out)) { packer => val bufferOut = new OutputStreamBufferOutput(new ByteArrayOutputStream()) @@ -140,23 +137,19 @@ class MessagePackerTest "pack larger string array than byte buf" taggedAs ("larger-string-array-than-byte-buf") in { // Based on https://github.com/msgpack/msgpack-java/issues/154 - // TODO: Refactor this test code to fit other ones. def test(bufferSize: Int, stringSize: Int): Boolean = { - val factory = new MessagePackFactory() - .outputBufferSize(bufferSize); val str = "a" * stringSize val rawString = ValueFactory.newString(str.getBytes("UTF-8")) val array = ValueFactory.newArray(rawString) - val out = new - ByteArrayOutputStream() - val packer = factory.newPacker(out) + val out = new ByteArrayOutputStream(bufferSize) + val packer = MessagePack.newDefaultPacker(out) packer.packValue(array) packer.close() out.toByteArray true } - val testCases = List( + val testCases = Seq( 32 -> 30, 33 -> 31, 32 -> 31, @@ -265,7 +258,7 @@ class MessagePackerTest "compute totalWrittenBytes" in { val out = new ByteArrayOutputStream - val packerTotalWrittenBytes = IOUtil.withResource(factory.newPacker(out)) { packer => + val packerTotalWrittenBytes = IOUtil.withResource(MessagePack.newDefaultPacker(out)) { packer => packer.packByte(0) // 1 .packBoolean(true) // 1 .packShort(12) // 1 diff --git a/msgpack-core/src/test/scala/org/msgpack/core/MessageUnpackerTest.scala b/msgpack-core/src/test/scala/org/msgpack/core/MessageUnpackerTest.scala index 2611d8c9d..e8ed65be7 100644 --- a/msgpack-core/src/test/scala/org/msgpack/core/MessageUnpackerTest.scala +++ b/msgpack-core/src/test/scala/org/msgpack/core/MessageUnpackerTest.scala @@ -29,11 +29,9 @@ import scala.util.Random */ class MessageUnpackerTest extends MessagePackSpec { - val factory = new MessagePackFactory() - def testData: Array[Byte] = { val out = new ByteArrayOutputStream() - val packer = factory.newPacker(out) + val packer = MessagePack.newDefaultPacker(out) packer .packArrayHeader(2) @@ -55,7 +53,7 @@ class MessageUnpackerTest extends MessagePackSpec { def testData2: Array[Byte] = { val out = new ByteArrayOutputStream() - val packer = factory.newPacker(out); + val packer = MessagePack.newDefaultPacker(out); packer .packBoolean(true) @@ -125,7 +123,7 @@ class MessageUnpackerTest extends MessagePackSpec { def testData3(N: Int): Array[Byte] = { val out = new ByteArrayOutputStream() - val packer = factory.newPacker(out) + val packer = MessagePack.newDefaultPacker(out) val r = new Random(0) @@ -179,7 +177,7 @@ class MessageUnpackerTest extends MessagePackSpec { "parse message packed data" taggedAs ("unpack") in { val arr = testData - val unpacker = factory.newUnpacker(arr) + val unpacker = MessagePack.newDefaultUnpacker(arr) var count = 0 while (unpacker.hasNext) { @@ -192,7 +190,7 @@ class MessageUnpackerTest extends MessagePackSpec { "skip reading values" in { - val unpacker = factory.newUnpacker(testData) + val unpacker = MessagePack.newDefaultUnpacker(testData) var skipCount = 0 while (unpacker.hasNext) { unpacker.skipValue() @@ -209,7 +207,7 @@ class MessageUnpackerTest extends MessagePackSpec { time("skip performance", repeat = 100) { block("switch") { - val unpacker = factory.newUnpacker(data) + val unpacker = MessagePack.newDefaultUnpacker(data) var skipCount = 0 while (unpacker.hasNext) { unpacker.skipValue() @@ -227,7 +225,7 @@ class MessageUnpackerTest extends MessagePackSpec { val ib = Seq.newBuilder[Int] - val unpacker = factory.newUnpacker(testData2) + val unpacker = MessagePack.newDefaultUnpacker(testData2) while (unpacker.hasNext) { val f = unpacker.getNextFormat f.getValueType match { @@ -269,7 +267,7 @@ class MessageUnpackerTest extends MessagePackSpec { trait SplitTest { val data: Array[Byte] def run { - val unpacker = factory.newUnpacker(data) + val unpacker = MessagePack.newDefaultUnpacker(data) val numElems = { var c = 0 while (unpacker.hasNext) { @@ -283,7 +281,7 @@ class MessageUnpackerTest extends MessagePackSpec { debug(s"split at $splitPoint") val (h, t) = data.splitAt(splitPoint) val bin = new SplitMessageBufferInput(Array(h, t)) - val unpacker = new MessageUnpacker(bin) + val unpacker = MessagePack.newDefaultUnpacker(bin) var count = 0 while (unpacker.hasNext) { count += 1 @@ -326,7 +324,7 @@ class MessageUnpackerTest extends MessagePackSpec { } block("v7") { - val unpacker = factory.newUnpacker(data) + val unpacker = MessagePack.newDefaultUnpacker(data) var count = 0 try { while (unpacker.hasNext) { @@ -428,7 +426,7 @@ class MessageUnpackerTest extends MessagePackSpec { } block("v7") { - val unpacker = factory.newUnpacker(data) + val unpacker = MessagePack.newDefaultUnpacker(data) var count = 0 try { while (unpacker.hasNext) { @@ -449,7 +447,7 @@ class MessageUnpackerTest extends MessagePackSpec { "be faster for reading binary than v6" taggedAs ("cmp-binary") in { val bos = new ByteArrayOutputStream() - val packer = factory.newPacker(bos) + val packer = MessagePack.newDefaultPacker(bos) val L = 10000 val R = 100 (0 until R).foreach { i => @@ -472,7 +470,7 @@ class MessageUnpackerTest extends MessagePackSpec { } block("v7") { - val unpacker = factory.newUnpacker(b) + val unpacker = MessagePack.newDefaultUnpacker(b) var i = 0 while (i < R) { val len = unpacker.unpackBinaryHeader() @@ -484,7 +482,7 @@ class MessageUnpackerTest extends MessagePackSpec { } block("v7-ref") { - val unpacker = factory.newUnpacker(b) + val unpacker = MessagePack.newDefaultUnpacker(b) var i = 0 while (i < R) { val len = unpacker.unpackBinaryHeader() @@ -505,12 +503,12 @@ class MessageUnpackerTest extends MessagePackSpec { val data = new Array[Byte](s) Random.nextBytes(data) val b = new ByteArrayOutputStream() - val packer = factory.newPacker(b) + val packer = MessagePack.newDefaultPacker(b) packer.packBinaryHeader(s) packer.writePayload(data) packer.close() - val unpacker = factory.newUnpacker(b.toByteArray) + val unpacker = MessagePack.newDefaultUnpacker(b.toByteArray) val len = unpacker.unpackBinaryHeader() len shouldBe s val ref = unpacker.readPayloadAsReference(len) @@ -529,7 +527,7 @@ class MessageUnpackerTest extends MessagePackSpec { val data = intSeq val b = createMessagePackData(packer => data foreach packer.packInt) - val unpacker = factory.newUnpacker(b) + val unpacker = MessagePack.newDefaultUnpacker(b) val unpacked = Array.newBuilder[Int] while (unpacker.hasNext) { @@ -564,7 +562,7 @@ class MessageUnpackerTest extends MessagePackSpec { "improve the performance via reset method" taggedAs ("reset-arr") in { val out = new ByteArrayOutputStream - val packer = factory.newPacker(out) + val packer = MessagePack.newDefaultPacker(out) packer.packInt(0) packer.flush val arr = out.toByteArray @@ -573,7 +571,7 @@ class MessageUnpackerTest extends MessagePackSpec { val N = 1000 val t = time("unpacker", repeat = 10) { block("no-buffer-reset") { - IOUtil.withResource(factory.newUnpacker(arr)) { unpacker => + IOUtil.withResource(MessagePack.newDefaultUnpacker(arr)) { unpacker => for (i <- 0 until N) { val buf = new ArrayBufferInput(arr) unpacker.reset(buf) @@ -584,7 +582,7 @@ class MessageUnpackerTest extends MessagePackSpec { } block("reuse-array-input") { - IOUtil.withResource(factory.newUnpacker(arr)) { unpacker => + IOUtil.withResource(MessagePack.newDefaultUnpacker(arr)) { unpacker => val buf = new ArrayBufferInput(arr) for (i <- 0 until N) { buf.reset(arr) @@ -596,7 +594,7 @@ class MessageUnpackerTest extends MessagePackSpec { } block("reuse-message-buffer") { - IOUtil.withResource(factory.newUnpacker(arr)) { unpacker => + IOUtil.withResource(MessagePack.newDefaultUnpacker(arr)) { unpacker => val buf = new ArrayBufferInput(arr) for (i <- 0 until N) { buf.reset(mb) @@ -640,7 +638,7 @@ class MessageUnpackerTest extends MessagePackSpec { "unpack large string data" taggedAs ("large-string") in { def createLargeData(stringLength: Int): Array[Byte] = { val out = new ByteArrayOutputStream() - val packer = factory.newPacker(out) + val packer = MessagePack.newDefaultPacker(out) packer .packArrayHeader(2) @@ -655,7 +653,7 @@ class MessageUnpackerTest extends MessagePackSpec { Seq(8191, 8192, 8193, 16383, 16384, 16385).foreach { n => val arr = createLargeData(n) - val unpacker = factory.newUnpacker(arr) + val unpacker = MessagePack.newDefaultUnpacker(arr) unpacker.unpackArrayHeader shouldBe 2 unpacker.unpackString.length shouldBe n @@ -676,7 +674,7 @@ class MessageUnpackerTest extends MessagePackSpec { packer.packString(expected) packer.close - val unpacker = new MessageUnpacker(new InputStreamBufferInput(new ByteArrayInputStream(out.toByteArray))) + val unpacker = MessagePack.newDefaultUnpacker(new InputStreamBufferInput(new ByteArrayInputStream(out.toByteArray))) val len = unpacker.unpackBinaryHeader unpacker.readPayload(len) val got = unpacker.unpackString diff --git a/msgpack-core/src/test/scala/org/msgpack/core/buffer/ByteStringTest.scala b/msgpack-core/src/test/scala/org/msgpack/core/buffer/ByteStringTest.scala index eb78432d2..2c080b59a 100644 --- a/msgpack-core/src/test/scala/org/msgpack/core/buffer/ByteStringTest.scala +++ b/msgpack-core/src/test/scala/org/msgpack/core/buffer/ByteStringTest.scala @@ -16,7 +16,7 @@ package org.msgpack.core.buffer import akka.util.ByteString -import org.msgpack.core.{MessagePackSpec, MessageUnpacker} +import org.msgpack.core.{MessagePack, MessagePackSpec, MessageUnpacker} class ByteStringTest extends MessagePackSpec { @@ -41,7 +41,6 @@ class ByteStringTest override def close(): Unit = {} } - new - MessageUnpacker(input).unpackString() + MessagePack.newDefaultUnpacker(input).unpackString() } } diff --git a/msgpack-core/src/test/scala/org/msgpack/core/buffer/MessageBufferInputTest.scala b/msgpack-core/src/test/scala/org/msgpack/core/buffer/MessageBufferInputTest.scala index 6333e381c..1638806ee 100644 --- a/msgpack-core/src/test/scala/org/msgpack/core/buffer/MessageBufferInputTest.scala +++ b/msgpack-core/src/test/scala/org/msgpack/core/buffer/MessageBufferInputTest.scala @@ -24,9 +24,6 @@ import xerial.core.io.IOUtil._ import scala.util.Random -/** - * Created on 5/30/14. - */ class MessageBufferInputTest extends MessagePackSpec { @@ -144,8 +141,7 @@ class MessageBufferInputTest } def readInt(buf: MessageBufferInput): Int = { - val unpacker = new - MessageUnpacker(buf) + val unpacker = MessagePack.newDefaultUnpacker(buf) unpacker.unpackInt } diff --git a/msgpack-core/src/test/scala/org/msgpack/value/ValueTypeTest.scala b/msgpack-core/src/test/scala/org/msgpack/value/ValueTypeTest.scala index 2bd1c7b14..979c33c9b 100644 --- a/msgpack-core/src/test/scala/org/msgpack/value/ValueTypeTest.scala +++ b/msgpack-core/src/test/scala/org/msgpack/value/ValueTypeTest.scala @@ -15,20 +15,22 @@ // package org.msgpack.value -import org.msgpack.core.MessageFormat.Code._ +import org.msgpack.core.MessagePack.Code._ import org.msgpack.core.{MessageFormat, MessageFormatException, MessagePackSpec} /** - * Created on 2014/05/06. - */ + * Created on 2014/05/06. + */ class ValueTypeTest - extends MessagePackSpec { + extends MessagePackSpec +{ "ValueType" should { "lookup ValueType from a byte value" taggedAs ("code") in { - def check(b: Byte, tpe: ValueType) { + def check(b: Byte, tpe: ValueType) + { MessageFormat.valueOf(b).getValueType shouldBe tpe } diff --git a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFormatFactory.java b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFactory.java similarity index 98% rename from msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFormatFactory.java rename to msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFactory.java index 1082e5770..ff7aa373f 100644 --- a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFormatFactory.java +++ b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFactory.java @@ -30,7 +30,7 @@ import java.io.Writer; import java.util.Arrays; -public class MessagePackFormatFactory +public class MessagePackFactory extends JsonFactory { private static final long serialVersionUID = 2578263992015504347L; diff --git a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java index c040ee7dd..e62528a75 100644 --- a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java +++ b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackGenerator.java @@ -20,6 +20,7 @@ import com.fasterxml.jackson.core.ObjectCodec; import com.fasterxml.jackson.core.base.GeneratorBase; import com.fasterxml.jackson.core.json.JsonWriteContext; +import org.msgpack.core.MessagePack; import org.msgpack.core.MessagePacker; import org.msgpack.core.buffer.OutputStreamBufferOutput; @@ -109,7 +110,7 @@ public MessagePackGenerator(int features, ObjectCodec codec, OutputStream out) messageBufferOutputHolder.set(messageBufferOutput); if (messagePacker == null) { - messagePacker = new MessagePacker(messageBufferOutput); + messagePacker = MessagePack.newDefaultPacker(messageBufferOutput); } else { messagePacker.reset(messageBufferOutput); diff --git a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java index 7ac2fa5af..e85ff7cd6 100644 --- a/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java +++ b/msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackParser.java @@ -28,11 +28,17 @@ import com.fasterxml.jackson.core.io.IOContext; import com.fasterxml.jackson.core.json.DupDetector; import com.fasterxml.jackson.core.json.JsonReadContext; +import org.msgpack.core.MessagePack; import org.msgpack.core.MessageUnpacker; import org.msgpack.core.buffer.ArrayBufferInput; import org.msgpack.core.buffer.InputStreamBufferInput; import org.msgpack.core.buffer.MessageBufferInput; -import org.msgpack.value.*; +import org.msgpack.value.ExtensionValue; +import org.msgpack.value.IntegerValue; +import org.msgpack.value.Value; +import org.msgpack.value.ValueFactory; +import org.msgpack.value.ValueType; +import org.msgpack.value.Variable; import java.io.IOException; import java.io.InputStream; @@ -121,7 +127,7 @@ private MessagePackParser(IOContext ctxt, int features, MessageBufferInput input MessageUnpacker messageUnpacker; Tuple messageUnpackerTuple = messageUnpackerHolder.get(); if (messageUnpackerTuple == null) { - messageUnpacker = new MessageUnpacker(input); + messageUnpacker = MessagePack.newDefaultUnpacker(input); } else { // Considering to reuse InputStream with JsonParser.Feature.AUTO_CLOSE_SOURCE, @@ -278,7 +284,8 @@ else if (newStack instanceof StackItemForObject) { @Override protected void _handleEOF() throws JsonParseException - {} + { + } @Override public String getText() diff --git a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/ExampleOfTypeInformationSerDe.java b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/ExampleOfTypeInformationSerDe.java index 6a211b3ae..5414b0bdc 100644 --- a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/ExampleOfTypeInformationSerDe.java +++ b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/ExampleOfTypeInformationSerDe.java @@ -151,7 +151,7 @@ public void test() objectContainer.getObjects().put("pi", pi); } - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); byte[] bytes = objectMapper.writeValueAsBytes(objectContainer); ObjectContainer restored = objectMapper.readValue(bytes, ObjectContainer.class); diff --git a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackDataformatTestBase.java b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackDataformatTestBase.java index c9692ebf9..1d5156adc 100644 --- a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackDataformatTestBase.java +++ b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackDataformatTestBase.java @@ -35,7 +35,7 @@ public class MessagePackDataformatTestBase { - protected MessagePackFormatFactory factory; + protected MessagePackFactory factory; protected ByteArrayOutputStream out; protected ByteArrayInputStream in; protected ObjectMapper objectMapper; @@ -47,7 +47,7 @@ public class MessagePackDataformatTestBase @Before public void setup() { - factory = new MessagePackFormatFactory(); + factory = new MessagePackFactory(); objectMapper = new ObjectMapper(factory); out = new ByteArrayOutputStream(); in = new ByteArrayInputStream(new byte[4096]); diff --git a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFormatFactoryTest.java b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFactoryTest.java similarity index 97% rename from msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFormatFactoryTest.java rename to msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFactoryTest.java index f7e5fe045..25180f784 100644 --- a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFormatFactoryTest.java +++ b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackFactoryTest.java @@ -24,7 +24,7 @@ import static org.junit.Assert.assertEquals; -public class MessagePackFormatFactoryTest +public class MessagePackFactoryTest extends MessagePackDataformatTestBase { @Test diff --git a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackGeneratorTest.java b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackGeneratorTest.java index de58e4a1a..fd2ea313f 100644 --- a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackGeneratorTest.java +++ b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackGeneratorTest.java @@ -24,6 +24,7 @@ import org.msgpack.core.MessageUnpacker; import org.msgpack.core.buffer.ArrayBufferInput; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -36,6 +37,11 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; @@ -80,7 +86,7 @@ public void testGeneratorShouldWriteObject() long bitmap = 0; byte[] bytes = objectMapper.writeValueAsBytes(hashMap); - MessageUnpacker messageUnpacker = new MessageUnpacker(new ArrayBufferInput(bytes)); + MessageUnpacker messageUnpacker = MessagePack.newDefaultUnpacker(new ArrayBufferInput(bytes)); assertEquals(hashMap.size(), messageUnpacker.unpackMapHeader()); for (int i = 0; i < hashMap.size(); i++) { String key = messageUnpacker.unpackString(); @@ -193,7 +199,7 @@ public void testGeneratorShouldWriteArray() long bitmap = 0; byte[] bytes = objectMapper.writeValueAsBytes(array); - MessageUnpacker messageUnpacker = new MessageUnpacker(new ArrayBufferInput(bytes)); + MessageUnpacker messageUnpacker = MessagePack.newDefaultUnpacker(new ArrayBufferInput(bytes)); assertEquals(array.size(), messageUnpacker.unpackArrayHeader()); // #1 assertEquals("komamitsu", messageUnpacker.unpackString()); @@ -230,7 +236,7 @@ else if (key.equals("num")) { public void testMessagePackGeneratorDirectly() throws Exception { - MessagePackFormatFactory messagePackFactory = new MessagePackFormatFactory(); + MessagePackFactory messagePackFactory = new MessagePackFactory(); File tempFile = createTempFile(); JsonGenerator generator = messagePackFactory.createGenerator(tempFile, JsonEncoding.UTF8); @@ -257,7 +263,7 @@ public void testMessagePackGeneratorDirectly() public void testWritePrimitives() throws Exception { - MessagePackFormatFactory messagePackFactory = new MessagePackFormatFactory(); + MessagePackFactory messagePackFactory = new MessagePackFactory(); File tempFile = createTempFile(); JsonGenerator generator = messagePackFactory.createGenerator(tempFile, JsonEncoding.UTF8); @@ -280,7 +286,7 @@ public void testWritePrimitives() public void testBigDecimal() throws IOException { - ObjectMapper mapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper mapper = new ObjectMapper(new MessagePackFactory()); { double d0 = 1.23456789; @@ -328,7 +334,7 @@ public void testEnableFeatureAutoCloseTarget() throws IOException { OutputStream out = createTempFileOutputStream(); - MessagePackFormatFactory messagePackFactory = new MessagePackFormatFactory(); + MessagePackFactory messagePackFactory = new MessagePackFactory(); ObjectMapper objectMapper = new ObjectMapper(messagePackFactory); List integers = Arrays.asList(1); objectMapper.writeValue(out, integers); @@ -341,7 +347,7 @@ public void testDisableFeatureAutoCloseTarget() { File tempFile = createTempFile(); OutputStream out = new FileOutputStream(tempFile); - MessagePackFormatFactory messagePackFactory = new MessagePackFormatFactory(); + MessagePackFactory messagePackFactory = new MessagePackFactory(); ObjectMapper objectMapper = new ObjectMapper(messagePackFactory); objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); List integers = Arrays.asList(1); @@ -363,7 +369,7 @@ public void testWritePrimitiveObjectViaObjectMapper() File tempFile = createTempFile(); OutputStream out = new FileOutputStream(tempFile); - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); objectMapper.writeValue(out, 1); objectMapper.writeValue(out, "two"); @@ -379,4 +385,54 @@ public void testWritePrimitiveObjectViaObjectMapper() assertEquals(4, unpacker.unpackInt()); assertEquals(5, unpacker.unpackLong()); } + + @Test + public void testInMultiThreads() + throws Exception + { + int threadCount = 8; + final int loopCount = 4000; + ExecutorService executorService = Executors.newFixedThreadPool(threadCount); + final ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); + objectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); + final List buffers = new ArrayList(threadCount); + List> results = new ArrayList>(); + + for (int ti = 0; ti < threadCount; ti++) { + buffers.add(new ByteArrayOutputStream()); + final int threadIndex = ti; + results.add(executorService.submit(new Callable() + { + @Override + public Exception call() + throws Exception + { + try { + for (int i = 0; i < loopCount; i++) { + objectMapper.writeValue(buffers.get(threadIndex), threadIndex); + } + return null; + } + catch (IOException e) { + return e; + } + } + })); + } + + for (int ti = 0; ti < threadCount; ti++) { + Future exceptionFuture = results.get(ti); + Exception exception = exceptionFuture.get(20, TimeUnit.SECONDS); + if (exception != null) { + throw exception; + } + else { + ByteArrayOutputStream outputStream = buffers.get(ti); + MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(outputStream.toByteArray()); + for (int i = 0; i < loopCount; i++) { + assertEquals(ti, unpacker.unpackInt()); + } + } + } + } } diff --git a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackParserTest.java b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackParserTest.java index 3321110d4..112390598 100644 --- a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackParserTest.java +++ b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/MessagePackParserTest.java @@ -27,7 +27,6 @@ import org.junit.Test; import org.msgpack.core.MessagePack; import org.msgpack.core.MessagePacker; -import org.msgpack.core.buffer.OutputStreamBufferOutput; import java.io.ByteArrayOutputStream; import java.io.File; @@ -54,7 +53,7 @@ public class MessagePackParserTest public void testParserShouldReadObject() throws IOException { - MessagePacker packer = new MessagePacker(new OutputStreamBufferOutput(out)); + MessagePacker packer = MessagePack.newDefaultPacker(out); packer.packMapHeader(9); // #1 packer.packString("str"); @@ -182,7 +181,7 @@ else if (k.equals("ext")) { public void testParserShouldReadArray() throws IOException { - MessagePacker packer = new MessagePacker(new OutputStreamBufferOutput(out)); + MessagePacker packer = MessagePack.newDefaultPacker(out); packer.packArrayHeader(11); // #1 packer.packArrayHeader(3); @@ -288,7 +287,7 @@ else if (k.equals("child_map_age")) { public void testMessagePackParserDirectly() throws IOException { - MessagePackFormatFactory factory = new MessagePackFormatFactory(); + MessagePackFactory factory = new MessagePackFactory(); File tempFile = File.createTempFile("msgpackTest", "msgpack"); tempFile.deleteOnExit(); @@ -354,7 +353,7 @@ public void testMessagePackParserDirectly() public void testReadPrimitives() throws Exception { - MessagePackFormatFactory factory = new MessagePackFormatFactory(); + MessagePackFactory factory = new MessagePackFactory(); File tempFile = createTempFile(); FileOutputStream out = new FileOutputStream(tempFile); @@ -387,7 +386,7 @@ public void testBigDecimal() { double d0 = 1.23456789; double d1 = 1.23450000000000000000006789; - MessagePacker packer = new MessagePacker(new OutputStreamBufferOutput(out)); + MessagePacker packer = MessagePack.newDefaultPacker(out); packer.packArrayHeader(5); packer.packDouble(d0); packer.packDouble(d1); @@ -396,7 +395,7 @@ public void testBigDecimal() packer.packDouble(Double.MIN_NORMAL); packer.flush(); - ObjectMapper mapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper mapper = new ObjectMapper(new MessagePackFactory()); mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true); List objects = mapper.readValue(out.toByteArray(), new TypeReference>() {}); assertEquals(5, objects.size()); @@ -431,7 +430,7 @@ public void testEnableFeatureAutoCloseSource() throws Exception { File tempFile = createTestFile(); - MessagePackFormatFactory factory = new MessagePackFormatFactory(); + MessagePackFactory factory = new MessagePackFactory(); FileInputStream in = new FileInputStream(tempFile); ObjectMapper objectMapper = new ObjectMapper(factory); objectMapper.readValue(in, new TypeReference>() {}); @@ -444,7 +443,7 @@ public void testDisableFeatureAutoCloseSource() { File tempFile = createTestFile(); FileInputStream in = new FileInputStream(tempFile); - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); objectMapper.readValue(in, new TypeReference>() {}); objectMapper.readValue(in, new TypeReference>() {}); @@ -456,7 +455,7 @@ public void testParseBigDecimal() { ArrayList list = new ArrayList(); list.add(new BigDecimal(Long.MAX_VALUE)); - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); byte[] bytes = objectMapper.writeValueAsBytes(list); ArrayList result = objectMapper.readValue( @@ -481,7 +480,7 @@ public void testReadPrimitiveObjectViaObjectMapper() packer.close(); FileInputStream in = new FileInputStream(tempFile); - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); objectMapper.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false); assertEquals("foo", objectMapper.readValue(in, new TypeReference() {})); long l = objectMapper.readValue(in, new TypeReference() {}); @@ -511,7 +510,7 @@ public void testBinaryKey() packer.packLong(42); packer.close(); - ObjectMapper mapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper mapper = new ObjectMapper(new MessagePackFactory()); Map object = mapper.readValue(new FileInputStream(tempFile), new TypeReference>() {}); assertEquals(2, object.size()); assertEquals(3.14, object.get("foo")); @@ -533,7 +532,7 @@ public void testBinaryKeyInNestedObject() packer.packInt(1); packer.close(); - ObjectMapper mapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper mapper = new ObjectMapper(new MessagePackFactory()); List objects = mapper.readValue(out.toByteArray(), new TypeReference>() {}); assertEquals(2, objects.size()); @SuppressWarnings(value = "unchecked") @@ -555,7 +554,7 @@ public void testByteArrayKey() messagePacker.packBinaryHeader(1).writePayload(k1).packInt(3); messagePacker.close(); - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); SimpleModule module = new SimpleModule(); module.addKeyDeserializer(byte[].class, new KeyDeserializer() { @@ -592,7 +591,7 @@ public void testIntegerKey() } messagePacker.close(); - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); SimpleModule module = new SimpleModule(); module.addKeyDeserializer(Integer.class, new KeyDeserializer() { @@ -623,7 +622,7 @@ public void testFloatKey() } messagePacker.close(); - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); SimpleModule module = new SimpleModule(); module.addKeyDeserializer(Float.class, new KeyDeserializer() { @@ -653,7 +652,7 @@ public void testBooleanKey() messagePacker.packBoolean(false).packInt(3); messagePacker.close(); - ObjectMapper objectMapper = new ObjectMapper(new MessagePackFormatFactory()); + ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); SimpleModule module = new SimpleModule(); module.addKeyDeserializer(Boolean.class, new KeyDeserializer() { diff --git a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatHugeDataBenchmarkTest.java b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatHugeDataBenchmarkTest.java index aaf828439..b3a159111 100644 --- a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatHugeDataBenchmarkTest.java +++ b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatHugeDataBenchmarkTest.java @@ -20,7 +20,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; -import org.msgpack.jackson.dataformat.MessagePackFormatFactory; +import org.msgpack.jackson.dataformat.MessagePackFactory; import java.io.File; import java.io.FileOutputStream; @@ -34,7 +34,7 @@ public class MessagePackDataformatHugeDataBenchmarkTest private static final int COUNT = 6; private static final int WARMUP_COUNT = 4; private final ObjectMapper origObjectMapper = new ObjectMapper(); - private final ObjectMapper msgpackObjectMapper = new ObjectMapper(new MessagePackFormatFactory()); + private final ObjectMapper msgpackObjectMapper = new ObjectMapper(new MessagePackFactory()); private static final List value; private static final byte[] packedByOriginal; private static final byte[] packedByMsgPack; @@ -61,7 +61,7 @@ public class MessagePackDataformatHugeDataBenchmarkTest packedByOriginal = bytes; try { - bytes = new ObjectMapper(new MessagePackFormatFactory()).writeValueAsBytes(value); + bytes = new ObjectMapper(new MessagePackFactory()).writeValueAsBytes(value); } catch (JsonProcessingException e) { e.printStackTrace(); diff --git a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatPojoBenchmarkTest.java b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatPojoBenchmarkTest.java index 33c5f024c..179b09891 100644 --- a/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatPojoBenchmarkTest.java +++ b/msgpack-jackson/src/test/java/org/msgpack/jackson/dataformat/benchmark/MessagePackDataformatPojoBenchmarkTest.java @@ -19,7 +19,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Test; -import org.msgpack.jackson.dataformat.MessagePackFormatFactory; +import org.msgpack.jackson.dataformat.MessagePackFactory; import static org.msgpack.jackson.dataformat.MessagePackDataformatTestBase.NormalPojo; import static org.msgpack.jackson.dataformat.MessagePackDataformatTestBase.Suit; @@ -32,19 +32,21 @@ public class MessagePackDataformatPojoBenchmarkTest { - private static final int LOOP_MAX = 600; - private static final int LOOP_FACTOR = 40; + private static final int LOOP_MAX = 200; + private static final int LOOP_FACTOR_SER = 40; + private static final int LOOP_FACTOR_DESER = 200; private static final int COUNT = 6; private static final int WARMUP_COUNT = 4; - private static final List pojos = new ArrayList(LOOP_MAX); - private static final List pojosSerWithOrig = new ArrayList(LOOP_MAX); - private static final List pojosSerWithMsgPack = new ArrayList(LOOP_MAX); + private final List pojos = new ArrayList(LOOP_MAX); + private final List pojosSerWithOrig = new ArrayList(LOOP_MAX); + private final List pojosSerWithMsgPack = new ArrayList(LOOP_MAX); private final ObjectMapper origObjectMapper = new ObjectMapper(); - private final ObjectMapper msgpackObjectMapper = new ObjectMapper(new MessagePackFormatFactory()); + private final ObjectMapper msgpackObjectMapper = new ObjectMapper(new MessagePackFactory()); - static { - final ObjectMapper origObjectMapper = new ObjectMapper(); - final ObjectMapper msgpackObjectMapper = new ObjectMapper(new MessagePackFormatFactory()); + public MessagePackDataformatPojoBenchmarkTest() + { + origObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); + msgpackObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); for (int i = 0; i < LOOP_MAX; i++) { NormalPojo pojo = new NormalPojo(); @@ -52,7 +54,11 @@ public class MessagePackDataformatPojoBenchmarkTest pojo.l = i; pojo.f = Float.valueOf(i); pojo.d = Double.valueOf(i); - pojo.setS(String.valueOf(i)); + StringBuilder sb = new StringBuilder(); + for (int sbi = 0; sbi < i * 50; sbi++) { + sb.append("x"); + } + pojo.setS(sb.toString()); pojo.bool = i % 2 == 0; pojo.bi = BigInteger.valueOf(i); switch (i % 4) { @@ -78,7 +84,7 @@ public class MessagePackDataformatPojoBenchmarkTest pojosSerWithOrig.add(origObjectMapper.writeValueAsBytes(pojos.get(i))); } catch (JsonProcessingException e) { - e.printStackTrace(); + throw new RuntimeException("Failed to create test data"); } } @@ -87,17 +93,11 @@ public class MessagePackDataformatPojoBenchmarkTest pojosSerWithMsgPack.add(msgpackObjectMapper.writeValueAsBytes(pojos.get(i))); } catch (JsonProcessingException e) { - e.printStackTrace(); + throw new RuntimeException("Failed to create test data"); } } } - public MessagePackDataformatPojoBenchmarkTest() - { - origObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); - msgpackObjectMapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false); - } - @Test public void testBenchmark() throws Exception @@ -117,7 +117,7 @@ public void testBenchmark() public void run() throws Exception { - for (int j = 0; j < LOOP_FACTOR; j++) { + for (int j = 0; j < LOOP_FACTOR_SER; j++) { for (int i = 0; i < LOOP_MAX; i++) { origObjectMapper.writeValue(outputStreamJackson, pojos.get(i)); } @@ -130,7 +130,7 @@ public void run() public void run() throws Exception { - for (int j = 0; j < LOOP_FACTOR; j++) { + for (int j = 0; j < LOOP_FACTOR_SER; j++) { for (int i = 0; i < LOOP_MAX; i++) { msgpackObjectMapper.writeValue(outputStreamMsgpack, pojos.get(i)); } @@ -143,7 +143,7 @@ public void run() public void run() throws Exception { - for (int j = 0; j < LOOP_FACTOR; j++) { + for (int j = 0; j < LOOP_FACTOR_DESER; j++) { for (int i = 0; i < LOOP_MAX; i++) { origObjectMapper.readValue(pojosSerWithOrig.get(i), NormalPojo.class); } @@ -156,7 +156,7 @@ public void run() public void run() throws Exception { - for (int j = 0; j < LOOP_FACTOR; j++) { + for (int j = 0; j < LOOP_FACTOR_DESER; j++) { for (int i = 0; i < LOOP_MAX; i++) { msgpackObjectMapper.readValue(pojosSerWithMsgPack.get(i), NormalPojo.class); }