Skip to content

Commit

Permalink
serialization improvements: fix ByteBasedInputStream
Browse files Browse the repository at this point in the history
Not all buffers start at zero.

Signed-off-by: Erik Escher <eclipse@erikescher.de>
  • Loading branch information
erikescher committed Jan 17, 2020
1 parent c470af6 commit 187c593
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ private ByteBufferInputStream(ByteBuffer byteBuffer){
}

static InputStream of(ByteBuffer byteBuffer){
if (byteBuffer.hasArray()){
return new ByteArrayInputStream(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
if (byteBuffer.hasArray()) {
return new ByteArrayInputStream(
byteBuffer.array(),
byteBuffer.arrayOffset() + byteBuffer.position(),
byteBuffer.limit());
}
return new ByteBufferInputStream(byteBuffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class ByteBufferInputStreamTest {
for (int i = 0; i < length; i++) {
buffer.put((byte) i);
}
buffer.flip();
final InputStream stream = ByteBufferInputStream.of(buffer);
for (int i = 0; i < length; i++) {
assertThat(stream.read()).isEqualTo(i);
Expand All @@ -48,6 +49,7 @@ public class ByteBufferInputStreamTest {
for (int i = 0; i < totalLength; i++) {
buffer.put(TESTVALUE);
}
buffer.flip();
final InputStream stream = ByteBufferInputStream.of(buffer);

byte[] bytes = new byte[totalLength];
Expand Down

0 comments on commit 187c593

Please sign in to comment.