Skip to content

Commit

Permalink
Fixed preserving contents of buffer when resized. (issue 11)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Mar 23, 2010
1 parent 197c95c commit 8569c86
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/com/esotericsoftware/kryo/ObjectBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void readToBuffer (InputStream input, int contentLength) {
int count = input.read(bytes, position, bytes.length - position);
if (count == -1) break;
position += count;
if (position >= bytes.length && !resizeBuffer(true)) throw new BufferOverflowException();
if (position == bytes.length && !resizeBuffer(true)) throw new BufferOverflowException();
}
buffer.position(0);
buffer.limit(position);
Expand Down Expand Up @@ -255,13 +255,15 @@ private byte[] writeToBytes () {
}

private boolean resizeBuffer (boolean preserveContents) {
int newCapacity = buffer.capacity() * 2;
if (newCapacity > maxCapacity) return false;
int capacity = buffer.capacity();
if (capacity == maxCapacity) return false;
int newCapacity = Math.min(maxCapacity, capacity * 2);

ByteBuffer newBuffer = ByteBuffer.allocate(newCapacity);
if (preserveContents) newBuffer.put(buffer);
byte[] newArray = newBuffer.array();
if (preserveContents) System.arraycopy(bytes, 0, newArray, 0, bytes.length);
buffer = newBuffer;
bytes = newBuffer.array();
bytes = newArray;

if (DEBUG) debug("kryo", "Resized ObjectBuffer to: " + newCapacity);
return true;
Expand Down

0 comments on commit 8569c86

Please sign in to comment.