-
Notifications
You must be signed in to change notification settings - Fork 321
Support method-chain syntax to create packer and unpacker with immutable config #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5039f16
Support method-chain syntax to create packer and unpacker with config
frsyuki e34df59
Make buffer size of StreamBuffer{Input,Output} and ChannelBuffer{Inpu…
frsyuki 61184bf
make PackerConfg and UnpackerConfig immutable to make
frsyuki b1e30b2
added test code for PackerConfig and UnpackerConfig
frsyuki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,18 +234,41 @@ public static MessageUnpacker newDefaultUnpacker(byte[] contents, int offset, in | |
| * MessagePacker configuration. | ||
| */ | ||
| public static class PackerConfig | ||
| implements Cloneable | ||
| { | ||
| /** | ||
| * 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; | ||
|
|
||
| public PackerConfig() | ||
| { } | ||
|
|
||
| private PackerConfig(PackerConfig copy) | ||
| { | ||
| this.smallStringOptimizationThreshold = smallStringOptimizationThreshold; | ||
| this.bufferFlushThreshold = bufferFlushThreshold; | ||
| this.bufferSize = bufferSize; | ||
| } | ||
|
|
||
| @Override | ||
| public PackerConfig clone() | ||
| { | ||
| return new PackerConfig(this); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) | ||
| { | ||
| if (!(obj instanceof PackerConfig)) { | ||
| return false; | ||
| } | ||
| PackerConfig o = (PackerConfig) obj; | ||
| return this.smallStringOptimizationThreshold == o.smallStringOptimizationThreshold | ||
| && this.bufferFlushThreshold == o.bufferFlushThreshold | ||
| && this.bufferSize == o.bufferSize; | ||
| } | ||
|
|
||
| /** | ||
| * Create a packer that outputs the packed data to a given output | ||
|
|
@@ -266,7 +289,7 @@ public MessagePacker newPacker(MessageBufferOutput out) | |
| */ | ||
| public MessagePacker newPacker(OutputStream out) | ||
| { | ||
| return newPacker(new OutputStreamBufferOutput(out)); | ||
| return newPacker(new OutputStreamBufferOutput(out, bufferSize)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -277,7 +300,7 @@ public MessagePacker newPacker(OutputStream out) | |
| */ | ||
| public MessagePacker newPacker(WritableByteChannel channel) | ||
| { | ||
| return newPacker(new ChannelBufferOutput(channel)); | ||
| return newPacker(new ChannelBufferOutput(channel, bufferSize)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -289,42 +312,110 @@ 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 withSmallStringOptimizationThreshold(int bytes) | ||
| { | ||
| PackerConfig copy = clone(); | ||
| copy.smallStringOptimizationThreshold = bytes; | ||
| return copy; | ||
| } | ||
|
|
||
| /** | ||
| * 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 withBufferFlushThreshold(int bytes) | ||
| { | ||
| PackerConfig copy = clone(); | ||
| copy.bufferFlushThreshold = bytes; | ||
| return copy; | ||
| } | ||
|
|
||
| /** | ||
| * 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 withBufferSize(int bytes) | ||
| { | ||
| PackerConfig copy = clone(); | ||
| copy.bufferSize = bytes; | ||
| return copy; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public int stringDecoderBufferSize = 8192; | ||
| public int getBufferSize() | ||
| { | ||
| return bufferSize; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * MessageUnpacker configuration. | ||
| */ | ||
| public static class UnpackerConfig | ||
| implements Cloneable | ||
| { | ||
| 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; | ||
|
|
||
| private int stringDecoderBufferSize = 8192; | ||
|
|
||
| public UnpackerConfig() | ||
| { } | ||
|
|
||
| private UnpackerConfig(UnpackerConfig copy) | ||
| { | ||
| this.allowReadingStringAsBinary = copy.allowReadingStringAsBinary; | ||
| this.allowReadingBinaryAsString = copy.allowReadingBinaryAsString; | ||
| this.actionOnMalformedString = copy.actionOnMalformedString; | ||
| this.actionOnUnmappableString = copy.actionOnUnmappableString; | ||
| this.stringSizeLimit = copy.stringSizeLimit; | ||
| this.bufferSize = copy.bufferSize; | ||
| } | ||
|
|
||
| @Override | ||
| public UnpackerConfig clone() | ||
| { | ||
| return new UnpackerConfig(this); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test code to check the equality of config objects
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
| { | ||
| if (!(obj instanceof UnpackerConfig)) { | ||
| return false; | ||
| } | ||
| UnpackerConfig o = (UnpackerConfig) obj; | ||
| return this.allowReadingStringAsBinary == o.allowReadingStringAsBinary | ||
| && this.allowReadingBinaryAsString == o.allowReadingBinaryAsString | ||
| && this.actionOnMalformedString == o.actionOnMalformedString | ||
| && this.actionOnUnmappableString == o.actionOnUnmappableString | ||
| && this.stringSizeLimit == o.stringSizeLimit | ||
| && this.stringDecoderBufferSize == o.stringDecoderBufferSize | ||
| && this.bufferSize == o.bufferSize; | ||
| } | ||
|
|
||
| /** | ||
| * Create an unpacker that reads the data from a given input | ||
|
|
@@ -345,7 +436,7 @@ public MessageUnpacker newUnpacker(MessageBufferInput in) | |
| */ | ||
| public MessageUnpacker newUnpacker(InputStream in) | ||
| { | ||
| return newUnpacker(new InputStreamBufferInput(in)); | ||
| return newUnpacker(new InputStreamBufferInput(in, bufferSize)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -356,7 +447,7 @@ public MessageUnpacker newUnpacker(InputStream in) | |
| */ | ||
| public MessageUnpacker newUnpacker(ReadableByteChannel channel) | ||
| { | ||
| return newUnpacker(new ChannelBufferInput(channel)); | ||
| return newUnpacker(new ChannelBufferInput(channel, bufferSize)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -380,5 +471,111 @@ 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 withAllowReadingStringAsBinary(boolean enable) | ||
| { | ||
| UnpackerConfig copy = clone(); | ||
| copy.allowReadingStringAsBinary = enable; | ||
| return copy; | ||
| } | ||
|
|
||
| public boolean getAllowReadingStringAsBinary() | ||
| { | ||
| return allowReadingStringAsBinary; | ||
| } | ||
|
|
||
| /** | ||
| * Allow unpackString and unpackRawStringHeader and unpackString to read bin format family (default: true) | ||
| */ | ||
| public UnpackerConfig withAllowReadingBinaryAsString(boolean enable) | ||
| { | ||
| UnpackerConfig copy = clone(); | ||
| copy.allowReadingBinaryAsString = enable; | ||
| return copy; | ||
| } | ||
|
|
||
| public boolean getAllowReadingBinaryAsString() | ||
| { | ||
| return allowReadingBinaryAsString; | ||
| } | ||
|
|
||
| /** | ||
| * Action when encountered a malformed input (default: REPLACE) | ||
| */ | ||
| public UnpackerConfig withActionOnMalformedString(CodingErrorAction action) | ||
| { | ||
| UnpackerConfig copy = clone(); | ||
| copy.actionOnMalformedString = action; | ||
| return copy; | ||
| } | ||
|
|
||
| public CodingErrorAction getActionOnMalformedString() | ||
| { | ||
| return actionOnMalformedString; | ||
| } | ||
|
|
||
| /** | ||
| * Action when an unmappable character is found (default: REPLACE) | ||
| */ | ||
| public UnpackerConfig withActionOnUnmappableString(CodingErrorAction action) | ||
| { | ||
| UnpackerConfig copy = clone(); | ||
| copy.actionOnUnmappableString = action; | ||
| return copy; | ||
| } | ||
|
|
||
| public CodingErrorAction getActionOnUnmappableString() | ||
| { | ||
| return actionOnUnmappableString; | ||
| } | ||
|
|
||
| /** | ||
| * unpackString size limit (default: Integer.MAX_VALUE). | ||
| */ | ||
| public UnpackerConfig withStringSizeLimit(int bytes) | ||
| { | ||
| UnpackerConfig copy = clone(); | ||
| copy.stringSizeLimit = bytes; | ||
| return copy; | ||
| } | ||
|
|
||
| public int getStringSizeLimit() | ||
| { | ||
| return stringSizeLimit; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public UnpackerConfig withStringDecoderBufferSize(int bytes) | ||
| { | ||
| UnpackerConfig copy = clone(); | ||
| copy.stringDecoderBufferSize = bytes; | ||
| return copy; | ||
| } | ||
|
|
||
| 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 withBufferSize(int bytes) | ||
| { | ||
| UnpackerConfig copy = clone(); | ||
| copy.bufferSize = bytes; | ||
| return copy; | ||
| } | ||
|
|
||
| public int getBufferSize() | ||
| { | ||
| return bufferSize; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that Packer/UnpackerConfig objects are immutable, we can make them public final to remove redundant getter methods. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using getter is safer here because {Packer,Unpacker}Config are public API and change of the API means backward incompatibility. Exposing internal data structure seems negative for the future development.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we may need to use @deprecated annotation for the future config change. OK.