class RELPEncoder {

    public byte[] encode(final RELPFrame frame) {
        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        // write transaction number followed by separator
        byte[] txnr = String.format("%s", frame.getTxnr()).getBytes(charset);
        buffer.write(txnr, 0, txnr.length);
        buffer.write(RELPFrame.SEPARATOR);

        // write the command followed by separator
        byte[] command = frame.getCommand().getBytes(charset);
        buffer.write(command, 0, command.length);
        buffer.write(RELPFrame.SEPARATOR);

        // write the data length
        byte[] dataLength = String.format("%s", frame.getDataLength()).getBytes(charset);
        buffer.write(dataLength, 0, dataLength.length);

        // if data to write then put a separator and write the data
        if (frame.getDataLength() > 0) {
            buffer.write(RELPFrame.SEPARATOR);
            buffer.write(frame.getData(), 0, frame.getDataLength());
        }

        // write the end of the frame
        buffer.write(RELPFrame.DELIMITER);

        return buffer.toByteArray();
    }

    public  RELPEncoder(final Charset charset);

}