KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > util > HexDump


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.util;
18
19 import java.io.PrintStream JavaDoc;
20
21 /**
22  * Hex dump class.
23  */

24 public final class HexDump
25 {
26
27     /**
28      * Hex dump a byte array
29      *
30      * @param byt Byte array to dump
31      * @param len Length of data to dump
32      * @param offset Offset to start data dump
33      */

34
35     public static final void Dump(byte[] byt, int len, int offset)
36     {
37         Dump(byt, len, offset, System.out);
38     }
39
40     /**
41      * Hex dump a byte array
42      *
43      * @param byt Byte array to dump
44      * @param len Length of data to dump
45      * @param offset Offset to start data dump
46      * @param stream Output stream to dump the output to.
47      */

48
49     public static final void Dump(byte[] byt, int len, int offset, PrintStream JavaDoc stream)
50     {
51
52         // Create buffers for the ASCII and Hex output
53

54         StringBuffer JavaDoc ascBuf = new StringBuffer JavaDoc();
55         StringBuffer JavaDoc hexBuf = new StringBuffer JavaDoc();
56
57         // Dump 16 byte blocks from the array until the length has been
58
// reached
59

60         int dlen = 0;
61         int doff = offset;
62         String JavaDoc posStr = null;
63
64         while (dlen < len)
65         {
66
67             // Reset the ASCII/Hex buffers
68

69             ascBuf.setLength(0);
70             hexBuf.setLength(0);
71
72             posStr = generatePositionString(doff);
73
74             // Dump a block of data, update the data offset
75

76             doff = generateLine(byt, doff, ascBuf, hexBuf);
77
78             // Output the current record
79

80             stream.print(posStr);
81             stream.print(hexBuf.toString());
82             stream.println(ascBuf.toString());
83
84             // Update the dump length
85

86             dlen += 16;
87         }
88     }
89
90     /**
91      * Generate a hex string for the specified string
92      *
93      * @param str String
94      * @return String
95      */

96     public static final String JavaDoc hexString(String JavaDoc str)
97     {
98         if (str != null)
99             return hexString(str.getBytes());
100         return "";
101     }
102
103     /**
104      * Generate a hex string for the specified string
105      *
106      * @param str String
107      * @param gap String
108      * @return String
109      */

110     public static final String JavaDoc hexString(String JavaDoc str, String JavaDoc gap)
111     {
112         if (str != null)
113             return hexString(str.getBytes(), gap);
114         return "";
115     }
116
117     /**
118      * Generate a hex string for the specified bytes
119      *
120      * @param buf byte[]
121      * @return String
122      */

123     public static final String JavaDoc hexString(byte[] buf)
124     {
125         return hexString(buf, buf.length, null);
126     }
127
128     /**
129      * Generate a hex string for the specified bytes
130      *
131      * @param buf byte[]
132      * @param gap String
133      * @return String
134      */

135     public static final String JavaDoc hexString(byte[] buf, String JavaDoc gap)
136     {
137         return hexString(buf, buf.length, gap);
138     }
139
140     /**
141      * Generate a hex string for the specified bytes
142      *
143      * @param buf byte[]
144      * @param len int
145      * @param gap String
146      * @return String
147      */

148     public static final String JavaDoc hexString(byte[] buf, int len, String JavaDoc gap)
149     {
150
151         // Check if the buffer is valid
152

153         if (buf == null)
154             return "";
155
156         // Create a string buffer for the hex string
157

158         int buflen = buf.length * 2;
159         if (gap != null)
160             buflen += buf.length * gap.length();
161
162         StringBuffer JavaDoc hex = new StringBuffer JavaDoc(buflen);
163
164         // Convert the bytes to hex-ASCII
165

166         for (int i = 0; i < len; i++)
167         {
168
169             // Get the current byte
170

171             int curbyt = (int) (buf[i] & 0x00FF);
172
173             // Output the hex string
174

175             hex.append(Integer.toHexString((curbyt & 0xF0) >> 4));
176             hex.append(Integer.toHexString(curbyt & 0x0F));
177
178             // Add the gap string, if specified
179

180             if (gap != null && i < (len - 1))
181                 hex.append(gap);
182         }
183
184         // Return the hex-ASCII string
185

186         return hex.toString();
187     }
188
189     /**
190      * Generate a buffer position string
191      *
192      * @param off int
193      * @return String
194      */

195     private static final String JavaDoc generatePositionString(int off)
196     {
197
198         // Create a buffer position string
199

200         StringBuffer JavaDoc posStr = new StringBuffer JavaDoc("" + off + " - ");
201         while (posStr.length() < 8)
202             posStr.insert(0, " ");
203
204         // Return the string
205

206         return posStr.toString();
207     }
208
209     /**
210      * Output a single line of the hex dump to a debug device
211      *
212      * @param byt Byte array to dump
213      * @param off Offset to start data dump
214      * @param ascBuf Buffer for ASCII output
215      * @param hexBuf Buffer for Hex output
216      * @return New offset value
217      */

218
219     private static final int generateLine(byte[] byt, int off, StringBuffer JavaDoc ascBuf, StringBuffer JavaDoc hexBuf)
220     {
221
222         // Check if there is enough buffer space to dump 16 bytes
223

224         int dumplen = byt.length - off;
225         if (dumplen > 16)
226             dumplen = 16;
227
228         // Dump a 16 byte block of data
229

230         for (int i = 0; i < dumplen; i++)
231         {
232
233             // Get the current byte
234

235             int curbyt = (int) (byt[off++] & 0x00FF);
236
237             // Output the hex string
238

239             hexBuf.append(Integer.toHexString((curbyt & 0xF0) >> 4));
240             hexBuf.append(Integer.toHexString(curbyt & 0x0F));
241             hexBuf.append(" ");
242
243             // Output the character equivalent, if printable
244

245             if (Character.isLetterOrDigit((char) curbyt) || Character.getType((char) curbyt) != Character.CONTROL)
246                 ascBuf.append((char) curbyt);
247             else
248                 ascBuf.append(".");
249         }
250
251         // Output the hex dump line
252

253         hexBuf.append(" - ");
254
255         // Return the new data offset
256

257         return off;
258     }
259 }
Popular Tags