KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > HexDump


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

16 package org.apache.commons.io;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20
21 /**
22  * Dumps data in hexadecimal format.
23  *
24  * Derived from a HexDump utility I wrote in June 2001.
25  *
26  * Taken from the POI project.
27  *
28  * @author Scott Sanders (sanders at apache dot org)
29  * @author Marc Johnson
30  * @version $Revision: 1.8 $ $Date: 2004/02/23 04:35:59 $
31  */

32 public class HexDump {
33
34     /**
35      * Instances should NOT be constructed in standard programming.
36      */

37     public HexDump() { }
38
39     /**
40      * dump an array of bytes to an OutputStream
41      *
42      * @param data the byte array to be dumped
43      * @param offset its offset, whatever that might mean
44      * @param stream the OutputStream to which the data is to be
45      * written
46      * @param index initial index into the byte array
47      *
48      * @exception IOException is thrown if anything goes wrong writing
49      * the data to stream
50      * @exception ArrayIndexOutOfBoundsException if the index is
51      * outside the data array's bounds
52      * @exception IllegalArgumentException if the output stream is
53      * null
54      */

55
56     public static void dump(byte[] data, long offset,
57                             OutputStream JavaDoc stream, int index)
58             throws IOException JavaDoc, ArrayIndexOutOfBoundsException JavaDoc,
59             IllegalArgumentException JavaDoc {
60         if ((index < 0) || (index >= data.length)) {
61             throw new ArrayIndexOutOfBoundsException JavaDoc(
62                     "illegal index: " + index + " into array of length "
63                     + data.length);
64         }
65         if (stream == null) {
66             throw new IllegalArgumentException JavaDoc("cannot write to nullstream");
67         }
68         long display_offset = offset + index;
69         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(74);
70
71         for (int j = index; j < data.length; j += 16) {
72             int chars_read = data.length - j;
73
74             if (chars_read > 16) {
75                 chars_read = 16;
76             }
77             buffer.append(dump(display_offset)).append(' ');
78             for (int k = 0; k < 16; k++) {
79                 if (k < chars_read) {
80                     buffer.append(dump(data[k + j]));
81                 } else {
82                     buffer.append(" ");
83                 }
84                 buffer.append(' ');
85             }
86             for (int k = 0; k < chars_read; k++) {
87                 if ((data[k + j] >= ' ') && (data[k + j] < 127)) {
88                     buffer.append((char) data[k + j]);
89                 } else {
90                     buffer.append('.');
91                 }
92             }
93             buffer.append(EOL);
94             stream.write(buffer.toString().getBytes());
95             stream.flush();
96             buffer.setLength(0);
97             display_offset += chars_read;
98         }
99     }
100
101     /** line-separator (initializes to "line.separator" system property. */
102     public static final String JavaDoc EOL =
103             System.getProperty("line.separator");
104     private static final StringBuffer JavaDoc _lbuffer = new StringBuffer JavaDoc(8);
105     private static final StringBuffer JavaDoc _cbuffer = new StringBuffer JavaDoc(2);
106     private static final char _hexcodes[] =
107             {
108                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
109                 'E', 'F'
110             };
111     private static final int _shifts[] =
112             {
113                 28, 24, 20, 16, 12, 8, 4, 0
114             };
115
116     private static StringBuffer JavaDoc dump(long value) {
117         _lbuffer.setLength(0);
118         for (int j = 0; j < 8; j++) {
119             _lbuffer
120                     .append(_hexcodes[((int) (value >> _shifts[j])) & 15]);
121         }
122         return _lbuffer;
123     }
124
125     private static StringBuffer JavaDoc dump(byte value) {
126         _cbuffer.setLength(0);
127         for (int j = 0; j < 2; j++) {
128             _cbuffer.append(_hexcodes[(value >> _shifts[j + 6]) & 15]);
129         }
130         return _cbuffer;
131     }
132 }
133
Popular Tags