NioPlainEngine implements NioFilter { @Override public ByteBuffer ensureWrappedCapacity(int amount, ByteBuffer wrappedBuffer, BufferPool.BufferType bufferType) { ByteBuffer buffer = wrappedBuffer; if (buffer == null) { buffer = bufferPool.acquireDirectBuffer(bufferType, amount); buffer.clear(); lastProcessedPosition = 0; lastReadPosition = 0; } else if (buffer.capacity() > amount) { if (buffer.capacity() - lastProcessedPosition < amount) { buffer.limit(lastReadPosition); buffer.position(lastProcessedPosition); buffer.compact(); lastReadPosition = buffer.position(); lastProcessedPosition = 0; } } else { ByteBuffer oldBuffer = buffer; oldBuffer.limit(lastReadPosition); oldBuffer.position(lastProcessedPosition); buffer = bufferPool.acquireDirectBuffer(bufferType, amount); buffer.clear(); buffer.put(oldBuffer); bufferPool.releaseBuffer(bufferType, oldBuffer); lastReadPosition = buffer.position(); lastProcessedPosition = 0; } return buffer; } NioPlainEngine(BufferPool bufferPool); @Override ByteBuffer wrap(ByteBuffer buffer); @Override ByteBuffer unwrap(ByteBuffer wrappedBuffer); @Override ByteBuffer ensureWrappedCapacity(int amount, ByteBuffer wrappedBuffer,
      BufferPool.BufferType bufferType); @Override ByteBuffer readAtLeast(SocketChannel channel, int bytes, ByteBuffer wrappedBuffer); void doneReading(ByteBuffer unwrappedBuffer); @Override ByteBuffer getUnwrappedBuffer(ByteBuffer wrappedBuffer);  }
@Test public void ensureWrappedCapacity() { ByteBuffer wrappedBuffer = bufferPool.acquireDirectReceiveBuffer(100); wrappedBuffer.put(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); nioEngine.lastReadPosition = 10; int requestedCapacity = 210; ByteBuffer result = nioEngine.ensureWrappedCapacity(requestedCapacity, wrappedBuffer, BufferPool.BufferType.TRACKED_RECEIVER); verify(mockStats, times(2)).incReceiverBufferSize(any(Integer.class), any(Boolean.class)); assertThat(result.capacity()).isGreaterThanOrEqualTo(requestedCapacity); assertThat(result).isGreaterThanOrEqualTo(wrappedBuffer); for (int i = 0; i < 10; i++) { assertThat(result.get(i)).isEqualTo(wrappedBuffer.get(i)); } }
@Test public void ensureWrappedCapacityWithEnoughExistingCapacityAndConsumedDataPresent() { int requestedCapacity = 210; final int consumedDataPresentInBuffer = 100; final int unconsumedDataPresentInBuffer = 10; ByteBuffer wrappedBuffer = ByteBuffer.allocate(requestedCapacity + unconsumedDataPresentInBuffer); wrappedBuffer.put(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); nioEngine.lastProcessedPosition = consumedDataPresentInBuffer; nioEngine.lastReadPosition = consumedDataPresentInBuffer + unconsumedDataPresentInBuffer; ByteBuffer result = wrappedBuffer = nioEngine.ensureWrappedCapacity(requestedCapacity, wrappedBuffer, BufferPool.BufferType.UNTRACKED); assertThat(result.capacity()).isEqualTo(requestedCapacity + unconsumedDataPresentInBuffer); assertThat(result).isSameAs(wrappedBuffer); for (int i = 0; i < 10; i++) { assertThat(result.get(i)).isEqualTo(wrappedBuffer.get(i)); } assertThat(nioEngine.lastProcessedPosition).isEqualTo(0); assertThat(nioEngine.lastReadPosition).isEqualTo(10); }