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 59bbbad48..358a59f25 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java @@ -235,17 +235,11 @@ public static MessageUnpacker newDefaultUnpacker(byte[] contents, int offset, in */ 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; + private int smallStringOptimizationThreshold = 512; - /** - * When the next payload size exceeds this threshold, MessagePacker will call MessageBufferOutput.flush() before - * packing the data. - */ - public int bufferFlushThreshold = 8192; + private int bufferFlushThreshold = 8192; + + private int bufferSize = 8192; /** * Create a packer that outputs the packed data to a given output @@ -266,7 +260,7 @@ public MessagePacker newPacker(MessageBufferOutput out) */ public MessagePacker newPacker(OutputStream out) { - return newPacker(new OutputStreamBufferOutput(out)); + return newPacker(new OutputStreamBufferOutput(out, bufferSize)); } /** @@ -277,7 +271,7 @@ public MessagePacker newPacker(OutputStream out) */ public MessagePacker newPacker(WritableByteChannel channel) { - return newPacker(new ChannelBufferOutput(channel)); + return newPacker(new ChannelBufferOutput(channel, bufferSize)); } /** @@ -289,42 +283,74 @@ public MessageBufferPacker newBufferPacker() { return new MessageBufferPacker(this); } - } - /** - * MessageUnpacker configuration. - */ - public static class UnpackerConfig - { /** - * Allow unpackBinaryHeader to read str format family (default:true) + * Use String.getBytes() for converting Java Strings that are smaller than this threshold into UTF8. + * Note that this parameter is subject to change. */ - public boolean allowReadingStringAsBinary = true; + public PackerConfig setSmallStringOptimizationThreshold(int bytes) + { + this.smallStringOptimizationThreshold = bytes; + return this; + } - /** - * Allow unpackRawStringHeader and unpackString to read bin format family (default: true) - */ - public boolean allowReadingBinaryAsString = true; + public int getSmallStringOptimizationThreshold() + { + return smallStringOptimizationThreshold; + } /** - * Action when encountered a malformed input + * When the next payload size exceeds this threshold, MessagePacker will call MessageBufferOutput.flush() before + * packing the data (default: 8192). */ - public CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE; + public PackerConfig setBufferFlushThreshold(int bytes) + { + this.bufferFlushThreshold = bytes; + return this; + } - /** - * Action when an unmappable character is found - */ - public CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE; + public int getBufferFlushThreshold() + { + return bufferFlushThreshold; + } /** - * unpackString size limit. (default: Integer.MAX_VALUE) + * When a packer is created with newPacker(OutputStream) or newPacker(WritableByteChannel), the stream will be + * buffered with this size of buffer (default: 8192). */ - public int stringSizeLimit = Integer.MAX_VALUE; + public PackerConfig setBufferSize(int bytes) + { + this.bufferSize = bytes; + return this; + } + + public int getBufferSize() + { + return bufferSize; + } + } + + /** + * MessageUnpacker configuration. + */ + public static class UnpackerConfig + { + private boolean allowReadingStringAsBinary = true; + + private boolean allowReadingBinaryAsString = true; + + private CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE; + + private CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE; + + private int stringSizeLimit = Integer.MAX_VALUE; + + private int bufferSize = 8192; /** * */ - public int stringDecoderBufferSize = 8192; + private int stringDecoderBufferSize = 8192; /** * Create an unpacker that reads the data from a given input @@ -345,7 +371,7 @@ public MessageUnpacker newUnpacker(MessageBufferInput in) */ public MessageUnpacker newUnpacker(InputStream in) { - return newUnpacker(new InputStreamBufferInput(in)); + return newUnpacker(new InputStreamBufferInput(in, bufferSize)); } /** @@ -356,7 +382,7 @@ public MessageUnpacker newUnpacker(InputStream in) */ public MessageUnpacker newUnpacker(ReadableByteChannel channel) { - return newUnpacker(new ChannelBufferInput(channel)); + return newUnpacker(new ChannelBufferInput(channel, bufferSize)); } /** @@ -380,5 +406,104 @@ public MessageUnpacker newUnpacker(byte[] contents, int offset, int length) { return newUnpacker(new ArrayBufferInput(contents, offset, length)); } + + /** + * Allow unpackBinaryHeader to read str format family (default: true) + */ + public UnpackerConfig setAllowReadingStringAsBinary(boolean enable) + { + this.allowReadingStringAsBinary = enable; + return this; + } + + public boolean getAllowReadingStringAsBinary() + { + return allowReadingStringAsBinary; + } + + /** + * Allow unpackString and unpackRawStringHeader and unpackString to read bin format family (default: true) + */ + public UnpackerConfig setAllowReadingBinaryAsString(boolean enable) + { + this.allowReadingBinaryAsString = enable; + return this; + } + + public boolean getAllowReadingBinaryAsString() + { + return allowReadingBinaryAsString; + } + + /** + * Action when encountered a malformed input (default: REPLACE) + */ + public UnpackerConfig setActionOnMalformedString(CodingErrorAction action) + { + this.actionOnMalformedString = action; + return this; + } + + public CodingErrorAction getActionOnMalformedString() + { + return actionOnMalformedString; + } + + /** + * Action when an unmappable character is found (default: REPLACE) + */ + public UnpackerConfig setActionOnUnmappableString(CodingErrorAction action) + { + this.actionOnUnmappableString = action; + return this; + } + + public CodingErrorAction getActionOnUnmappableString() + { + return actionOnUnmappableString; + } + + /** + * unpackString size limit (default: Integer.MAX_VALUE). + */ + public UnpackerConfig setStringSizeLimit(int bytes) + { + this.stringSizeLimit = bytes; + return this; + } + + public int getStringSizeLimit() + { + return stringSizeLimit; + } + + /** + * + */ + public UnpackerConfig setStringDecoderBufferSize(int bytes) + { + this.stringDecoderBufferSize = bytes; + return this; + } + + public int getStringDecoderBufferSize() + { + return stringDecoderBufferSize; + } + + /** + * When a packer is created with newUnpacker(OutputStream) or newUnpacker(WritableByteChannel), the stream will be + * buffered with this size of buffer (default: 8192). + */ + public UnpackerConfig setBufferSize(int bytes) + { + this.bufferSize = bytes; + return this; + } + + public int getBufferSize() + { + return bufferSize; + } } } 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 c99653fc9..3cb806ad3 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessagePacker.java @@ -114,8 +114,8 @@ 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.smallStringOptimizationThreshold = config.getSmallStringOptimizationThreshold(); + this.bufferFlushThreshold = config.getBufferFlushThreshold(); this.position = 0; this.totalFlushBytes = 0; } 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 a7cf970eb..9275a5c84 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java @@ -130,12 +130,12 @@ public MessageUnpacker(MessageBufferInput in, MessagePack.UnpackerConfig config) { this.in = checkNotNull(in, "MessageBufferInput is null"); // 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; + this.allowReadingStringAsBinary = config.getAllowReadingStringAsBinary(); + this.allowReadingBinaryAsString = config.getAllowReadingBinaryAsString(); + this.actionOnMalformedString = config.getActionOnMalformedString(); + this.actionOnUnmappableString = config.getActionOnUnmappableString(); + this.stringSizeLimit = config.getStringSizeLimit(); + this.stringDecoderBufferSize = config.getStringDecoderBufferSize(); } /** 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 e8802a037..22da382c6 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 @@ -246,20 +246,19 @@ public static void configuration() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); - PackerConfig packerConfig = new PackerConfig(); - packerConfig.smallStringOptimizationThreshold = 256; // String - MessagePacker packer = packerConfig.newPacker(out); + MessagePacker packer = new PackerConfig() + .setSmallStringOptimizationThreshold(256) // String + .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 = unpackerConfig.newUnpacker(packedData); + MessageUnpacker unpacker = new UnpackerConfig() + .setStringDecoderBufferSize(16 * 1024) // If your data contains many large strings (the default is 8k) + .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/MessagePackTest.scala b/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala index 112c3e5a7..d8cac3495 100644 --- a/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala +++ b/msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala @@ -337,8 +337,9 @@ class MessagePackTest extends MessagePackSpec { // Report error on unmappable character val unpackerConfig = new UnpackerConfig() - unpackerConfig.actionOnMalformedString = CodingErrorAction.REPORT - unpackerConfig.actionOnUnmappableString = CodingErrorAction.REPORT + unpackerConfig + .setActionOnMalformedString(CodingErrorAction.REPORT) + .setActionOnUnmappableString(CodingErrorAction.REPORT) for (bytes <- Seq(unmappable)) { When("unpacking")