ThreadLocalByteBufferPool implements ByteBufferPool { @Override public ByteBuffer borrow(final int size, boolean zeroed) { final int requiredCapacity = PowerOf2Util.align(size, Env.osPageSize()); ByteBuffer byteBuffer = bytesPool.get(); if (byteBuffer == null || requiredCapacity > byteBuffer.capacity()) { byteBuffer = direct ? ByteBuffer.allocateDirect(requiredCapacity) : ByteBuffer.allocate(requiredCapacity); } else { bytesPool.set(null); if (zeroed) { ByteUtil.zeros(byteBuffer, 0, size); } byteBuffer.clear(); } byteBuffer.limit(size); return byteBuffer; } ThreadLocalByteBufferPool(boolean direct); @Override ByteBuffer borrow(final int size, boolean zeroed); @Override void release(ByteBuffer buffer);  }
@Test public void shouldBorrowOnlyBuffersOfTheCorrectType() { Assert.assertEquals(isDirect, pool.borrow(0, zeroed).isDirect()); }
@Test public void shouldBorrowZeroedBuffer() { final int size = 32; final ByteBuffer buffer = pool.borrow(size, zeroed); Assert.assertEquals(0, buffer.position()); Assert.assertEquals(size, buffer.limit()); if (zeroed) { assertZeroed(buffer); } }