KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > HexDump


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.util;
20
21 import java.io.*;
22 import java.text.DecimalFormat JavaDoc;
23
24 /**
25  * dump data in hexadecimal format; derived from a HexDump utility I
26  * wrote in June 2001.
27  *
28  * @author Marc Johnson
29  * @author Glen Stampoultzis (glens at apache.org)
30  */

31
32 public class HexDump
33 {
34     public static final String JavaDoc EOL =
35         System.getProperty("line.separator");
36 // private static final StringBuffer _lbuffer = new StringBuffer(8);
37
// private static final StringBuffer _cbuffer = new StringBuffer(2);
38
private static final char _hexcodes[] =
39     {
40         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
41         'E', 'F'
42     };
43     private static final int _shifts[] =
44     {
45         60, 56, 52, 48, 44, 40, 36, 32, 28, 24, 20, 16, 12, 8, 4, 0
46     };
47
48
49     // all static methods, so no need for a public constructor
50
private HexDump()
51     {
52     }
53
54     /**
55      * dump an array of bytes to an OutputStream
56      *
57      * @param data the byte array to be dumped
58      * @param offset its offset, whatever that might mean
59      * @param stream the OutputStream to which the data is to be
60      * written
61      * @param index initial index into the byte array
62      * @param length number of characters to output
63      *
64      * @exception IOException is thrown if anything goes wrong writing
65      * the data to stream
66      * @exception ArrayIndexOutOfBoundsException if the index is
67      * outside the data array's bounds
68      * @exception IllegalArgumentException if the output stream is
69      * null
70      */

71     public synchronized static void dump(final byte [] data, final long offset,
72                             final OutputStream stream, final int index, final int length)
73             throws IOException, ArrayIndexOutOfBoundsException JavaDoc,
74                     IllegalArgumentException JavaDoc
75     {
76         if (data.length == 0)
77         {
78             stream.write( "No Data".getBytes() );
79             stream.flush();
80             return;
81         }
82         if ((index < 0) || (index >= data.length))
83         {
84             throw new ArrayIndexOutOfBoundsException JavaDoc(
85                 "illegal index: " + index + " into array of length "
86                 + data.length);
87         }
88         if (stream == null)
89         {
90             throw new IllegalArgumentException JavaDoc("cannot write to nullstream");
91         }
92
93         long display_offset = offset + index;
94         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(74);
95
96
97         int data_length = Math.min(data.length,index+length);
98         for (int j = index; j < data_length; j += 16)
99         {
100             int chars_read = data_length - j;
101
102             if (chars_read > 16)
103             {
104                 chars_read = 16;
105             }
106             buffer.append(
107                         dump(display_offset)
108                          ).append(' ');
109             for (int k = 0; k < 16; k++)
110             {
111                 if (k < chars_read)
112                 {
113                     buffer.append(dump(data[ k + j ]));
114                 }
115                 else
116                 {
117                     buffer.append(" ");
118                 }
119                 buffer.append(' ');
120             }
121             for (int k = 0; k < chars_read; k++)
122             {
123                 if ((data[ k + j ] >= ' ') && (data[ k + j ] < 127))
124                 {
125                     buffer.append(( char ) data[ k + j ]);
126                 }
127                 else
128                 {
129                     buffer.append('.');
130                 }
131             }
132             buffer.append(EOL);
133             stream.write(buffer.toString().getBytes());
134             stream.flush();
135             buffer.setLength(0);
136             display_offset += chars_read;
137         }
138
139     }
140
141     /**
142      * dump an array of bytes to an OutputStream
143      *
144      * @param data the byte array to be dumped
145      * @param offset its offset, whatever that might mean
146      * @param stream the OutputStream to which the data is to be
147      * written
148      * @param index initial index into the byte array
149      *
150      * @exception IOException is thrown if anything goes wrong writing
151      * the data to stream
152      * @exception ArrayIndexOutOfBoundsException if the index is
153      * outside the data array's bounds
154      * @exception IllegalArgumentException if the output stream is
155      * null
156      */

157
158     public synchronized static void dump(final byte [] data, final long offset,
159                             final OutputStream stream, final int index)
160         throws IOException, ArrayIndexOutOfBoundsException JavaDoc,
161                 IllegalArgumentException JavaDoc
162     {
163         dump(data, offset, stream, index, data.length-index);
164     }
165
166     /**
167      * dump an array of bytes to a String
168      *
169      * @param data the byte array to be dumped
170      * @param offset its offset, whatever that might mean
171      * @param index initial index into the byte array
172      *
173      * @exception ArrayIndexOutOfBoundsException if the index is
174      * outside the data array's bounds
175      * @return output string
176      */

177     
178     public static String JavaDoc dump(final byte [] data, final long offset,
179                             final int index) {
180         StringBuffer JavaDoc buffer;
181         if ((index < 0) || (index >= data.length))
182         {
183             throw new ArrayIndexOutOfBoundsException JavaDoc(
184                 "illegal index: " + index + " into array of length "
185                 + data.length);
186         }
187         long display_offset = offset + index;
188         buffer = new StringBuffer JavaDoc(74);
189
190         for (int j = index; j < data.length; j += 16)
191         {
192             int chars_read = data.length - j;
193
194             if (chars_read > 16)
195             {
196                 chars_read = 16;
197             }
198             buffer.append(dump(display_offset)).append(' ');
199             for (int k = 0; k < 16; k++)
200             {
201                 if (k < chars_read)
202                 {
203                     buffer.append(dump(data[ k + j ]));
204                 }
205                 else
206                 {
207                     buffer.append(" ");
208                 }
209                 buffer.append(' ');
210             }
211             for (int k = 0; k < chars_read; k++)
212             {
213                 if ((data[ k + j ] >= ' ') && (data[ k + j ] < 127))
214                 {
215                     buffer.append(( char ) data[ k + j ]);
216                 }
217                 else
218                 {
219                     buffer.append('.');
220                 }
221             }
222             buffer.append(EOL);
223             display_offset += chars_read;
224         }
225         return buffer.toString();
226     }
227     
228
229     private static String JavaDoc dump(final long value)
230     {
231         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
232         buf.setLength(0);
233         for (int j = 0; j < 8; j++)
234         {
235             buf.append( _hexcodes[ (( int ) (value >> _shifts[ j + _shifts.length - 8 ])) & 15 ]);
236         }
237         return buf.toString();
238     }
239
240     private static String JavaDoc dump(final byte value)
241     {
242         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
243         buf.setLength(0);
244         for (int j = 0; j < 2; j++)
245         {
246             buf.append(_hexcodes[ (value >> _shifts[ j + 6 ]) & 15 ]);
247         }
248         return buf.toString();
249     }
250
251     /**
252      * Converts the parameter to a hex value.
253      *
254      * @param value The value to convert
255      * @return A String representing the array of bytes
256      */

257     public static String JavaDoc toHex(final byte[] value)
258     {
259         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
260         retVal.append('[');
261         for(int x = 0; x < value.length; x++)
262         {
263             retVal.append(toHex(value[x]));
264             retVal.append(", ");
265         }
266         retVal.append(']');
267         return retVal.toString();
268     }
269
270     /**
271      * <p>Converts the parameter to a hex value breaking the results into
272      * lines.</p>
273      *
274      * @param value The value to convert
275      * @param bytesPerLine The maximum number of bytes per line. The next byte
276      * will be written to a new line
277      * @return A String representing the array of bytes
278      */

279     public static String JavaDoc toHex(final byte[] value, final int bytesPerLine)
280     {
281         final int digits =
282             (int) Math.round(Math.log(value.length) / Math.log(10) + 0.5);
283         final StringBuffer JavaDoc formatString = new StringBuffer JavaDoc();
284         for (int i = 0; i < digits; i++)
285             formatString.append('0');
286         formatString.append(": ");
287         final DecimalFormat JavaDoc format = new DecimalFormat JavaDoc(formatString.toString());
288         StringBuffer JavaDoc retVal = new StringBuffer JavaDoc();
289         retVal.append(format.format(0));
290         int i = -1;
291         for(int x = 0; x < value.length; x++)
292         {
293             if (++i == bytesPerLine)
294             {
295                 retVal.append('\n');
296                 retVal.append(format.format(x));
297                 i = 0;
298             }
299             retVal.append(toHex(value[x]));
300             retVal.append(", ");
301         }
302         return retVal.toString();
303     }
304
305     /**
306      * Converts the parameter to a hex value.
307      *
308      * @param value The value to convert
309      * @return The result right padded with 0
310      */

311     public static String JavaDoc toHex(final short value)
312     {
313         return toHex(value, 4);
314     }
315
316     /**
317      * Converts the parameter to a hex value.
318      *
319      * @param value The value to convert
320      * @return The result right padded with 0
321      */

322     public static String JavaDoc toHex(final byte value)
323     {
324         return toHex(value, 2);
325     }
326
327     /**
328      * Converts the parameter to a hex value.
329      *
330      * @param value The value to convert
331      * @return The result right padded with 0
332      */

333     public static String JavaDoc toHex(final int value)
334     {
335         return toHex(value, 8);
336     }
337
338     /**
339      * Converts the parameter to a hex value.
340      *
341      * @param value The value to convert
342      * @return The result right padded with 0
343      */

344     public static String JavaDoc toHex(final long value)
345     {
346         return toHex(value, 16);
347     }
348
349
350     private static String JavaDoc toHex(final long value, final int digits)
351     {
352         StringBuffer JavaDoc result = new StringBuffer JavaDoc(digits);
353         for (int j = 0; j < digits; j++)
354         {
355             result.append( _hexcodes[ (int) ((value >> _shifts[ j + (16 - digits) ]) & 15)]);
356         }
357         return result.toString();
358     }
359
360     /**
361      * Dumps <code>bytesToDump</code> bytes to an output stream.
362      *
363      * @param in The stream to read from
364      * @param out The output stream
365      * @param start The index to use as the starting position for the left hand side label
366      * @param bytesToDump The number of bytes to output. Use -1 to read until the end of file.
367      */

368     public static void dump( InputStream in, PrintStream out, int start, int bytesToDump ) throws IOException
369     {
370         ByteArrayOutputStream buf = new ByteArrayOutputStream();
371         if (bytesToDump == -1)
372         {
373             int c = in.read();
374             while (c != -1)
375             {
376                 buf.write(c);
377                 c = in.read();
378             }
379         }
380         else
381         {
382             int bytesRemaining = bytesToDump;
383             while (bytesRemaining-- > 0)
384             {
385                 int c = in.read();
386                 if (c == -1)
387                     break;
388                 else
389                     buf.write(c);
390             }
391         }
392
393         byte[] data = buf.toByteArray();
394         dump(data, 0, out, start, data.length);
395     }
396 }
397
Popular Tags