KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > util > Hexdump


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  * "Christopher R. Hertel" <jcifs at samba dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package jcifs.util;
21
22 import java.io.OutputStream JavaDoc;
23 import java.io.PrintStream JavaDoc;
24
25 /**
26  */

27
28 public class Hexdump {
29
30     private static final String JavaDoc NL = System.getProperty( "line.separator" );
31     private static final int NL_LENGTH = NL.length();
32
33     private static final char[] SPACE_CHARS = {
34         ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
35         ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
36         ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
37     };
38
39     public static final char[] HEX_DIGITS = {
40         '0', '1', '2', '3', '4', '5',
41         '6', '7', '8', '9', 'A', 'B',
42         'C', 'D', 'E', 'F'
43     };
44
45 /**
46  * Generate "hexdump" output of the buffer at src like the following:
47  *
48  * <p><blockquote><pre>
49  * 00000: 04 d2 29 00 00 01 00 00 00 00 00 01 20 45 47 46 |..)......... EGF|
50  * 00010: 43 45 46 45 45 43 41 43 41 43 41 43 41 43 41 43 |CEFEECACACACACAC|
51  * 00020: 41 43 41 43 41 43 41 43 41 43 41 41 44 00 00 20 |ACACACACACAAD.. |
52  * 00030: 00 01 c0 0c 00 20 00 01 00 00 00 00 00 06 20 00 |..... ........ .|
53  * 00040: ac 22 22 e1 |."". |
54  * </blockquote></pre>
55  */

56
57     public static void hexdump( PrintStream JavaDoc ps, byte[] src, int srcIndex, int length ) {
58         if( length == 0 ) {
59             return;
60         }
61
62         int s = length % 16;
63         int r = ( s == 0 ) ? length / 16 : length / 16 + 1;
64         char[] c = new char[r * (74 + NL_LENGTH)];
65         char[] d = new char[16];
66         int i;
67         int si = 0;
68         int ci = 0;
69
70         do {
71             toHexChars( si, c, ci, 5 );
72             ci += 5;
73             c[ci++] = ':';
74             do {
75                 if( si == length ) {
76                     int n = 16 - s;
77                     System.arraycopy( SPACE_CHARS, 0, c, ci, n * 3 );
78                     ci += n * 3;
79                     System.arraycopy( SPACE_CHARS, 0, d, s, n );
80                     break;
81                 }
82                 c[ci++] = ' ';
83                 i = src[srcIndex + si] & 0xFF;
84                 toHexChars( i, c, ci, 2 );
85                 ci += 2;
86                 if( i < 0 || Character.isISOControl( (char)i )) {
87                     d[si % 16] = '.';
88                 } else {
89                     d[si % 16] = (char)i;
90                 }
91             } while(( ++si % 16 ) != 0 );
92             c[ci++] = ' ';
93             c[ci++] = ' ';
94             c[ci++] = '|';
95             System.arraycopy( d, 0, c, ci, 16 );
96             ci += 16;
97             c[ci++] = '|';
98             NL.getChars( 0, NL_LENGTH, c, ci );
99             ci += NL_LENGTH;
100         } while( si < length );
101
102         ps.println( c );
103     }
104
105 /**
106  * This is an alternative to the <code>java.lang.Integer.toHexString</cod>
107  * method. It is an efficient relative that also will pad the left side so
108  * that the result is <code>size</code> digits.
109  */

110     public static String JavaDoc toHexString( int val, int size ) {
111         char[] c = new char[size];
112         toHexChars( val, c, 0, size );
113         return new String JavaDoc( c );
114     }
115     public static String JavaDoc toHexString( long val, int size ) {
116         char[] c = new char[size];
117         toHexChars( val, c, 0, size );
118         return new String JavaDoc( c );
119     }
120     public static String JavaDoc toHexString( byte[] src, int srcIndex, int size ) {
121         char[] c = new char[size];
122         size = ( size % 2 == 0 ) ? size / 2 : size / 2 + 1;
123         for( int i = 0, j = 0; i < size; i++ ) {
124             c[j++] = HEX_DIGITS[(src[i] >> 4 ) & 0x0F];
125             if( j == c.length ) {
126                 break;
127             }
128             c[j++] = HEX_DIGITS[src[i] & 0x0F];
129         }
130         return new String JavaDoc( c );
131     }
132
133 /**
134  * This is the same as {@link jcifs.util.Hexdump#toHexString(int val, int
135  * size)} but provides a more practical form when trying to avoid {@link
136  * java.lang.String} concatenation and {@link java.lang.StringBuffer}.
137  */

138     public static void toHexChars( int val, char dst[], int dstIndex, int size ) {
139         while( size > 0 ) {
140             int i = dstIndex + size - 1;
141             if( i < dst.length ) {
142                 dst[i] = HEX_DIGITS[val & 0x000F];
143             }
144             if( val != 0 ) {
145                 val >>>= 4;
146             }
147             size--;
148         }
149     }
150     public static void toHexChars( long val, char dst[], int dstIndex, int size ) {
151         while( size > 0 ) {
152             dst[dstIndex + size - 1] = HEX_DIGITS[(int)( val & 0x000FL )];
153             if( val != 0 ) {
154                 val >>>= 4;
155             }
156             size--;
157         }
158     }
159 }
160
161
Popular Tags