1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package davmail.ldap;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.InputStream;
32 import java.io.PrintStream;
33 import java.io.OutputStream;
34 import java.io.IOException;
35 import java.nio.ByteBuffer;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class HexDumpEncoder {
52
53 private int offset;
54 private int thisLineLength;
55 private int currentByte;
56 private byte[] thisLine = new byte[16];
57
58 static void hexDigit(PrintStream p, byte x) {
59 char c;
60
61 c = (char) ((x >> 4) & 0xf);
62 if (c > 9)
63 c = (char) ((c-10) + 'A');
64 else
65 c = (char)(c + '0');
66 p.write(c);
67 c = (char) (x & 0xf);
68 if (c > 9)
69 c = (char)((c-10) + 'A');
70 else
71 c = (char)(c + '0');
72 p.write(c);
73 }
74
75 protected int bytesPerAtom() {
76 return (1);
77 }
78
79 protected int bytesPerLine() {
80 return (16);
81 }
82
83 protected void encodeBufferPrefix(OutputStream o) {
84 offset = 0;
85 pStream = new PrintStream(o);
86 }
87
88 protected void encodeLinePrefix(int len) {
89 hexDigit(pStream, (byte)((offset >>> 8) & 0xff));
90 hexDigit(pStream, (byte)(offset & 0xff));
91 pStream.print(": ");
92 currentByte = 0;
93 thisLineLength = len;
94 }
95
96 protected void encodeAtom(byte[] buf, int off) {
97 thisLine[currentByte] = buf[off];
98 hexDigit(pStream, buf[off]);
99 pStream.print(" ");
100 currentByte++;
101 if (currentByte == 8)
102 pStream.print(" ");
103 }
104
105 protected void encodeLineSuffix() {
106 if (thisLineLength < 16) {
107 for (int i = thisLineLength; i < 16; i++) {
108 pStream.print(" ");
109 if (i == 7)
110 pStream.print(" ");
111 }
112 }
113 pStream.print(" ");
114 for (int i = 0; i < thisLineLength; i++) {
115 if ((thisLine[i] < ' ') || (thisLine[i] > 'z')) {
116 pStream.print(".");
117 } else {
118 pStream.write(thisLine[i]);
119 }
120 }
121 pStream.println();
122 offset += thisLineLength;
123 }
124
125
126 protected PrintStream pStream;
127
128
129
130
131
132 protected int readFully(InputStream in, byte[] buffer)
133 throws java.io.IOException {
134 for (int i = 0; i < buffer.length; i++) {
135 int q = in.read();
136 if (q == -1)
137 return i;
138 buffer[i] = (byte)q;
139 }
140 return buffer.length;
141 }
142
143
144
145
146
147
148
149 public void encode(InputStream inStream, OutputStream outStream)
150 throws IOException
151 {
152 int j;
153 int numBytes;
154 byte[] tmpbuffer = new byte[bytesPerLine()];
155
156 encodeBufferPrefix(outStream);
157
158 while (true) {
159 numBytes = readFully(inStream, tmpbuffer);
160 if (numBytes == 0) {
161 break;
162 }
163 encodeLinePrefix(numBytes);
164 for (j = 0; j < numBytes; j += bytesPerAtom()) {
165 encodeAtom(tmpbuffer, j);
166 }
167 if (numBytes < bytesPerLine()) {
168 break;
169 } else {
170 encodeLineSuffix();
171 }
172 }
173 }
174
175
176
177
178
179 public String encode(byte[] aBuffer) {
180 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
181 ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
182 String retVal;
183 try {
184 encode(inStream, outStream);
185
186 retVal = outStream.toString("ISO-8859-1");
187 } catch (Exception IOException) {
188
189 throw new Error("CharacterEncoder.encode internal error");
190 }
191 return (retVal);
192 }
193
194
195
196
197
198
199
200
201
202
203 private byte [] getBytes(ByteBuffer bb) {
204
205
206
207
208 byte [] buf = null;
209
210
211
212
213
214 if (bb.hasArray()) {
215 byte [] tmp = bb.array();
216 if ((tmp.length == bb.capacity()) &&
217 (tmp.length == bb.remaining())) {
218 buf = tmp;
219 bb.position(bb.limit());
220 }
221 }
222
223 if (buf == null) {
224
225
226
227
228
229 buf = new byte[bb.remaining()];
230
231
232
233
234 bb.get(buf);
235 }
236
237 return buf;
238 }
239
240
241
242
243
244
245
246 public String encode(ByteBuffer aBuffer) {
247 byte [] buf = getBytes(aBuffer);
248 return encode(buf);
249 }
250
251
252
253
254
255
256
257 public void encodeBuffer(InputStream inStream, OutputStream outStream)
258 throws IOException
259 {
260 int j;
261 int numBytes;
262 byte[] tmpbuffer = new byte[bytesPerLine()];
263
264 encodeBufferPrefix(outStream);
265
266 while (true) {
267 numBytes = readFully(inStream, tmpbuffer);
268 if (numBytes == 0) {
269 break;
270 }
271 encodeLinePrefix(numBytes);
272 for (j = 0; j < numBytes; j += bytesPerAtom()) {
273 encodeAtom(tmpbuffer, j);
274 }
275 encodeLineSuffix();
276 if (numBytes < bytesPerLine()) {
277 break;
278 }
279 }
280 }
281
282
283
284
285
286 public void encodeBuffer(byte[] aBuffer, OutputStream aStream)
287 throws IOException
288 {
289 ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
290 encodeBuffer(inStream, aStream);
291 }
292
293
294
295
296
297 @SuppressWarnings("unused")
298 public String encodeBuffer(byte[] aBuffer) {
299 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
300 ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
301 try {
302 encodeBuffer(inStream, outStream);
303 } catch (Exception IOException) {
304
305 throw new Error("CharacterEncoder.encodeBuffer internal error");
306 }
307 return (outStream.toString());
308 }
309
310
311
312
313
314
315
316 @SuppressWarnings("unused")
317 public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream)
318 throws IOException
319 {
320 byte [] buf = getBytes(aBuffer);
321 encodeBuffer(buf, aStream);
322 }
323
324 }