1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.mina.util;
20
21 import java.nio.ByteBuffer;
22
23
24
25
26
27
28 public class ByteBufferDumper {
29
30 private static final byte[] HEX_CHAR = new byte[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
31 'C', 'D', 'E', 'F' };
32
33
34
35
36
37
38
39
40
41
42 public static String dump(ByteBuffer buffer, int nbBytes, boolean toAscii) {
43 byte data[];
44 int start;
45 int size = Math.min(buffer.remaining(), nbBytes >= 0 ? nbBytes : Integer.MAX_VALUE);
46 int length = buffer.remaining();
47
48 if (buffer.hasArray() && !buffer.isReadOnly()) {
49 start = buffer.position();
50 data = buffer.array();
51 } else {
52 data = new byte[size];
53
54 int oldpos = buffer.position();
55 buffer.get(data);
56 buffer.position(oldpos);
57
58 start = 0;
59 length = data.length;
60 }
61
62
63 boolean binaryContent = false;
64
65 if (toAscii) {
66 for (int i = start; i < start + size; i++) {
67 byte b = data[i];
68
69 if (((b < 32) || (b > 126)) && (b != 13) && (b != 10)) {
70 binaryContent = true;
71 break;
72 }
73 }
74 }
75
76 if (!toAscii || binaryContent) {
77 StringBuilder out = new StringBuilder(size * 3 + 30);
78 out.append("ByteBuffer[len=").append(length).append(",bytes='");
79
80
81 int byteValue = data[start] & 0xFF;
82 boolean isFirst = true;
83
84
85 for (int i = start; i < start + size; i++) {
86 if (isFirst) {
87 isFirst = false;
88 } else {
89 out.append(' ');
90 }
91
92 byteValue = data[i] & 0xFF;
93 out.append(new String(new byte[] { '0', 'x', HEX_CHAR[(byteValue & 0x00F0) >> 4],
94 HEX_CHAR[byteValue & 0x000F] }));
95 }
96
97 out.append("']");
98
99 return out.toString();
100
101 } else {
102 StringBuilder sb = new StringBuilder(size);
103 sb.append("ByteBuffer[len=").append(length).append(",str='").append(new String(data, start, size))
104 .append("']");
105
106 return sb.toString();
107 }
108 }
109
110
111
112
113
114
115
116 public static String dump(ByteBuffer buffer) {
117 return dump(buffer, -1, true);
118 }
119
120
121
122
123
124
125
126 public static String toHex(ByteBuffer buffer) {
127 StringBuilder out = new StringBuilder(buffer.remaining() * 2);
128 int pos = buffer.position();
129 while (buffer.hasRemaining()) {
130 int byteValue = buffer.get() & 0xFF;
131 out.append((char) (HEX_CHAR[(byteValue & 0x00F0) >> 4]))
132 .append((char) ((HEX_CHAR[byteValue & 0x000F]) - 0));
133 }
134 buffer.position(pos);
135 return out.toString();
136 }
137
138 public static ByteBuffer fromHexString(String hex) {
139 if (hex.length() % 2 != 0) {
140 throw new IllegalArgumentException("the hexa-decimal string length cannot be odd");
141 }
142 int size = hex.length() / 2;
143 ByteBuffer res = ByteBuffer.allocate(size);
144
145 for (int i = 0; i < size; i++) {
146 int b = ((Character.digit(hex.charAt(i * 2), 16) << 4) | (Character.digit(hex.charAt(i * 2 + 1), 16)));
147 res.put((byte) b);
148 }
149
150 res.flip();
151 return res;
152 }
153 }