001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: * "Christopher R. Hertel" <jcifs at samba dot org>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package jcifs.util;
021:
022: import java.io.OutputStream;
023: import java.io.PrintStream;
024:
025: /**
026: */
027:
028: public class Hexdump {
029:
030: private static final String NL = System
031: .getProperty("line.separator");
032: private static final int NL_LENGTH = NL.length();
033:
034: private static final char[] SPACE_CHARS = { ' ', ' ', ' ', ' ',
035: ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
036: ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
037: ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
038: ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
039:
040: public static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4',
041: '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
042:
043: /**
044: * Generate "hexdump" output of the buffer at src like the following:
045: *
046: * <p><blockquote><pre>
047: * 00000: 04 d2 29 00 00 01 00 00 00 00 00 01 20 45 47 46 |..)......... EGF|
048: * 00010: 43 45 46 45 45 43 41 43 41 43 41 43 41 43 41 43 |CEFEECACACACACAC|
049: * 00020: 41 43 41 43 41 43 41 43 41 43 41 41 44 00 00 20 |ACACACACACAAD.. |
050: * 00030: 00 01 c0 0c 00 20 00 01 00 00 00 00 00 06 20 00 |..... ........ .|
051: * 00040: ac 22 22 e1 |."". |
052: * </blockquote></pre>
053: */
054:
055: public static void hexdump(PrintStream ps, byte[] src,
056: int srcIndex, int length) {
057: if (length == 0) {
058: return;
059: }
060:
061: int s = length % 16;
062: int r = (s == 0) ? length / 16 : length / 16 + 1;
063: char[] c = new char[r * (74 + NL_LENGTH)];
064: char[] d = new char[16];
065: int i;
066: int si = 0;
067: int ci = 0;
068:
069: do {
070: toHexChars(si, c, ci, 5);
071: ci += 5;
072: c[ci++] = ':';
073: do {
074: if (si == length) {
075: int n = 16 - s;
076: System.arraycopy(SPACE_CHARS, 0, c, ci, n * 3);
077: ci += n * 3;
078: System.arraycopy(SPACE_CHARS, 0, d, s, n);
079: break;
080: }
081: c[ci++] = ' ';
082: i = src[srcIndex + si] & 0xFF;
083: toHexChars(i, c, ci, 2);
084: ci += 2;
085: if (i < 0 || Character.isISOControl((char) i)) {
086: d[si % 16] = '.';
087: } else {
088: d[si % 16] = (char) i;
089: }
090: } while ((++si % 16) != 0);
091: c[ci++] = ' ';
092: c[ci++] = ' ';
093: c[ci++] = '|';
094: System.arraycopy(d, 0, c, ci, 16);
095: ci += 16;
096: c[ci++] = '|';
097: NL.getChars(0, NL_LENGTH, c, ci);
098: ci += NL_LENGTH;
099: } while (si < length);
100:
101: ps.println(c);
102: }
103:
104: /**
105: * This is an alternative to the <code>java.lang.Integer.toHexString</cod>
106: * method. It is an efficient relative that also will pad the left side so
107: * that the result is <code>size</code> digits.
108: */
109: public static String toHexString(int val, int size) {
110: char[] c = new char[size];
111: toHexChars(val, c, 0, size);
112: return new String(c);
113: }
114:
115: public static String toHexString(long val, int size) {
116: char[] c = new char[size];
117: toHexChars(val, c, 0, size);
118: return new String(c);
119: }
120:
121: public static String toHexString(byte[] src, int srcIndex, int size) {
122: char[] c = new char[size];
123: size = (size % 2 == 0) ? size / 2 : size / 2 + 1;
124: for (int i = 0, j = 0; i < size; i++) {
125: c[j++] = HEX_DIGITS[(src[i] >> 4) & 0x0F];
126: if (j == c.length) {
127: break;
128: }
129: c[j++] = HEX_DIGITS[src[i] & 0x0F];
130: }
131: return new String(c);
132: }
133:
134: /**
135: * This is the same as {@link jcifs.util.Hexdump#toHexString(int val, int
136: * size)} but provides a more practical form when trying to avoid {@link
137: * java.lang.String} concatenation and {@link java.lang.StringBuffer}.
138: */
139: public static void toHexChars(int val, char dst[], int dstIndex,
140: int size) {
141: while (size > 0) {
142: int i = dstIndex + size - 1;
143: if (i < dst.length) {
144: dst[i] = HEX_DIGITS[val & 0x000F];
145: }
146: if (val != 0) {
147: val >>>= 4;
148: }
149: size--;
150: }
151: }
152:
153: public static void toHexChars(long val, char dst[], int dstIndex,
154: int size) {
155: while (size > 0) {
156: dst[dstIndex + size - 1] = HEX_DIGITS[(int) (val & 0x000FL)];
157: if (val != 0) {
158: val >>>= 4;
159: }
160: size--;
161: }
162: }
163: }
|