### Question:
ByteBufferChannelProcessor extends CommonChannelProcessor { @Override protected void write(byte[] buf, int length) { if (length > buf.length) { throw new ArrayIndexOutOfBoundsException(length + " exceeds the size of the byte array " + buf.length); } int toCopy = length; if (internalBuffer == null) { internalBuffer = new byte[capacity << 1]; readPointer = 0; writePointer = 0; full = false; } if (freeSpace() < length) { throw new IllegalStateException("Internal buffer ran out of memory"); } int toTransfer = Math.min(length, internalBuffer.length - writePointer); System.arraycopy(buf, 0, internalBuffer, writePointer, toTransfer); writePointer = (writePointer + toTransfer) % internalBuffer.length; length -= toTransfer; if (length > 0) { System.arraycopy(buf, toTransfer, internalBuffer, writePointer, length); writePointer = (writePointer + length) % internalBuffer.length; } if (writePointer == readPointer && toCopy > 0) { full = true; } } ByteBufferChannelProcessor(int capacity);   }### Answer:
@Test public void randomPassthrough() { mainThread = Thread.currentThread(); ByteBuf buffer = Unpooled.buffer(2048); ChannelHandlerContext ctx = ChannelHandlerContextFaker.setup(); ByteBufferChannelProcessor processor = new ByteBufferChannelProcessor(256); byte[] input = new byte[LENGTH]; byte[] output = new byte[LENGTH]; Random r = new Random(); for (int i = 0; i < input.length; i++) { input[i] = (byte) (r.nextInt()); } int writePointer = 0; int readPointer = 0; int pass = 0; while (writePointer < LENGTH && (pass++) < 512) { int toWrite = r.nextInt(512); if (r.nextInt(10) == 0) { toWrite *= 10; } if (toWrite > buffer.writableBytes()) { toWrite = buffer.writableBytes(); } if (toWrite > LENGTH - writePointer) { toWrite = LENGTH - writePointer; } System.out.println("Writing block of size " + toWrite); buffer.writeBytes(input, writePointer, toWrite); writePointer += toWrite; ByteBuf outputBuffer = processor.write(ctx, buffer); buffer.discardReadBytes(); while (outputBuffer.isReadable()) { int toRead = r.nextInt(768); if (toRead > outputBuffer.readableBytes()) { toRead = outputBuffer.readableBytes(); } System.out.println("ToRead: " + toRead + " of " + outputBuffer.readableBytes()); outputBuffer.readBytes(output, readPointer, toRead); readPointer += toRead; outputBuffer.discardReadBytes(); } outputBuffer.release(); } buffer.release(); for (int i = 0; i < input.length; i++) { assertTrue("Mismatch at position " + i, input[i] == output[i]); } }