PostgreSQLPacketCodecEngine implements DatabasePacketCodecEngine<PostgreSQLPacket> { @Override public void decode(final ChannelHandlerContext context, final ByteBuf in, final List<Object> out, final int readableBytes) { int messageTypeLength = 0; if ('\0' == in.markReaderIndex().readByte()) { in.resetReaderIndex(); } else { messageTypeLength = PostgreSQLPacket.MESSAGE_TYPE_LENGTH; } int payloadLength = in.readInt(); int realPacketLength = payloadLength + messageTypeLength; if (readableBytes < realPacketLength) { in.resetReaderIndex(); return; } in.resetReaderIndex(); out.add(in.readRetainedSlice(payloadLength + messageTypeLength)); }  @Override boolean isValidHeader(final int readableBytes); @Override void decode(final ChannelHandlerContext context, final ByteBuf in, final List<Object> out, final int readableBytes); @Override void encode(final ChannelHandlerContext context, final PostgreSQLPacket message, final ByteBuf out); @Override PostgreSQLPacketPayload createPacketPayload(final ByteBuf message);  }
@Test public void assertDecode() { when(byteBuf.markReaderIndex()).thenReturn(byteBuf); when(byteBuf.readInt()).thenReturn(50); List<Object> out = new LinkedList<>(); new PostgreSQLPacketCodecEngine().decode(context, byteBuf, out, 54); assertThat(out.size(), is(1)); }
@Test public void assertDecodeWithStickyPacket() { when(byteBuf.markReaderIndex()).thenReturn(byteBuf); when(byteBuf.readInt()).thenReturn(50); List<Object> out = new LinkedList<>(); new PostgreSQLPacketCodecEngine().decode(context, byteBuf, out, 40); assertTrue(out.isEmpty()); }