1 4 package com.tc.util; 5 6 import com.tc.bytes.TCByteBuffer; 7 import com.tc.bytes.TCByteBufferFactory; 8 9 import java.io.IOException ; 10 import java.io.OutputStream ; 11 import java.io.OutputStreamWriter ; 12 import java.io.PrintWriter ; 13 import java.io.StringWriter ; 14 import java.io.Writer ; 15 import java.nio.ByteBuffer ; 16 17 20 public final class Dumper { 21 22 25 private Dumper() { 26 super(); 27 } 28 29 32 public static final void hexDump(TCByteBuffer buffer, OutputStream outs) throws IOException { 33 dump(new ByteishWrapper(null, buffer), 10, 16, 2, 8, new OutputStreamWriter (outs)); 34 } 35 36 39 public static final void hexDump(TCByteBuffer buffer, Writer writer) throws IOException { 40 dump(new ByteishWrapper(null, buffer), 10, 16, 2, 8, writer); 41 } 42 43 46 public static final void hexDump(ByteBuffer buffer, OutputStream outs) throws IOException { 47 dump(new ByteishWrapper(buffer, null), 10, 16, 2, 8, new OutputStreamWriter (outs)); 48 } 49 50 53 public static final void hexDump(ByteBuffer buffer, Writer writer) throws IOException { 54 dump(new ByteishWrapper(buffer, null), 10, 16, 2, 8, writer); 55 } 56 57 60 public static final void octalDump(TCByteBuffer buffer, OutputStream outs) throws IOException { 61 dump(new ByteishWrapper(null, buffer), 10, 8, 2, 8, new OutputStreamWriter (outs)); 62 } 63 64 67 public static final void octalDump(TCByteBuffer buffer, Writer writer) throws IOException { 68 dump(new ByteishWrapper(null, buffer), 10, 8, 2, 8, writer); 69 } 70 71 74 public static final void octalDump(ByteBuffer buffer, OutputStream outs) throws IOException { 75 dump(new ByteishWrapper(buffer, null), 10, 8, 2, 8, new OutputStreamWriter (outs)); 76 } 77 78 81 public static final void octalDump(ByteBuffer buffer, Writer writer) throws IOException { 82 dump(new ByteishWrapper(buffer, null), 10, 8, 2, 8, writer); 83 } 84 85 89 public static final void dump(TCByteBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn, 90 int columnsPerLine, OutputStream outs) throws IOException { 91 dump(new ByteishWrapper(null, buffer), offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, 92 new OutputStreamWriter (outs)); 93 } 94 95 98 public static final void dump(TCByteBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn, 99 int columnsPerLine, Writer writer) throws IOException { 100 dump(new ByteishWrapper(null, buffer), offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, writer); 101 } 102 103 107 public static final void dump(ByteBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn, 108 int columnsPerLine, OutputStream outs) throws IOException { 109 dump(new ByteishWrapper(buffer, null), offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, 110 new OutputStreamWriter (outs)); 111 } 112 113 116 public static final void dump(ByteBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn, 117 int columnsPerLine, Writer writer) throws IOException { 118 dump(new ByteishWrapper(buffer, null), offsetRadix, dataRadix, bytesPerColumn, columnsPerLine, writer); 119 } 120 121 133 private static final void dump(ByteishBuffer buffer, int offsetRadix, int dataRadix, int bytesPerColumn, 134 int columnsPerLine, Writer writer) throws IOException { 135 if (buffer == null || offsetRadix < 2 || dataRadix < 2 || bytesPerColumn < 1 || columnsPerLine < 1 137 || writer == null) { return; } 138 int dataPadding = 2; 139 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 if (bytesPrintedOnLine == bytesPerLine) { 156 writer.write("\n"); 157 writer.flush(); 158 bytesPrintedOnLine = 0; 159 } 160 if (bytesPrintedOnLine == 0) { 162 writer.write(StringUtil.toPaddedString(pos, offsetRadix, 7)); 164 } 165 if (bytesPrintedOnLine % bytesPerColumn == 0) { 169 writer.write(StringUtil.SPACE_STRING); 170 } 171 writer.write(StringUtil.toPaddedString(0x00000000000000ff & buffer.get(pos), dataRadix, dataPadding)); 174 ++bytesPrintedOnLine; 175 } 176 178 writer.write("\n" + StringUtil.toPaddedString(buffer.limit() + 1, offsetRadix, 7) + "\n"); 180 writer.flush(); 181 } 182 183 public static String hexDump(byte[] bytes, int length) { 184 StringWriter sw = new StringWriter (); 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 ioe) { 192 sw = new StringWriter (); 193 PrintWriter pw = new PrintWriter (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 |