001: package com.ice.util;
002:
003: import java.lang.*;
004: import java.io.*;
005:
006: public class HexDump {
007: private static final int ROW_BYTES = 16;
008: private static final int ROW_QTR1 = 3;
009: private static final int ROW_HALF = 7;
010: private static final int ROW_QTR2 = 11;
011:
012: public static void dumpHexData(PrintStream out, String title,
013: byte[] buf, int numBytes) {
014: PrintWriter wrtr = new PrintWriter(new OutputStreamWriter(out));
015:
016: HexDump.dumpHexData(wrtr, title, buf, 0, numBytes);
017: }
018:
019: public static void dumpHexData(PrintWriter out, String title,
020: byte[] buf, int offset, int numBytes) {
021: int rows, residue, i, j;
022: byte[] save_buf = new byte[ROW_BYTES + 2];
023: char[] hex_buf = new char[4];
024: char[] idx_buf = new char[8];
025: char[] hex_chars = new char[20];
026:
027: hex_chars[0] = '0';
028: hex_chars[1] = '1';
029: hex_chars[2] = '2';
030: hex_chars[3] = '3';
031: hex_chars[4] = '4';
032: hex_chars[5] = '5';
033: hex_chars[6] = '6';
034: hex_chars[7] = '7';
035: hex_chars[8] = '8';
036: hex_chars[9] = '9';
037: hex_chars[10] = 'A';
038: hex_chars[11] = 'B';
039: hex_chars[12] = 'C';
040: hex_chars[13] = 'D';
041: hex_chars[14] = 'E';
042: hex_chars[15] = 'F';
043:
044: out.println(title + " - " + numBytes + " bytes.");
045: rows = numBytes >> 4;
046: residue = numBytes & 0x0000000F;
047: for (i = 0; i < rows; i++) {
048: int hexVal = (i * ROW_BYTES);
049: idx_buf[0] = hex_chars[((hexVal >> 12) & 15)];
050: idx_buf[1] = hex_chars[((hexVal >> 8) & 15)];
051: idx_buf[2] = hex_chars[((hexVal >> 4) & 15)];
052: idx_buf[3] = hex_chars[(hexVal & 15)];
053:
054: String idxStr = new String(idx_buf, 0, 4);
055: out.print(idxStr + ": ");
056:
057: for (j = 0; j < ROW_BYTES; j++) {
058: save_buf[j] = buf[offset + (i * ROW_BYTES) + j];
059:
060: hex_buf[0] = hex_chars[(save_buf[j] >> 4) & 0x0F];
061: hex_buf[1] = hex_chars[save_buf[j] & 0x0F];
062:
063: out.print(hex_buf[0]);
064: out.print(hex_buf[1]);
065: out.print(' ');
066:
067: if (j == ROW_QTR1 || j == ROW_HALF || j == ROW_QTR2)
068: out.print(" ");
069:
070: if (save_buf[j] < 0x20 || save_buf[j] > 0x7E)
071: save_buf[j] = (byte) '.';
072: }
073:
074: String saveStr = new String(save_buf, 0, j);
075: out.println(" | " + saveStr + " |");
076: }
077:
078: if (residue > 0) {
079: int hexVal = (i * ROW_BYTES);
080: idx_buf[0] = hex_chars[((hexVal >> 12) & 15)];
081: idx_buf[1] = hex_chars[((hexVal >> 8) & 15)];
082: idx_buf[2] = hex_chars[((hexVal >> 4) & 15)];
083: idx_buf[3] = hex_chars[(hexVal & 15)];
084:
085: String idxStr = new String(idx_buf, 0, 4);
086: out.print(idxStr + ": ");
087:
088: for (j = 0; j < residue; j++) {
089: save_buf[j] = buf[offset + (i * ROW_BYTES) + j];
090:
091: hex_buf[0] = hex_chars[(save_buf[j] >> 4) & 0x0F];
092: hex_buf[1] = hex_chars[save_buf[j] & 0x0F];
093:
094: out.print((char) hex_buf[0]);
095: out.print((char) hex_buf[1]);
096: out.print(' ');
097:
098: if (j == ROW_QTR1 || j == ROW_HALF || j == ROW_QTR2)
099: out.print(" ");
100:
101: if (save_buf[j] < 0x20 || save_buf[j] > 0x7E)
102: save_buf[j] = (byte) '.';
103: }
104:
105: for ( /*j INHERITED*/; j < ROW_BYTES; j++) {
106: save_buf[j] = (byte) ' ';
107: out.print(" ");
108: if (j == ROW_QTR1 || j == ROW_HALF || j == ROW_QTR2)
109: out.print(" ");
110: }
111:
112: String saveStr = new String(save_buf, 0, j);
113: out.println(" | " + saveStr + " |");
114: }
115: }
116:
117: static public void main(String[] args) {
118: byte[] data = new byte[132];
119: for (int i = 0; i < 132; ++i)
120: data[i] = (byte) i;
121:
122: HexDump.dumpHexData(System.err, "Test HexDump", data, 132);
123: }
124:
125: }
|