KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > HexDump


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import com.tc.exception.TCRuntimeException;
7
8 import java.io.IOException JavaDoc;
9 import java.io.StringWriter JavaDoc;
10 import java.io.Writer JavaDoc;
11
12 /**
13  * Knows how to dump out byte arrays in the style of emacs' hexl-mode.
14  */

15 public class HexDump {
16
17   private static final int BYTES_PER_LINE = 16;
18
19   public static String JavaDoc dump(byte[] data) {
20     return dump(data, 0, data.length);
21   }
22
23   public static String JavaDoc dump(byte[] data, int offset, int length) {
24     try {
25       StringWriter JavaDoc writer = new StringWriter JavaDoc();
26       dump(data, offset, length, writer);
27       return writer.toString();
28     } catch (IOException JavaDoc ioe) {
29       throw new TCRuntimeException("How'd we get an IOException with an in-memory stream?", ioe);
30     }
31   }
32
33   public static void dump(byte[] data, Writer JavaDoc out) throws IOException JavaDoc {
34     dump(data, 0, data.length, out);
35   }
36
37   public static void dump(byte[] data, int offset, int length, Writer JavaDoc out) throws IOException JavaDoc {
38     Assert.assertNotNull(data);
39     Assert.assertNotNull(out);
40     Assert.eval(offset >= 0);
41     Assert.eval(offset + length <= data.length);
42     Assert.eval(length >= 0);
43
44     boolean multiline = length > BYTES_PER_LINE;
45     byte[] thisLine = new byte[BYTES_PER_LINE];
46
47     out.write(length + " byte" + (length == 1 ? "" : "s") + ":");
48     if (multiline) out.write("\n");
49     else out.write(" ");
50
51     int linePos = 0;
52     for (int i = 0; i < length; ++i) {
53       if (i % BYTES_PER_LINE == 0 && multiline) {
54         if (i != 0) out.write("\n");
55         out.write(padHex(i, 8) + ":");
56         linePos = 0;
57       }
58
59       byte b = data[offset + i];
60       thisLine[linePos++] = b;
61
62       if (i % 2 == 0 && (multiline || i != 0)) out.write(" ");
63       out.write(padHex((b & 0x000000FF), 2));
64
65       if (i == (length - 1)) {
66         int onLastLine = length - ((length / BYTES_PER_LINE) * BYTES_PER_LINE);
67         if (onLastLine == 0) onLastLine = BYTES_PER_LINE;
68         int remaining = BYTES_PER_LINE - onLastLine;
69
70         if (multiline) {
71           if (remaining == 0) out.write(" ");
72           if (remaining % 2 != 0) {
73             out.write(" ");
74             remaining--;
75           }
76
77           Assert.eval(remaining % 2 == 0);
78
79           while (remaining > 0) {
80             out.write(" ");
81             remaining -= 2;
82           }
83
84           out.write(" ");
85         } else {
86           out.write(" ");
87         }
88
89         for (int j = 0; j < onLastLine; ++j) {
90           out.write(getChar(thisLine[j]));
91         }
92
93         if (multiline) out.write("\n");
94       } else if ((i + 1) % BYTES_PER_LINE == 0 && i != 0) {
95         out.write(" ");
96         for (int j = 0; j < thisLine.length; ++j) {
97           out.write(getChar(thisLine[j]));
98         }
99       }
100     }
101   }
102
103   /**
104    * @return a "raw" hex dump (2 hex characters per byte), not in emacs' hexl mode.
105    */

106   public static String JavaDoc rawHexDump(byte[] data) {
107     StringBuffer JavaDoc hexDump = new StringBuffer JavaDoc();
108     for (int pos = 0; pos < data.length; ++pos)
109       hexDump.append(padHex(0xff & data[pos], 2));
110     return hexDump.toString();
111   }
112
113   private static char getChar(byte b) {
114     int val = b & 0x000000FF;
115     if (val < 0x20 || val > 0x7E) return '.';
116     else return (char) b;
117   }
118
119   private static String JavaDoc padHex(int num, int totalLength) {
120     String JavaDoc out = Integer.toHexString(num);
121     while (out.length() < totalLength) {
122       out = "0" + out;
123     }
124     return out;
125   }
126
127 }
Popular Tags