1 17 package org.alfresco.filesys.util; 18 19 import java.io.PrintStream ; 20 21 24 public final class HexDump 25 { 26 27 34 35 public static final void Dump(byte[] byt, int len, int offset) 36 { 37 Dump(byt, len, offset, System.out); 38 } 39 40 48 49 public static final void Dump(byte[] byt, int len, int offset, PrintStream stream) 50 { 51 52 54 StringBuffer ascBuf = new StringBuffer (); 55 StringBuffer hexBuf = new StringBuffer (); 56 57 60 int dlen = 0; 61 int doff = offset; 62 String posStr = null; 63 64 while (dlen < len) 65 { 66 67 69 ascBuf.setLength(0); 70 hexBuf.setLength(0); 71 72 posStr = generatePositionString(doff); 73 74 76 doff = generateLine(byt, doff, ascBuf, hexBuf); 77 78 80 stream.print(posStr); 81 stream.print(hexBuf.toString()); 82 stream.println(ascBuf.toString()); 83 84 86 dlen += 16; 87 } 88 } 89 90 96 public static final String hexString(String str) 97 { 98 if (str != null) 99 return hexString(str.getBytes()); 100 return ""; 101 } 102 103 110 public static final String hexString(String str, String gap) 111 { 112 if (str != null) 113 return hexString(str.getBytes(), gap); 114 return ""; 115 } 116 117 123 public static final String hexString(byte[] buf) 124 { 125 return hexString(buf, buf.length, null); 126 } 127 128 135 public static final String hexString(byte[] buf, String gap) 136 { 137 return hexString(buf, buf.length, gap); 138 } 139 140 148 public static final String hexString(byte[] buf, int len, String gap) 149 { 150 151 153 if (buf == null) 154 return ""; 155 156 158 int buflen = buf.length * 2; 159 if (gap != null) 160 buflen += buf.length * gap.length(); 161 162 StringBuffer hex = new StringBuffer (buflen); 163 164 166 for (int i = 0; i < len; i++) 167 { 168 169 171 int curbyt = (int) (buf[i] & 0x00FF); 172 173 175 hex.append(Integer.toHexString((curbyt & 0xF0) >> 4)); 176 hex.append(Integer.toHexString(curbyt & 0x0F)); 177 178 180 if (gap != null && i < (len - 1)) 181 hex.append(gap); 182 } 183 184 186 return hex.toString(); 187 } 188 189 195 private static final String generatePositionString(int off) 196 { 197 198 200 StringBuffer posStr = new StringBuffer ("" + off + " - "); 201 while (posStr.length() < 8) 202 posStr.insert(0, " "); 203 204 206 return posStr.toString(); 207 } 208 209 218 219 private static final int generateLine(byte[] byt, int off, StringBuffer ascBuf, StringBuffer hexBuf) 220 { 221 222 224 int dumplen = byt.length - off; 225 if (dumplen > 16) 226 dumplen = 16; 227 228 230 for (int i = 0; i < dumplen; i++) 231 { 232 233 235 int curbyt = (int) (byt[off++] & 0x00FF); 236 237 239 hexBuf.append(Integer.toHexString((curbyt & 0xF0) >> 4)); 240 hexBuf.append(Integer.toHexString(curbyt & 0x0F)); 241 hexBuf.append(" "); 242 243 245 if (Character.isLetterOrDigit((char) curbyt) || Character.getType((char) curbyt) != Character.CONTROL) 246 ascBuf.append((char) curbyt); 247 else 248 ascBuf.append("."); 249 } 250 251 253 hexBuf.append(" - "); 254 255 257 return off; 258 } 259 } | Popular Tags |