1 | /* |
2 | * $Id: DumpBytes.java,v 1.1 2005/11/30 06:58:48 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | package com.moesol.util; |
13 | |
14 | import java.io.PrintStream; |
15 | import java.io.PrintWriter; |
16 | import java.nio.ByteBuffer; |
17 | |
18 | /** |
19 | * Dump an array of bytes to output as hex and ascii. |
20 | * Since jSegue treats unsigned char as short to hold |
21 | * the range 0 - 255 dumpBytes also has a short[] form. |
22 | * |
23 | * @author Hastings |
24 | */ |
25 | public class DumpBytes { |
26 | public static void dumpBytes(PrintStream ps, byte[] b) { |
27 | PrintWriter pw = new PrintWriter(ps); |
28 | dumpBytes(pw, b); |
29 | pw.flush(); |
30 | } |
31 | public static void dumpBytes(PrintStream ps, short[] bytes) { |
32 | PrintWriter pw = new PrintWriter(ps); |
33 | dumpBytes(pw, bytes); |
34 | pw.flush(); |
35 | } |
36 | public static void dumpBytes(PrintWriter out, byte[] b) { |
37 | for (int i = 0; i < b.length; i += 16) { |
38 | dumpInt(out, i); |
39 | out.print(": "); |
40 | dumpHexRow(out, b, i); |
41 | } |
42 | } |
43 | public static void dumpBytes(PrintWriter out, ByteBuffer bb) { |
44 | for (int i = bb.position(); i < bb.limit(); i += 16) { |
45 | dumpInt(out, i); |
46 | out.print(": "); |
47 | dumpHexRow(out, bb, i); |
48 | } |
49 | } |
50 | public static void dumpBytes(PrintWriter out, short[] bytes) { |
51 | for (int i = 0; i < bytes.length; i += 16) { |
52 | dumpInt(out, i); |
53 | out.print(": "); |
54 | dumpHexRow(out, bytes, i); |
55 | } |
56 | } |
57 | |
58 | public static void dumpHexRow(PrintWriter out, byte[] b, int current) { |
59 | dumpHexRow(out, ByteBuffer.wrap(b), current); |
60 | } |
61 | |
62 | public static void dumpHexRow(PrintWriter out, ByteBuffer bb, int current) { |
63 | int num_bytes = Math.min(16, bb.limit() - current); |
64 | |
65 | for (int i = 0; i < num_bytes; i++) { |
66 | dumpByte(out, bb.get(current + i)); |
67 | out.print(' '); |
68 | } |
69 | for (int i = 0; i < num_bytes; i++) { |
70 | if (isPrintable(bb.get(current + i))) { |
71 | out.print( (char) bb.get(current + i)); |
72 | } else { |
73 | out.print('.'); |
74 | } |
75 | } |
76 | out.println(); |
77 | } |
78 | public static void dumpHexRow(PrintWriter out, short[] bytes, int current) { |
79 | int num_bytes = Math.min(16, bytes.length - current); |
80 | |
81 | for (int i = 0; i < num_bytes; i++) { |
82 | dumpByte(out, (byte)bytes[current + i]); |
83 | out.print(' '); |
84 | } |
85 | for (int i = 0; i < num_bytes; i++) { |
86 | if (isPrintable((byte)bytes[current + i])) { |
87 | out.print( (char) bytes[current + i]); |
88 | } else { |
89 | out.print('.'); |
90 | } |
91 | } |
92 | out.println(); |
93 | } |
94 | public static boolean isPrintable(byte v) { |
95 | char ch = (char)(v & 0xFF); |
96 | return (ch >= ' ' && ch <= '~'); |
97 | } |
98 | public static void dumpByte(PrintWriter out, byte v) { |
99 | dumpInt(out, v & 0xFF, 2); |
100 | } |
101 | public static void dumpByte(PrintStream ps, byte v) { |
102 | PrintWriter pw = new PrintWriter(ps); |
103 | dumpByte(pw, v); |
104 | pw.flush(); |
105 | } |
106 | public static void dumpInt(PrintWriter out, int v) { |
107 | dumpInt(out, v, 4); |
108 | } |
109 | public static void dumpInt(PrintWriter out, int v, int width) { |
110 | String s = Integer.toHexString(v); |
111 | while (s.length() < width) { |
112 | s = "0" + s; |
113 | } |
114 | out.print(s); |
115 | } |
116 | } |