class PostgreSQLPacketCodecEngine implements DatabasePacketCodecEngine<PostgreSQLPacket> {

    @Override
    public void encode(final ChannelHandlerContext context, final PostgreSQLPacket message, final ByteBuf out) {
        try (PostgreSQLPacketPayload payload = new PostgreSQLPacketPayload(context.alloc().buffer())) {
            message.write(payload);
            if (!(message instanceof PostgreSQLSSLNegativePacket)) {
                out.writeByte(message.getMessageType());
                out.writeInt(payload.getByteBuf().readableBytes() + PostgreSQLPacket.PAYLOAD_LENGTH);
            }
            out.writeBytes(payload.getByteBuf());
        }
    }

}