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
153 changes: 122 additions & 31 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,9 @@ 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;

/**
* Create a packer that outputs the packed data to a given output
Expand Down Expand Up @@ -289,42 +281,57 @@ public MessageBufferPacker newBufferPacker()
{
return new MessageBufferPacker(this);
}

/**
* Use String.getBytes() for converting Java Strings that are smaller than this threshold into UTF8.
* Note that this parameter is subject to change.
*/
public PackerConfig setSmallStringOptimizationThreshold(int bytes)
{
this.smallStringOptimizationThreshold = bytes;
return this;
}

public int getSmallStringOptimizationThreshold()
{
return smallStringOptimizationThreshold;
}

/**
* When the next payload size exceeds this threshold, MessagePacker will call MessageBufferOutput.flush() before
* packing the data.
*/
public PackerConfig setBufferFlushThreshold(int bytes)
{
this.bufferFlushThreshold = bytes;
return this;
}

public int getBufferFlushThreshold()
{
return bufferFlushThreshold;
}
}

/**
* MessageUnpacker configuration.
*/
public static class UnpackerConfig
{
/**
* Allow unpackBinaryHeader to read str format family (default:true)
*/
public boolean allowReadingStringAsBinary = true;
private boolean allowReadingStringAsBinary = true;

/**
* Allow unpackRawStringHeader and unpackString to read bin format family (default: true)
*/
public boolean allowReadingBinaryAsString = true;
private boolean allowReadingBinaryAsString = true;

/**
* Action when encountered a malformed input
*/
public CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE;
private CodingErrorAction actionOnMalformedString = CodingErrorAction.REPLACE;

/**
* Action when an unmappable character is found
*/
public CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE;
private CodingErrorAction actionOnUnmappableString = CodingErrorAction.REPLACE;

/**
* unpackString size limit. (default: Integer.MAX_VALUE)
*/
public int stringSizeLimit = Integer.MAX_VALUE;
private int stringSizeLimit = Integer.MAX_VALUE;

/**
*
*/
public int stringDecoderBufferSize = 8192;
private int stringDecoderBufferSize = 8192;

/**
* Create an unpacker that reads the data from a given input
Expand Down Expand Up @@ -380,5 +387,89 @@ 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;
}
}
}
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