1 2 17 18 19 package org.apache.poi.util; 20 21 import java.io.*; 22 import java.text.DecimalFormat ; 23 24 31 32 public class HexDump 33 { 34 public static final String EOL = 35 System.getProperty("line.separator"); 36 private static final char _hexcodes[] = 39 { 40 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 41 'E', 'F' 42 }; 43 private static final int _shifts[] = 44 { 45 60, 56, 52, 48, 44, 40, 36, 32, 28, 24, 20, 16, 12, 8, 4, 0 46 }; 47 48 49 private HexDump() 51 { 52 } 53 54 71 public synchronized static void dump(final byte [] data, final long offset, 72 final OutputStream stream, final int index, final int length) 73 throws IOException, ArrayIndexOutOfBoundsException , 74 IllegalArgumentException 75 { 76 if (data.length == 0) 77 { 78 stream.write( "No Data".getBytes() ); 79 stream.flush(); 80 return; 81 } 82 if ((index < 0) || (index >= data.length)) 83 { 84 throw new ArrayIndexOutOfBoundsException ( 85 "illegal index: " + index + " into array of length " 86 + data.length); 87 } 88 if (stream == null) 89 { 90 throw new IllegalArgumentException ("cannot write to nullstream"); 91 } 92 93 long display_offset = offset + index; 94 StringBuffer buffer = new StringBuffer (74); 95 96 97 int data_length = Math.min(data.length,index+length); 98 for (int j = index; j < data_length; j += 16) 99 { 100 int chars_read = data_length - j; 101 102 if (chars_read > 16) 103 { 104 chars_read = 16; 105 } 106 buffer.append( 107 dump(display_offset) 108 ).append(' '); 109 for (int k = 0; k < 16; k++) 110 { 111 if (k < chars_read) 112 { 113 buffer.append(dump(data[ k + j ])); 114 } 115 else 116 { 117 buffer.append(" "); 118 } 119 buffer.append(' '); 120 } 121 for (int k = 0; k < chars_read; k++) 122 { 123 if ((data[ k + j ] >= ' ') && (data[ k + j ] < 127)) 124 { 125 buffer.append(( char ) data[ k + j ]); 126 } 127 else 128 { 129 buffer.append('.'); 130 } 131 } 132 buffer.append(EOL); 133 stream.write(buffer.toString().getBytes()); 134 stream.flush(); 135 buffer.setLength(0); 136 display_offset += chars_read; 137 } 138 139 } 140 141 157 158 public synchronized static void dump(final byte [] data, final long offset, 159 final OutputStream stream, final int index) 160 throws IOException, ArrayIndexOutOfBoundsException , 161 IllegalArgumentException 162 { 163 dump(data, offset, stream, index, data.length-index); 164 } 165 166 177 178 public static String dump(final byte [] data, final long offset, 179 final int index) { 180 StringBuffer buffer; 181 if ((index < 0) || (index >= data.length)) 182 { 183 throw new ArrayIndexOutOfBoundsException ( 184 "illegal index: " + index + " into array of length " 185 + data.length); 186 } 187 long display_offset = offset + index; 188 buffer = new StringBuffer (74); 189 190 for (int j = index; j < data.length; j += 16) 191 { 192 int chars_read = data.length - j; 193 194 if (chars_read > 16) 195 { 196 chars_read = 16; 197 } 198 buffer.append(dump(display_offset)).append(' '); 199 for (int k = 0; k < 16; k++) 200 { 201 if (k < chars_read) 202 { 203 buffer.append(dump(data[ k + j ])); 204 } 205 else 206 { 207 buffer.append(" "); 208 } 209 buffer.append(' '); 210 } 211 for (int k = 0; k < chars_read; k++) 212 { 213 if ((data[ k + j ] >= ' ') && (data[ k + j ] < 127)) 214 { 215 buffer.append(( char ) data[ k + j ]); 216 } 217 else 218 { 219 buffer.append('.'); 220 } 221 } 222 buffer.append(EOL); 223 display_offset += chars_read; 224 } 225 return buffer.toString(); 226 } 227 228 229 private static String dump(final long value) 230 { 231 StringBuffer buf = new StringBuffer (); 232 buf.setLength(0); 233 for (int j = 0; j < 8; j++) 234 { 235 buf.append( _hexcodes[ (( int ) (value >> _shifts[ j + _shifts.length - 8 ])) & 15 ]); 236 } 237 return buf.toString(); 238 } 239 240 private static String dump(final byte value) 241 { 242 StringBuffer buf = new StringBuffer (); 243 buf.setLength(0); 244 for (int j = 0; j < 2; j++) 245 { 246 buf.append(_hexcodes[ (value >> _shifts[ j + 6 ]) & 15 ]); 247 } 248 return buf.toString(); 249 } 250 251 257 public static String toHex(final byte[] value) 258 { 259 StringBuffer retVal = new StringBuffer (); 260 retVal.append('['); 261 for(int x = 0; x < value.length; x++) 262 { 263 retVal.append(toHex(value[x])); 264 retVal.append(", "); 265 } 266 retVal.append(']'); 267 return retVal.toString(); 268 } 269 270 279 public static String toHex(final byte[] value, final int bytesPerLine) 280 { 281 final int digits = 282 (int) Math.round(Math.log(value.length) / Math.log(10) + 0.5); 283 final StringBuffer formatString = new StringBuffer (); 284 for (int i = 0; i < digits; i++) 285 formatString.append('0'); 286 formatString.append(": "); 287 final DecimalFormat format = new DecimalFormat (formatString.toString()); 288 StringBuffer retVal = new StringBuffer (); 289 retVal.append(format.format(0)); 290 int i = -1; 291 for(int x = 0; x < value.length; x++) 292 { 293 if (++i == bytesPerLine) 294 { 295 retVal.append('\n'); 296 retVal.append(format.format(x)); 297 i = 0; 298 } 299 retVal.append(toHex(value[x])); 300 retVal.append(", "); 301 } 302 return retVal.toString(); 303 } 304 305 311 public static String toHex(final short value) 312 { 313 return toHex(value, 4); 314 } 315 316 322 public static String toHex(final byte value) 323 { 324 return toHex(value, 2); 325 } 326 327 333 public static String toHex(final int value) 334 { 335 return toHex(value, 8); 336 } 337 338 344 public static String toHex(final long value) 345 { 346 return toHex(value, 16); 347 } 348 349 350 private static String toHex(final long value, final int digits) 351 { 352 StringBuffer result = new StringBuffer (digits); 353 for (int j = 0; j < digits; j++) 354 { 355 result.append( _hexcodes[ (int) ((value >> _shifts[ j + (16 - digits) ]) & 15)]); 356 } 357 return result.toString(); 358 } 359 360 368 public static void dump( InputStream in, PrintStream out, int start, int bytesToDump ) throws IOException 369 { 370 ByteArrayOutputStream buf = new ByteArrayOutputStream(); 371 if (bytesToDump == -1) 372 { 373 int c = in.read(); 374 while (c != -1) 375 { 376 buf.write(c); 377 c = in.read(); 378 } 379 } 380 else 381 { 382 int bytesRemaining = bytesToDump; 383 while (bytesRemaining-- > 0) 384 { 385 int c = in.read(); 386 if (c == -1) 387 break; 388 else 389 buf.write(c); 390 } 391 } 392 393 byte[] data = buf.toByteArray(); 394 dump(data, 0, out, start, data.length); 395 } 396 } 397 | Popular Tags |