@Override public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent e) throws Exception { super.messageReceived(ctx, e); final byte[] packet = getBufferBytes(e); this.output.write( joiner.join( "\"" + timeFormatter.print(new DateTime()) + "\"", "\"" + StringUtils.replaceNonPrintableAsciiCharacters(new String(packet)) + "\"", "\"" + StringUtils.toHexString(packet) + "\"", Long.toString(System.currentTimeMillis() / 1000))); output.newLine(); output.flush(); }
/** * Returns a printable (ASCII) String by constructing a new String of maximum length {@code * maxLength} and calling {@link StringUtils#replaceNonPrintableAsciiCharacters(String)} on it. * * @param buffer the buffer to convert * @param maxLength the maximum length of the input String for {@link * StringUtils#replaceNonPrintableAsciiCharacters(String)} * @return a printable String */ public static String toPrintableString(final ChannelBuffer buffer, int maxLength) { int actualLength = buffer.readableBytes() < maxLength ? buffer.readableBytes() : maxLength; final byte[] bytes = new byte[actualLength]; buffer.getBytes(buffer.readerIndex(), bytes, 0, actualLength); return StringUtils.replaceNonPrintableAsciiCharacters(new String(bytes)); }