Skip to content

Commit

Permalink
Fix growing ByteBufferOutput while writing varint
Browse files Browse the repository at this point in the history
Growing the ByteBuffer while writing varInt or varLong will change the byte order first, and then perform the require.

If the ByteBuffer needs to grow the setBuffer will again change the ByteOrder, but this time the one set by the varInt method. Thus the final ByteOrder was incorrect.
  • Loading branch information
Andre Hilsendeger authored and Andre Hilsendeger committed Oct 6, 2016
1 parent 6b8cb36 commit e721a44
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/com/esotericsoftware/kryo/io/ByteBufferOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ protected boolean require (int required) throws KryoException {
niobuffer.limit(position);
newBuffer.put(niobuffer);
newBuffer.order(niobuffer.order());

// writeVarInt & writeVarLong mess with the byte order. need to keep track of the current byte order when growing
final ByteOrder currentByteOrder = byteOrder;
setBuffer(newBuffer, maxCapacity);
byteOrder = currentByteOrder;
}
return true;
}
Expand Down
7 changes: 7 additions & 0 deletions test/com/esotericsoftware/kryo/ByteBufferInputOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,11 @@ public void testByteBufferOutputSetOrder() {
assertEquals(ByteOrder.LITTLE_ENDIAN, outputBuffer.order());
assertEquals(ByteOrder.LITTLE_ENDIAN, outputBuffer.getByteBuffer().order());
}

public void testByteBufferByteOrderTheSameAfterGrowingForVarInt() {
final ByteBufferOutput outputBuffer = new ByteBufferOutput(1, -1);
final ByteOrder byteOrder = outputBuffer.order();
outputBuffer.writeVarInt(300, true);
assertEquals(byteOrder, outputBuffer.order());
}
}

0 comments on commit e721a44

Please sign in to comment.