@Test public final void testEnlarge() { ReusableBuffer[] buffers = new ReusableBuffer[] { ReusableBuffer.wrap("Yagga Yagga".getBytes()), BufferPool.allocate(11) }; for (ReusableBuffer buf : buffers) { int originalBufSize = buf.capacity(); assertFalse(buf.enlarge(buf.capacityUnderlying() + 1)); assertTrue(buf.enlarge(originalBufSize)); assertEquals(originalBufSize, buf.capacity()); buf.limit(originalBufSize / 2); ReusableBuffer smallerBuf = buf.createViewBuffer(); int newBufSize = smallerBuf.capacity() * 2; assertTrue(smallerBuf.enlarge(newBufSize)); assertEquals(0, buf.position()); assertEquals(newBufSize, smallerBuf.limit()); assertEquals(newBufSize, smallerBuf.capacity()); assertEquals(newBufSize, smallerBuf.remaining()); assertFalse(smallerBuf.enlarge(buf.capacityUnderlying() + 1)); assertEquals(0, buf.position()); assertEquals(originalBufSize / 2, buf.limit()); assertEquals(originalBufSize, buf.capacity()); assertEquals(originalBufSize / 2, buf.remaining()); if (buf.isReusable()) { BufferPool.free(buf); } } }
public boolean enlarge(int newSize) { assert (!returned) : "Buffer was already freed and cannot be used anymore" + this.freeStack; if (newSize == this.size) { return true; } if (parentBuffer == null && viewParent == null) { return false; } ByteBuffer underlyingBuffer = parentBuffer != null ? parentBuffer : viewParent.getBuffer(); if (newSize > underlyingBuffer.capacity()) { return false; } else { this.size = newSize; int oldPos = buffer.position(); if (oldPos > newSize) oldPos = 0; int position = underlyingBuffer.position(); int limit = underlyingBuffer.limit(); underlyingBuffer.position(0); underlyingBuffer.limit(newSize); this.buffer = underlyingBuffer.slice(); buffer.position(oldPos); underlyingBuffer.position(position); underlyingBuffer.limit(limit); return true; } }
ReusableBuffer { public boolean enlarge(int newSize) { assert (!returned) : "Buffer was already freed and cannot be used anymore" + this.freeStack; if (newSize == this.size) { return true; } if (parentBuffer == null && viewParent == null) { return false; } ByteBuffer underlyingBuffer = parentBuffer != null ? parentBuffer : viewParent.getBuffer(); if (newSize > underlyingBuffer.capacity()) { return false; } else { this.size = newSize; int oldPos = buffer.position(); if (oldPos > newSize) oldPos = 0; int position = underlyingBuffer.position(); int limit = underlyingBuffer.limit(); underlyingBuffer.position(0); underlyingBuffer.limit(newSize); this.buffer = underlyingBuffer.slice(); buffer.position(oldPos); underlyingBuffer.position(position); underlyingBuffer.limit(limit); return true; } } }
ReusableBuffer { public boolean enlarge(int newSize) { assert (!returned) : "Buffer was already freed and cannot be used anymore" + this.freeStack; if (newSize == this.size) { return true; } if (parentBuffer == null && viewParent == null) { return false; } ByteBuffer underlyingBuffer = parentBuffer != null ? parentBuffer : viewParent.getBuffer(); if (newSize > underlyingBuffer.capacity()) { return false; } else { this.size = newSize; int oldPos = buffer.position(); if (oldPos > newSize) oldPos = 0; int position = underlyingBuffer.position(); int limit = underlyingBuffer.limit(); underlyingBuffer.position(0); underlyingBuffer.limit(newSize); this.buffer = underlyingBuffer.slice(); buffer.position(oldPos); underlyingBuffer.position(position); underlyingBuffer.limit(limit); return true; } } protected  ReusableBuffer(ByteBuffer buffer, int size);  ReusableBuffer(ByteBuffer nonManaged); }
ReusableBuffer { public boolean enlarge(int newSize) { assert (!returned) : "Buffer was already freed and cannot be used anymore" + this.freeStack; if (newSize == this.size) { return true; } if (parentBuffer == null && viewParent == null) { return false; } ByteBuffer underlyingBuffer = parentBuffer != null ? parentBuffer : viewParent.getBuffer(); if (newSize > underlyingBuffer.capacity()) { return false; } else { this.size = newSize; int oldPos = buffer.position(); if (oldPos > newSize) oldPos = 0; int position = underlyingBuffer.position(); int limit = underlyingBuffer.limit(); underlyingBuffer.position(0); underlyingBuffer.limit(newSize); this.buffer = underlyingBuffer.slice(); buffer.position(oldPos); underlyingBuffer.position(position); underlyingBuffer.limit(limit); return true; } } protected  ReusableBuffer(ByteBuffer buffer, int size);  ReusableBuffer(ByteBuffer nonManaged); static ReusableBuffer wrap(byte[] data); static ReusableBuffer wrap(byte[] data, int offset, int length); ReusableBuffer createViewBuffer(); int capacity(); int capacityUnderlying(); boolean hasArray(); byte[] array(); void flip(); void compact(); void limit(int l); int limit(); void position(int p); int position(); boolean hasRemaining(); ByteBuffer getBuffer(); boolean isReusable(); byte get(); byte get(int index); ReusableBuffer get(byte[] dst); ReusableBuffer get(byte[] dst, int offset, int length); ReusableBuffer put(byte b); ReusableBuffer put(byte[] src); ReusableBuffer put(byte[] src, int offset, int len); ReusableBuffer put(ByteBuffer src); ReusableBuffer put(ReusableBuffer src); int getInt(); ReusableBuffer putInt(int i); long getLong(); ReusableBuffer putLong(long l); double getDouble(); ReusableBuffer putDouble(double d); String getString(); ReusableBuffer putString(String str); ReusableBuffer putShortString(String str); ASCIIString getBufferBackedASCIIString(); ReusableBuffer putBufferBackedASCIIString(ASCIIString str); ReusableBuffer putShort(short s); short getShort(); boolean isDirect(); int remaining(); void clear(); byte[] getData(); void shrink(int newSize); boolean enlarge(int newSize); void range(int offset, int length); ReusableBuffer putBoolean(boolean bool); boolean getBoolean(); int getRefCount(); @Override String toString(); }
ReusableBuffer { public boolean enlarge(int newSize) { assert (!returned) : "Buffer was already freed and cannot be used anymore" + this.freeStack; if (newSize == this.size) { return true; } if (parentBuffer == null && viewParent == null) { return false; } ByteBuffer underlyingBuffer = parentBuffer != null ? parentBuffer : viewParent.getBuffer(); if (newSize > underlyingBuffer.capacity()) { return false; } else { this.size = newSize; int oldPos = buffer.position(); if (oldPos > newSize) oldPos = 0; int position = underlyingBuffer.position(); int limit = underlyingBuffer.limit(); underlyingBuffer.position(0); underlyingBuffer.limit(newSize); this.buffer = underlyingBuffer.slice(); buffer.position(oldPos); underlyingBuffer.position(position); underlyingBuffer.limit(limit); return true; } } protected  ReusableBuffer(ByteBuffer buffer, int size);  ReusableBuffer(ByteBuffer nonManaged); static ReusableBuffer wrap(byte[] data); static ReusableBuffer wrap(byte[] data, int offset, int length); ReusableBuffer createViewBuffer(); int capacity(); int capacityUnderlying(); boolean hasArray(); byte[] array(); void flip(); void compact(); void limit(int l); int limit(); void position(int p); int position(); boolean hasRemaining(); ByteBuffer getBuffer(); boolean isReusable(); byte get(); byte get(int index); ReusableBuffer get(byte[] dst); ReusableBuffer get(byte[] dst, int offset, int length); ReusableBuffer put(byte b); ReusableBuffer put(byte[] src); ReusableBuffer put(byte[] src, int offset, int len); ReusableBuffer put(ByteBuffer src); ReusableBuffer put(ReusableBuffer src); int getInt(); ReusableBuffer putInt(int i); long getLong(); ReusableBuffer putLong(long l); double getDouble(); ReusableBuffer putDouble(double d); String getString(); ReusableBuffer putString(String str); ReusableBuffer putShortString(String str); ASCIIString getBufferBackedASCIIString(); ReusableBuffer putBufferBackedASCIIString(ASCIIString str); ReusableBuffer putShort(short s); short getShort(); boolean isDirect(); int remaining(); void clear(); byte[] getData(); void shrink(int newSize); boolean enlarge(int newSize); void range(int offset, int length); ReusableBuffer putBoolean(boolean bool); boolean getBoolean(); int getRefCount(); @Override String toString();  }