### Question:
EchoFilter implements TCPProxyFilter { @Override public byte[] handle(ConnectionDetails connectionDetails, byte[] buffer, int bytesRead) throws FilterException { final StringBuilder stringBuffer = new StringBuilder(); boolean inHex = false; for (int i = 0; i < bytesRead; i++) { final int value = buffer[i] & 0xFF; if (value == '\r' || value == '\n' || (value >= ' ' && value <= '~')) { if (inHex) { stringBuffer.append(']'); inHex = false; } stringBuffer.append((char)value); } else { if (!inHex) { stringBuffer.append('['); inHex = true; } if (value <= 0xf) { stringBuffer.append("0"); } stringBuffer.append(Integer.toHexString(value).toUpperCase()); } } m_out.println("------ " + connectionDetails + " ------"); m_out.println(stringBuffer); return null; } EchoFilter(PrintWriter output); @Override byte[] handle(ConnectionDetails connectionDetails,
                                 byte[] buffer,
                                 int bytesRead); @Override void connectionOpened(ConnectionDetails connectionDetails); @Override void connectionClosed(ConnectionDetails connectionDetails);  }### Answer:
@Test public void testHandle() throws Exception { final EchoFilter echoFilter = new EchoFilter(m_out); assertEquals(0, m_outString.toString().length()); echoFilter.handle(m_connectionDetails, "This is a campaign.".getBytes(), 5); final String output = m_outString.toString(); assertContains(output, m_connectionDetails.toString()); assertContains(output, "This " + LINE_SEPARATOR); final String lines = "Some\nlines\rblah"; echoFilter.handle(m_connectionDetails, lines.getBytes(), lines.length()); final String output2 = m_outString.toString().substring(output.length()); assertContains(output2, m_connectionDetails.toString()); assertContains(output2, "Some\nlines\rblah" + LINE_SEPARATOR); final byte[] binary = { 0x01, (byte)0xE7, 'a', 'b', 'c', (byte)0x89, 'd', 'a', 'h', '\n', 'b', 'a', 'h' }; echoFilter.handle(m_connectionDetails, binary, binary.length); final String output3 = m_outString.toString().substring(output.length() + output2.length()); assertContains(output3, m_connectionDetails.toString()); assertContains(output3, "[01E7]abc[89]dah\nbah" + LINE_SEPARATOR); }