### Question:
RELPEncoder { public byte[] encode(final RELPFrame frame) { final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] txnr = String.format("%s", frame.getTxnr()).getBytes(charset); buffer.write(txnr, 0, txnr.length); buffer.write(RELPFrame.SEPARATOR); byte[] command = frame.getCommand().getBytes(charset); buffer.write(command, 0, command.length); buffer.write(RELPFrame.SEPARATOR); byte[] dataLength = String.format("%s", frame.getDataLength()).getBytes(charset); buffer.write(dataLength, 0, dataLength.length); if (frame.getDataLength() > 0) { buffer.write(RELPFrame.SEPARATOR); buffer.write(frame.getData(), 0, frame.getDataLength()); } buffer.write(RELPFrame.DELIMITER); return buffer.toByteArray(); } RELPEncoder(final Charset charset); Charset getCharset(); byte[] encode(final RELPFrame frame);  }### Answer:
@Test public void testEncodingWithData() throws IOException { final RELPFrame frame = new RELPFrame.Builder() .txnr(1) .command("rsp") .dataLength(5) .data("12345".getBytes(StandardCharsets.UTF_8)) .build(); final RELPEncoder encoder = new RELPEncoder(StandardCharsets.UTF_8); final byte[] result = encoder.encode(frame); final String expected = "1 rsp 5 12345\n"; Assert.assertEquals(expected, new String(result, StandardCharsets.UTF_8)); }
@Test public void testEncodingNoData() throws IOException { final RELPFrame frame = new RELPFrame.Builder() .txnr(1) .command("rsp") .dataLength(0) .data(new byte[0]) .build(); final RELPEncoder encoder = new RELPEncoder(StandardCharsets.UTF_8); final byte[] result = encoder.encode(frame); final String expected = "1 rsp 0\n"; Assert.assertEquals(expected, new String(result, StandardCharsets.UTF_8)); }
@Test public void testEncodingOpenResponse() { final String openFrameData = "relp_version=0\nrelp_software=librelp,1.2.7,http: final String openFrame = "1 open 85 " + openFrameData + "\n"; System.out.println(openFrame); final RELPDecoder decoder = new RELPDecoder(StandardCharsets.UTF_8); final RELPEncoder encoder = new RELPEncoder(StandardCharsets.UTF_8); RELPFrame frame = null; for (byte b : openFrame.getBytes(StandardCharsets.UTF_8)) { if (decoder.process(b)) { frame = decoder.getFrame(); break; } } Assert.assertNotNull(frame); final Map<String,String> offers = RELPResponse.parseOffers(frame.getData(), StandardCharsets.UTF_8); final RELPFrame responseFrame = RELPResponse.open(frame.getTxnr(), offers).toFrame(StandardCharsets.UTF_8); final byte[] response = encoder.encode(responseFrame); System.out.println(new String(response, StandardCharsets.UTF_8)); }