Skip to content
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

Fixed UnpackerConfig.bufferSize to work #658

Closed
wants to merge 2 commits into from
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
4 changes: 2 additions & 2 deletions msgpack-core/src/main/java/org/msgpack/core/MessagePack.java
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,8 @@ public int getStringDecoderBufferSize()
}

/**
* When a packer is created with newUnpacker(OutputStream) or newUnpacker(WritableByteChannel), the stream will be
* buffered with this size of buffer (default: 8192).
* When a packer is created with {@link #newUnpacker(InputStream)} or {@link #newUnpacker(ReadableByteChannel)},
* the stream will be buffered with this size of buffer (default: 8192).
*/
public UnpackerConfig withBufferSize(int bytes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public class MessageUnpacker
private final CodingErrorAction actionOnUnmappableString;
private final int stringSizeLimit;
private final int stringDecoderBufferSize;
private final int bufferSize;

private MessageBufferInput in;

Expand Down Expand Up @@ -215,6 +216,7 @@ protected MessageUnpacker(MessageBufferInput in, MessagePack.UnpackerConfig conf
this.actionOnUnmappableString = config.getActionOnUnmappableString();
this.stringSizeLimit = config.getStringSizeLimit();
this.stringDecoderBufferSize = config.getStringDecoderBufferSize();
this.bufferSize = config.getBufferSize();
}

/**
Expand Down Expand Up @@ -1635,11 +1637,16 @@ public void readPayload(byte[] dst)
*
* @param length number of bytes to be read
* @return the new byte array
* @throws MessageSizeException when length is larger than bufferSize
* @throws IOException when underlying input throws IOException
*/
public byte[] readPayload(int length)
throws IOException
{
if (length > bufferSize) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part is a good catch. If that happens, instead of throwing an error, we need to enlarge the buffer size by creating a new buffer large enough for storing the expected data size.

This implementation would be similar to ArrayList implementation of Java.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry that I don't quite understand here.
Am I correct that the purpose of UnpackerConfig.bufferSize is to limit the size of the new byte array in readPayload(int)?
Do I need to change this code?

throw new MessageSizeException(String.format("%,d exceeds bufferSize:%,d", length, bufferSize), length);
}

byte[] newArray = new byte[length];
readPayload(newArray);
return newArray;
Expand Down