55import io .netty .buffer .Unpooled ;
66import io .netty .channel .ChannelHandlerContext ;
77import io .netty .handler .codec .ByteToMessageCodec ;
8- import java .io .ByteArrayOutputStream ;
98import java .io .IOException ;
109import java .nio .charset .StandardCharsets ;
1110import java .util .List ;
12- import org .apache .commons .io .HexDump ;
1311import org .slf4j .Logger ;
1412import org .slf4j .LoggerFactory ;
1513
@@ -27,9 +25,9 @@ public BinaryHandler(HomekitClientConnection connection) {
2725 @ Override
2826 protected void encode (ChannelHandlerContext ctx , ByteBuf msg , ByteBuf out ) throws Exception {
2927 if (started ) {
28+ debugData ("Sending data" , msg , ctx );
3029 byte [] b = new byte [msg .readableBytes ()];
3130 msg .readBytes (b );
32- traceData ("Sending data" , b , ctx );
3331 out .writeBytes (connection .encryptResponse (b ));
3432 } else {
3533 out .writeBytes (msg );
@@ -41,8 +39,9 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
4139 byte [] b = new byte [in .readableBytes ()];
4240 in .readBytes (b );
4341 byte [] decrypted = connection .decryptRequest (b );
44- traceData ("Received data" , decrypted , ctx );
45- out .add (Unpooled .copiedBuffer (decrypted ));
42+ ByteBuf outBuf = Unpooled .copiedBuffer (decrypted );
43+ debugData ("Received data" , outBuf , ctx );
44+ out .add (outBuf );
4645 started = true ;
4746 }
4847
@@ -57,18 +56,12 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
5756 super .exceptionCaught (ctx , cause );
5857 }
5958
60- private void traceData (String msg , byte [] b , ChannelHandlerContext ctx ) throws Exception {
61- if (logger .isTraceEnabled () && b .length > 0 ) {
62- try (ByteArrayOutputStream stream = new ByteArrayOutputStream ()) {
63- HexDump .dump (b , 0 , stream , 0 );
64- stream .flush ();
65- logger .trace (
66- String .format (
67- "%s [%s]:%n%s%n" ,
68- msg ,
69- ctx .channel ().remoteAddress ().toString (),
70- stream .toString (StandardCharsets .UTF_8 .name ())));
71- }
59+ private void debugData (String msg , ByteBuf b , ChannelHandlerContext ctx ) throws Exception {
60+ if (logger .isDebugEnabled ()) {
61+ logger .debug (
62+ String .format (
63+ "%s [%s]:%n%s" ,
64+ msg , ctx .channel ().remoteAddress ().toString (), b .toString (StandardCharsets .UTF_8 )));
7265 }
7366 }
7467}
0 commit comments