1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.common;
20
21 import org.apache.mina.common.ByteBuffer;
22
23 /***
24 * Provides utility methods for ByteBuffers.
25 *
26 * @author Trustin Lee (trustin@apache.org)
27 * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $
28 */
29 class ByteBufferHexDumper
30 {
31 private static final byte[] highDigits;
32
33 private static final byte[] lowDigits;
34
35
36 static
37 {
38 final byte[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
39 '9', 'A', 'B', 'C', 'D', 'E', 'F' };
40
41 int i;
42 byte[] high = new byte[ 256 ];
43 byte[] low = new byte[ 256 ];
44
45 for( i = 0; i < 256; i++ )
46 {
47 high[ i ] = digits[ i >>> 4 ];
48 low[ i ] = digits[ i & 0x0F ];
49 }
50
51 highDigits = high;
52 lowDigits = low;
53 }
54
55 static String getHexdump( ByteBuffer in )
56 {
57 int size = in.remaining();
58
59 if( size == 0 )
60 {
61 return "empty";
62 }
63
64 StringBuffer out = new StringBuffer( ( in.remaining() * 3 ) - 1 );
65
66 int mark = in.position();
67
68
69 int byteValue = in.get() & 0xFF;
70 out.append( ( char ) highDigits[ byteValue ] );
71 out.append( ( char ) lowDigits[ byteValue ] );
72 size--;
73
74
75 for( ; size > 0; size-- )
76 {
77 out.append( ' ' );
78 byteValue = in.get() & 0xFF;
79 out.append( ( char ) highDigits[ byteValue ] );
80 out.append( ( char ) lowDigits[ byteValue ] );
81 }
82
83 in.position( mark );
84
85 return out.toString();
86 }
87 }