1919package jnr .enxio .channels ;
2020
2121import java .io .IOException ;
22+ import java .io .PrintWriter ;
23+ import java .io .StringWriter ;
2224import java .nio .ByteBuffer ;
2325import java .nio .channels .ByteChannel ;
2426import java .nio .channels .SocketChannel ;
2527import java .nio .channels .spi .SelectorProvider ;
2628
29+ import io .netty .buffer .ByteBufUtil ;
2730import jnr .constants .platform .Errno ;
2831import jnr .constants .platform .Shutdown ;
2932
@@ -96,6 +99,9 @@ public int read(ByteBuffer dst) throws IOException {
9699 }
97100
98101 public int write (ByteBuffer src ) throws IOException {
102+
103+ System .out .println ("write: " + hexDump (src , "" ));
104+
99105 int n = Native .write (fd , src );
100106 if (n < 0 ) {
101107 throw new IOException (Native .getLastErrorString ());
@@ -105,6 +111,7 @@ public int write(ByteBuffer src) throws IOException {
105111 }
106112
107113 public SocketChannel shutdownInput () throws IOException {
114+ System .out .println ("shutdownInput" );
108115 int n = Native .shutdown (fd , SHUT_RD );
109116 if (n < 0 ) {
110117 throw new IOException (Native .getLastErrorString ());
@@ -113,6 +120,7 @@ public SocketChannel shutdownInput() throws IOException {
113120 }
114121
115122 public SocketChannel shutdownOutput () throws IOException {
123+ System .out .println ("shutdownOutput" );
116124 int n = Native .shutdown (fd , SHUT_WR );
117125 if (n < 0 ) {
118126 throw new IOException (Native .getLastErrorString ());
@@ -122,4 +130,66 @@ public SocketChannel shutdownOutput() throws IOException {
122130
123131 private static final int SHUT_RD = Shutdown .SHUT_RD .intValue ();
124132 private static final int SHUT_WR = Shutdown .SHUT_WR .intValue ();
133+
134+ public static String hexDump (ByteBuffer buf , String prefix )
135+ {
136+ buf = buf .duplicate ();
137+ StringWriter str = new StringWriter ();
138+ PrintWriter out = new PrintWriter (str );
139+ int i = 0 ;
140+ int len = buf .remaining ();
141+ byte [] line = new byte [16 ];
142+ while (i < len )
143+ {
144+ if (prefix != null )
145+ out .print (prefix );
146+ out .print (formatInt (i , 16 , 8 ));
147+ out .print (" " );
148+ int l = Math .min (16 , len - i );
149+ buf .get (line , 0 , l );
150+ String s = toHexString (line , 0 , l , ' ' );
151+ out .print (s );
152+ for (int j = s .length (); j < 49 ; j ++)
153+ out .print (' ' );
154+ for (int j = 0 ; j < l ; j ++)
155+ {
156+ int c = line [j ] & 0xFF ;
157+ if (c < 0x20 || c > 0x7E )
158+ out .print ('.' );
159+ else
160+ out .print ((char ) c );
161+ }
162+ out .println ();
163+ i += 16 ;
164+ }
165+ return str .toString ();
166+ }
167+
168+ public static String formatInt (int i , int radix , int len )
169+ {
170+ String s = Integer .toString (i , radix );
171+ StringBuffer buf = new StringBuffer ();
172+ for (int j = 0 ; j < len - s .length (); j ++)
173+ buf .append ("0" );
174+ buf .append (s );
175+ return buf .toString ();
176+ }
177+
178+ public static String toHexString (byte [] buf , int off , int len , char sep )
179+ {
180+ StringBuffer str = new StringBuffer ();
181+ for (int i = 0 ; i < len ; i ++)
182+ {
183+ str .append (HEX .charAt (buf [i +off ] >>> 4 & 0x0F ));
184+ str .append (HEX .charAt (buf [i +off ] & 0x0F ));
185+ if (i < len - 1 )
186+ str .append (sep );
187+ }
188+ return str .toString ();
189+ }
190+
191+
192+ static final String HEX = "0123456789abcdef" ;
193+
194+
125195}
0 commit comments