5
5
import io .netty .buffer .Unpooled ;
6
6
import io .netty .channel .ChannelHandlerContext ;
7
7
import io .netty .handler .codec .ByteToMessageCodec ;
8
- import java .io .ByteArrayOutputStream ;
9
8
import java .io .IOException ;
10
9
import java .nio .charset .StandardCharsets ;
11
10
import java .util .List ;
12
- import org .apache .commons .io .HexDump ;
13
11
import org .slf4j .Logger ;
14
12
import org .slf4j .LoggerFactory ;
15
13
@@ -27,9 +25,9 @@ public BinaryHandler(HomekitClientConnection connection) {
27
25
@ Override
28
26
protected void encode (ChannelHandlerContext ctx , ByteBuf msg , ByteBuf out ) throws Exception {
29
27
if (started ) {
28
+ debugData ("Sending data" , msg , ctx );
30
29
byte [] b = new byte [msg .readableBytes ()];
31
30
msg .readBytes (b );
32
- traceData ("Sending data" , b , ctx );
33
31
out .writeBytes (connection .encryptResponse (b ));
34
32
} else {
35
33
out .writeBytes (msg );
@@ -41,8 +39,9 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
41
39
byte [] b = new byte [in .readableBytes ()];
42
40
in .readBytes (b );
43
41
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 );
46
45
started = true ;
47
46
}
48
47
@@ -57,18 +56,12 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
57
56
super .exceptionCaught (ctx , cause );
58
57
}
59
58
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 )));
72
65
}
73
66
}
74
67
}
0 commit comments