Issue #6166 - fix bug in the MessageInputStream byte array read
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
parent
1e3f3a3cbb
commit
bd79187194
|
@ -95,7 +95,9 @@ public class MessageInputStream extends InputStream implements MessageSink
|
|||
@Override
|
||||
public int read(final byte[] b, final int off, final int len) throws IOException
|
||||
{
|
||||
return read(ByteBuffer.wrap(b, off, len).flip());
|
||||
ByteBuffer buffer = ByteBuffer.wrap(b, off, len).slice();
|
||||
BufferUtil.clear(buffer);
|
||||
return read(buffer);
|
||||
}
|
||||
|
||||
public int read(ByteBuffer buffer) throws IOException
|
||||
|
|
|
@ -69,6 +69,36 @@ public class MessageInputStreamTest
|
|||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleReadsIntoSingleByteArray() throws IOException
|
||||
{
|
||||
try (MessageInputStream stream = new MessageInputStream())
|
||||
{
|
||||
// Append a single message (simple, short)
|
||||
Frame frame = new Frame(OpCode.TEXT);
|
||||
frame.setPayload("Hello World");
|
||||
frame.setFin(true);
|
||||
stream.accept(frame, Callback.NOOP);
|
||||
|
||||
// Read entire message it from the stream.
|
||||
byte[] bytes = new byte[100];
|
||||
|
||||
int read = stream.read(bytes, 0, 6);
|
||||
assertThat(read, is(6));
|
||||
|
||||
read = stream.read(bytes, 6, 10);
|
||||
assertThat(read, is(5));
|
||||
|
||||
read = stream.read(bytes, 11, 10);
|
||||
assertThat(read, is(-1));
|
||||
|
||||
String message = new String(bytes, 0, 11, StandardCharsets.UTF_8);
|
||||
|
||||
// Test it
|
||||
assertThat("Message", message, is("Hello World"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlockOnRead() throws Exception
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue