### Question:
LineReader { public void decode(ByteBuf in, List<Object> out, ByteBufAllocator allocator) { while (in.isReadable()) { final int startIndex = in.readerIndex(); int lastReadIndex = in.forEachByte(LINE_END_FINDER); if (-1 == lastReadIndex) { if (null == incompleteBuffer) { incompleteBuffer = allocator.buffer(DEFAULT_INITIAL_CAPACITY, maxLineLength); } incompleteBuffer.ensureWritable(in.readableBytes()); incompleteBuffer.writeBytes(in); } else { ByteBuf lineBuf = in.readSlice(lastReadIndex - startIndex); String line; if (null != incompleteBuffer) { line = incompleteBuffer.toString(encoding) + lineBuf.toString(encoding); incompleteBuffer.release(); incompleteBuffer = null; } else { line = lineBuf.toString(encoding); } out.add(line); in.skipBytes(1); } } } LineReader();  LineReader(int maxLineLength, Charset encoding); void decode(ByteBuf in, List<Object> out, ByteBufAllocator allocator); void decodeLast(ByteBuf in, List<Object> out, ByteBufAllocator allocator); void dispose(); static boolean isLineDelimiter(char c); static final int DEFAULT_INITIAL_CAPACITY; }### Answer:
@Test(timeout = 60000) public void testSingleLine() throws Exception { String msg = "Hello"; ByteBuf data = Unpooled.buffer().writeBytes((msg + "\n").getBytes()); List<Object> out = new ArrayList<>(); readerRule.lineReader.decode(data, out, UnpooledByteBufAllocator.DEFAULT); assertThat("Unexpected output size.", out, hasSize(1)); assertThat("Unexpected output message.", out, contains((Object) msg)); assertThat("Input buffer not consumed.", data.isReadable(), is(false)); }
@Test(timeout = 60000) public void testEmptyInputBuffer() throws Exception { ByteBuf data = Unpooled.buffer(); List<Object> out = new ArrayList<>(); readerRule.lineReader.decode(data, out, UnpooledByteBufAllocator.DEFAULT); assertThat("Unexpected output size.", out, is(empty())); assertThat("Input buffer not consumed.", data.isReadable(), is(false)); }
@Test(timeout = 60000) public void testEmptyLine() throws Exception { ByteBuf data = Unpooled.buffer().writeByte('\n'); List<Object> out = new ArrayList<>(); readerRule.lineReader.decode(data, out, UnpooledByteBufAllocator.DEFAULT); assertThat("Unexpected output size.", out, hasSize(1)); assertThat("Unexpected output message.", out, contains((Object)"")); assertThat("Input buffer not consumed.", data.isReadable(), is(false)); }
@Test(timeout = 60000) public void testSplitData() throws Exception { String msg1 = "Hell"; String msg2 = "o"; ByteBuf data1 = Unpooled.buffer().writeBytes((msg1.getBytes())); ByteBuf data2 = Unpooled.buffer().writeBytes((msg2 + "\n").getBytes()); List<Object> out = new ArrayList<>(); readerRule.lineReader.decode(data1, out, UnpooledByteBufAllocator.DEFAULT); assertThat("Unexpected output size post first decode.", out, is(empty())); assertThat("Input buffer not consumed.", data1.isReadable(), is(false)); readerRule.lineReader.decode(data2, out, UnpooledByteBufAllocator.DEFAULT); assertThat("Unexpected output size post second decode.", out, hasSize(1)); assertThat("Unexpected output message post second decode.", out, contains((Object) (msg1 + msg2))); assertThat("Input buffer not consumed.", data2.isReadable(), is(false)); }