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 edd449b3..4e25adc0 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessagePack.java @@ -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) { 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 ff638b74..6093bace 100644 --- a/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java +++ b/msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java @@ -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; @@ -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(); } /** @@ -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) { + throw new MessageSizeException(String.format("%,d exceeds bufferSize:%,d", length, bufferSize), length); + } + byte[] newArray = new byte[length]; readPayload(newArray); return newArray;