LengthPrefixedCodec implements ProtocolCodec { public BufferState flush() throws IOException { final long writeCounterBeforeFlush = write_counter; while(!next_write_buffers.isEmpty()) { final ByteBuffer nextBuffer = next_write_buffers.peek(); if (nextBuffer.remaining() < 1) { next_write_buffers.remove(); continue; } int bytesWritten = write_channel.write(nextBuffer); write_counter += bytesWritten; next_write_size -= bytesWritten; if (nextBuffer.remaining() > 0) { break; } } if (empty()) { if (writeCounterBeforeFlush == write_counter) { return BufferState.WAS_EMPTY; } else { return BufferState.EMPTY; } } return BufferState.NOT_EMPTY; }  boolean full(); void setWritableByteChannel(WritableByteChannel channel); BufferState write(Object value); BufferState flush(); long getWriteCounter(); void setReadableByteChannel(ReadableByteChannel channel); Object read(); long getReadCounter();  }
@Test public void testFlush() throws Exception { final Buffer value = Buffer.ascii("TESTDATA"); codec.write(value); final int bytesThatWillBeWritten = value.length(); expect(writableByteChannel.write(anyObject())).andAnswer(createWriteAnswer(bytesThatWillBeWritten)); replay(writableByteChannel); final BufferState state = codec.flush(); assertEquals(BufferState.EMPTY, state); assertEquals(false, codec.full()); assertEquals(true, codec.empty()); assertEquals(bytesThatWillBeWritten, codec.getWriteCounter()); assertEquals(BufferState.WAS_EMPTY, codec.flush()); }