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