Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 160 additions & 35 deletions msgpack-core/src/main/java/org/msgpack/core/MessagePack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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
Expand All @@ -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));
}

/**
Expand All @@ -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));
}

/**
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down