Skip to content

Commit

Permalink
Fixed #248. There was a bug in the buffer resizing code.
Browse files Browse the repository at this point in the history
  • Loading branch information
romix committed Sep 29, 2014
1 parent a306471 commit 23830f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/com/esotericsoftware/kryo/io/ByteBufferOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ public ByteBuffer getByteBuffer () {
/** Returns a new byte array containing the bytes currently in the buffer between zero and {@link #position()}. */
public byte[] toBytes () {
byte[] newBuffer = new byte[position];
niobuffer.position(position);
niobuffer.position(0);
niobuffer.get(newBuffer, 0, position);
return newBuffer;
Expand Down Expand Up @@ -195,8 +194,9 @@ protected boolean require (int required) throws KryoException {
.allocateDirect(capacity);
// Copy the whole buffer
niobuffer.position(0);
niobuffer.limit(position);
newBuffer.put(niobuffer);
newBuffer.order(byteOrder);
newBuffer.order(niobuffer.order());
niobuffer = newBuffer;
}
return true;
Expand Down
15 changes: 15 additions & 0 deletions test/com/esotericsoftware/kryo/InputOutputTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,21 @@ public void testSmallBuffers () throws Exception {

input.readBytes(toRead);
}

public void testVerySmallBuffers() throws Exception {
Output out1 = new Output(4, -1);
Output out2 = new ByteBufferOutput(4, -1);

for (int i = 0; i < 16; i++) {
out1.writeVarInt(92, false);
}

for (int i = 0; i < 16; i++) {
out2.writeVarInt(92, false);
}

assertEquals(out1.toBytes(), out2.toBytes());
}

public void testZeroLengthOutputs() throws Exception {
Output output = new Output(0, 10000);
Expand Down

0 comments on commit 23830f6

Please sign in to comment.