KickJava   Java API By Example, From Geeks To Geeks.

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


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.bytes.TCByteBuffer;
7 import com.tc.bytes.TCByteBufferFactory;
8
9 import java.io.IOException JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.io.OutputStreamWriter JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13 import java.io.StringWriter JavaDoc;
14 import java.io.Writer JavaDoc;
15 import java.nio.ByteBuffer JavaDoc;
16
17 /**
18  * Class to facilitate printing out binary data or opaque objects in some sort of other usable form.
19  */

20 public final class Dumper {
21
22   /**
23    * Does nothing
24    */

25   private Dumper() {
26     super();
27   }
28
29   /**
30    * Calls <code>dump(buffer, 10, 16, 2, 8, new OutputStreamWriter(outs))</code>.
31    */

32   public static final void hexDump(TCByteBuffer buffer, OutputStream JavaDoc outs) throws IOException JavaDoc {
33     dump(new ByteishWrapper(null, buffer), 10, 16, 2, 8, new OutputStreamWriter JavaDoc(outs));
34   }
35
36   /**
37    * Calls <code>dump(buffer, 10, 16, 2, 8, new OutputStreamWriter(outs))</code>.
38    */

39   public static final void hexDump(TCByteBuffer buffer, Writer JavaDoc writer) throws IOException JavaDoc {
40     dump(new ByteishWrapper(null, buffer), 10, 16, 2, 8, writer);
41   }
42
43   /**
44    * Calls <code>dump(buffer, 10, 16, 2, 8, new OutputStreamWriter(outs))</code>.
45    */

46   public static final void hexDump(ByteBuffer buffer, OutputStream JavaDoc outs) throws IOException JavaDoc {
47     dump(new ByteishWrapper(buffer, null), 10, 16, 2, 8, new OutputStreamWriter JavaDoc(outs));
48   }
49
50   /**
51    * Calls <code>dump(buffer, 10, 16, 2, 8, new OutputStreamWriter(outs))</code>.
52    */

53   public static final void hexDump(ByteBuffer buffer, Writer JavaDoc writer) throws IOException JavaDoc {
54     dump(new ByteishWrapper(buffer, null), 10, 16, 2, 8, writer);
55   }
56
57   /**
58    * Calls <code>dump(buffer, 10, 8, 2, 8, new OutputStreamWriter(outs))</code>.
59    */

60   public static final void octalDump(TCByteBuffer buffer, OutputStream JavaDoc outs) throws IOException JavaDoc {
61     dump(new ByteishWrapper(null, buffer), 10, 8, 2, 8, new OutputStreamWriter JavaDoc(outs));
62   }
63
64   /**
65    * Calls <code>dump(buffer, 10, 8, 2, 8, new OutputStreamWriter(outs))</code>.
66    */

67   public static final void octalDump(TCByteBuffer buffer, Writer JavaDoc writer) throws IOException JavaDoc {
68     dump(new ByteishWrapper(null, buffer), 10, 8, 2, 8, writer);
69   }
70
71   /**
72    * Calls <code>dump(buffer, 10, 8, 2, 8, new OutputStreamWriter(outs))</code>.
73    */

74   public static final void octalDump(ByteBuffer buffer, OutputStream JavaDoc outs) throws IOException JavaDoc {
75     dump(new ByteishWrapper(buffer, null), 10, 8, 2, 8, new OutputStreamWriter JavaDoc(outs));
76   }
77
78   /**
79    * Calls <code>dump(buffer, 10, 8, 2, 8, new OutputStreamWriter(outs))</code>.
80    */

81   public static final void octalDump(ByteBuffer buffer, Writer JavaDoc writer) throws IOException JavaDoc {
82     dump(new ByteishWrapper(buffer, null), 10, 8, 2, 8, writer);
83   }
84
85   /**
86    * Calls
87    * <code>dump(buffer, offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, new OutputStreamWriter(outs))</code>.
88    */

89   public static final void dump(TCByteBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn,
90                                 int columnsPerLine, OutputStream JavaDoc outs) throws IOException JavaDoc {
91     dump(new ByteishWrapper(null, buffer), offsetRadix, dataRadix, bytesPerColumn, columnsPerLine,
92          new OutputStreamWriter JavaDoc(outs));
93   }
94
95   /**
96    * Calls <code>dump(buffer, offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, writer)</code>.
97    */

98   public static final void dump(TCByteBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn,
99                                 int columnsPerLine, Writer JavaDoc writer) throws IOException JavaDoc {
100     dump(new ByteishWrapper(null, buffer), offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, writer);
101   }
102
103   /**
104    * Calls
105    * <code>dump(buffer, offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, new OutputStreamWriter(outs))</code>.
106    */

107   public static final void dump(ByteBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn,
108                                 int columnsPerLine, OutputStream JavaDoc outs) throws IOException JavaDoc {
109     dump(new ByteishWrapper(buffer, null), offsetRadix, dataRadix, bytesPerColumn, columnsPerLine,
110          new OutputStreamWriter JavaDoc(outs));
111   }
112
113   /**
114    * Calls <code>dump(buffer, offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, writer)</code>.
115    */

116   public static final void dump(ByteBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn,
117                                 int columnsPerLine, Writer JavaDoc writer) throws IOException JavaDoc {
118     dump(new ByteishWrapper(buffer, null), offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, writer);
119   }
120
121   /**
122    * Dumps the contents of the buffer in the tradition of the "od" and "hexdump" UNIX utilities.
123    *
124    * @param buffer the byte-ish buffer to read from
125    * @param offsetRadix the radix to use when printing the offset
126    * @param dataRadix the radix to use when printing the data
127    * @param bytesPerColumn the number of bytes to concatenate in radix <code>dataRadix</code> in each column (2 is
128    * normal)
129    * @param columnsPerLine the number of columns of groups of bytes to have (8 is normal for bases 8 and 16, probably
130    * want less for smaller bases)
131    * @param writer the writer to spit the output to
132    */

133   private static final void dump(ByteishBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn,
134                                  int columnsPerLine, Writer JavaDoc writer) throws IOException JavaDoc {
135     // Do some basic sanity checking
136
if (buffer == null || offsetRadix < 2 || dataRadix < 2 || bytesPerColumn < 1 || columnsPerLine < 1
137         || writer == null) { return; }
138     int dataPadding = 2;
139     // Increase the padding per byte for any radix lower than 16
140
if (dataRadix < 16) {
141       if (dataRadix > 6) {
142         dataPadding = 3;
143       } else if (dataRadix > 3) {
144         dataPadding = 4;
145       } else if (dataRadix == 3) {
146         dataPadding = 6;
147       } else if (dataRadix == 2) {
148         dataPadding = 8;
149       }
150     }
151     int bytesPerLine = bytesPerColumn * columnsPerLine;
152     int bytesPrintedOnLine = 0;
153     for (int pos = 0; pos < buffer.limit(); ++pos) {
154       // See if we need to start a new line
155
if (bytesPrintedOnLine == bytesPerLine) {
156         writer.write("\n");
157         writer.flush();
158         bytesPrintedOnLine = 0;
159       }
160       // See if we need to print out the offset
161
if (bytesPrintedOnLine == 0) {
162         // Print the offset
163
writer.write(StringUtil.toPaddedString(pos, offsetRadix, 7));
164       }
165       // See if we need to print a column break, either after the offset
166
// is printed
167
// or we've finished a column
168
if (bytesPrintedOnLine % bytesPerColumn == 0) {
169         writer.write(StringUtil.SPACE_STRING);
170       }
171       // Print out the next byte in the specified radix with a certain
172
// padding
173
writer.write(StringUtil.toPaddedString(0x00000000000000ff & buffer.get(pos), dataRadix, dataPadding));
174       ++bytesPrintedOnLine;
175     }
176     // If we didn't fill up the line, fill it with spaces
177

178     // Print the offset of the buffer length + 1 on a line by itself
179
writer.write("\n" + StringUtil.toPaddedString(buffer.limit() + 1, offsetRadix, 7) + "\n");
180     writer.flush();
181   }
182
183   public static String JavaDoc hexDump(byte[] bytes, int length) {
184     StringWriter JavaDoc sw = new StringWriter JavaDoc();
185     sw.write("\n");
186     byte[] dumpBytes = new byte[length];
187     for (int pos = 0; pos < length; ++pos)
188       dumpBytes[pos] = bytes[pos];
189     try {
190       Dumper.hexDump(TCByteBufferFactory.copyAndWrap(dumpBytes), sw);
191     } catch (IOException JavaDoc ioe) {
192       sw = new StringWriter JavaDoc();
193       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
194       sw.write("Unable to generate hex dump, exception was: ");
195       ioe.printStackTrace(pw);
196       pw.flush();
197     }
198     return sw.toString();
199   }
200
201 }
Popular Tags