1 package it.imolinfo.jbi4cics.typemapping.cobol;
2
3
4
5
6
7
8
9
10
11
12
13 import it.imolinfo.jbi4cics.Logger;
14 import it.imolinfo.jbi4cics.LoggerFactory;
15 import it.imolinfo.jbi4cics.jbi.Messages;
16 import java.io.BufferedInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.io.PrintStream;
24 import java.text.DecimalFormat;
25
26
27
28
29
30
31
32
33
34
35 public class HexDump {
36
37
38
39
40 private static final Logger LOG
41 = LoggerFactory.getLogger(HexDump.class);
42
43
44
45
46 private static final Messages MESSAGES
47 = Messages.getMessages(HexDump.class);
48
49 public static final String EOL =
50 System.getProperty("line.separator");
51
52
53 private static final char HEXCODES[] =
54 {
55 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
56 'E', 'F'
57 };
58 private static final int SHIFTS[] =
59 {
60 60, 56, 52, 48, 44, 40, 36, 32, 28, 24, 20, 16, 12, 8, 4, 0
61 };
62
63
64
65 private HexDump()
66 {
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public synchronized static void dump(final byte [] data, final long offset,
87 final OutputStream stream, final int index, final int length)
88 throws IOException, ArrayIndexOutOfBoundsException,
89 IllegalArgumentException
90 {
91 if (data.length == 0)
92 {
93 stream.write( ("No Data" + System.getProperty( "line.separator")).getBytes() );
94 stream.flush();
95 return;
96 }
97 if ((index < 0) || (index >= data.length))
98 {
99 LOG.error("CIC002112_Illegal_index", index, data.length);
100 throw new ArrayIndexOutOfBoundsException(MESSAGES.getString(
101 "CIC002112_Illegal_index", index, data.length));
102 }
103 if (stream == null)
104 {
105 LOG.error("CIC002113_Cannot_write_nullstream");
106 throw new IllegalArgumentException(MESSAGES.getString(
107 "CIC002113_Cannot_write_nullstream"));
108 }
109
110 long display_offset = offset + index;
111 StringBuffer buffer = new StringBuffer(74);
112
113
114 int data_length = Math.min(data.length,index+length);
115 for (int j = index; j < data_length; j += 16)
116 {
117 int chars_read = data_length - j;
118
119 if (chars_read > 16)
120 {
121 chars_read = 16;
122 }
123 buffer.append(
124 dump(display_offset)
125 ).append(' ');
126 for (int k = 0; k < 16; k++)
127 {
128 if (k < chars_read)
129 {
130 buffer.append(dump(data[ k + j ]));
131 }
132 else
133 {
134 buffer.append(" ");
135 }
136 buffer.append(' ');
137 }
138 for (int k = 0; k < chars_read; k++)
139 {
140 if ((data[ k + j ] >= ' ') && (data[ k + j ] < 127))
141 {
142 buffer.append(( char ) data[ k + j ]);
143 }
144 else
145 {
146 buffer.append('.');
147 }
148 }
149 buffer.append(EOL);
150 stream.write(buffer.toString().getBytes());
151 stream.flush();
152 buffer.setLength(0);
153 display_offset += chars_read;
154 }
155
156 }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 public synchronized static void dump(final byte [] data, final long offset,
176 final OutputStream stream, final int index)
177 throws IOException, ArrayIndexOutOfBoundsException,
178 IllegalArgumentException
179 {
180 dump(data, offset, stream, index, data.length-index);
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public static String dump(final byte [] data, final long offset,
196 final int index) {
197 StringBuffer buffer;
198 if ((index < 0) || (index >= data.length))
199 {
200 LOG.error("CIC002112_Illegal_index", index, data.length);
201 throw new ArrayIndexOutOfBoundsException(MESSAGES.getString(
202 "CIC002112_Illegal_index", index, data.length));
203 }
204 long display_offset = offset + index;
205 buffer = new StringBuffer(74);
206
207 for (int j = index; j < data.length; j += 16)
208 {
209 int chars_read = data.length - j;
210
211 if (chars_read > 16)
212 {
213 chars_read = 16;
214 }
215 buffer.append(dump(display_offset)).append(' ');
216 for (int k = 0; k < 16; k++)
217 {
218 if (k < chars_read)
219 {
220 buffer.append(dump(data[ k + j ]));
221 }
222 else
223 {
224 buffer.append(" ");
225 }
226 buffer.append(' ');
227 }
228 for (int k = 0; k < chars_read; k++)
229 {
230 if ((data[ k + j ] >= ' ') && (data[ k + j ] < 127))
231 {
232 buffer.append(( char ) data[ k + j ]);
233 }
234 else
235 {
236 buffer.append('.');
237 }
238 }
239 buffer.append(EOL);
240 display_offset += chars_read;
241 }
242 return buffer.toString();
243 }
244
245
246 private static String dump(final long value)
247 {
248 StringBuffer buf = new StringBuffer();
249 buf.setLength(0);
250 for (int j = 0; j < 8; j++)
251 {
252 buf.append( HEXCODES[ (( int ) (value >> SHIFTS[ j + SHIFTS.length - 8 ])) & 15 ]);
253 }
254 return buf.toString();
255 }
256
257 private static String dump(final byte value)
258 {
259 StringBuffer buf = new StringBuffer();
260 buf.setLength(0);
261 for (int j = 0; j < 2; j++)
262 {
263 buf.append(HEXCODES[ (value >> SHIFTS[ j + 6 ]) & 15 ]);
264 }
265 return buf.toString();
266 }
267
268
269
270
271
272
273
274 public static String toHex(final byte[] value)
275 {
276 StringBuffer retVal = new StringBuffer();
277 retVal.append('[');
278 for(int x = 0; x < value.length; x++)
279 {
280 retVal.append("0x").append(toHex(value[x]));
281 if (value.length-x>1) {
282 retVal.append(", ");
283 }
284 }
285 retVal.append(']');
286 return retVal.toString();
287 }
288
289
290
291
292
293
294
295
296
297
298 public static String toHex(final byte[] value, final int bytesPerLine)
299 {
300 final int digits =
301 (int) Math.round(Math.log(value.length) / Math.log(10) + 0.5);
302 final StringBuffer formatString = new StringBuffer();
303 for (int i = 0; i < digits; i++){
304 formatString.append('0');
305 }
306 formatString.append(": ");
307 final DecimalFormat format = new DecimalFormat(formatString.toString());
308 StringBuffer retVal = new StringBuffer();
309 retVal.append(format.format(0));
310 int i = -1;
311 for(int x = 0; x < value.length; x++)
312 {
313 if (++i == bytesPerLine)
314 {
315 retVal.append('\n');
316 retVal.append(format.format(x));
317 i = 0;
318 }
319 retVal.append(toHex(value[x]));
320 retVal.append(", ");
321 }
322 return retVal.toString();
323 }
324
325
326
327
328
329
330
331 public static String toHex(final short value)
332 {
333 return toHex(value, 4);
334 }
335
336
337
338
339
340
341
342 public static String toHex(final byte value)
343 {
344 return toHex(value, 2);
345 }
346
347
348
349
350
351
352
353 public static String toHex(final int value)
354 {
355 return toHex(value, 8);
356 }
357
358
359
360
361
362
363
364 public static String toHex(final long value)
365 {
366 return toHex(value, 16);
367 }
368
369
370 private static String toHex(final long value, final int digits)
371 {
372 StringBuffer result = new StringBuffer(digits);
373 for (int j = 0; j < digits; j++)
374 {
375 result.append( HEXCODES[ (int) ((value >> SHIFTS[ j + (16 - digits) ]) & 15)]);
376 }
377 return result.toString();
378 }
379
380
381
382
383
384
385
386
387
388
389 public static void dump( InputStream in, PrintStream out, int start, int bytesToDump ) throws IOException
390 {
391 ByteArrayOutputStream buf = new ByteArrayOutputStream();
392 if (bytesToDump == -1)
393 {
394 int c = in.read();
395 while (c != -1)
396 {
397 buf.write(c);
398 c = in.read();
399 }
400 }
401 else
402 {
403 int bytesRemaining = bytesToDump;
404 while (bytesRemaining-- > 0)
405 {
406 int c = in.read();
407 if (c == -1){
408 break;
409 }
410 else {
411 buf.write(c);
412 }
413 }
414 }
415
416 byte[] data = buf.toByteArray();
417 dump(data, 0, out, start, data.length);
418 }
419
420
421
422
423
424
425 public static void main(String[] args) throws Exception {
426 File file = new File(args[0]);
427 InputStream in = new BufferedInputStream(new FileInputStream(file));
428 byte[] b = new byte[(int)file.length()];
429 in.read(b);
430 LOG.debug(HexDump.dump(b, 0, 0));
431 in.close();
432 }
433 }