Issue #6166 - fix bug in the MessageInputStream byte array read

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2021-04-14 10:41:35 +10:00
parent 1e3f3a3cbb
commit bd79187194
2 changed files with 33 additions and 1 deletions

View File

@ -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

View File

@ -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
{