1 19 20 package jcifs.util; 21 22 import java.io.OutputStream ; 23 import java.io.PrintStream ; 24 25 27 28 public class Hexdump { 29 30 private static final String NL = System.getProperty( "line.separator" ); 31 private static final int NL_LENGTH = NL.length(); 32 33 private static final char[] SPACE_CHARS = { 34 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 35 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 36 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' 37 }; 38 39 public static final char[] HEX_DIGITS = { 40 '0', '1', '2', '3', '4', '5', 41 '6', '7', '8', '9', 'A', 'B', 42 'C', 'D', 'E', 'F' 43 }; 44 45 56 57 public static void hexdump( PrintStream ps, byte[] src, int srcIndex, int length ) { 58 if( length == 0 ) { 59 return; 60 } 61 62 int s = length % 16; 63 int r = ( s == 0 ) ? length / 16 : length / 16 + 1; 64 char[] c = new char[r * (74 + NL_LENGTH)]; 65 char[] d = new char[16]; 66 int i; 67 int si = 0; 68 int ci = 0; 69 70 do { 71 toHexChars( si, c, ci, 5 ); 72 ci += 5; 73 c[ci++] = ':'; 74 do { 75 if( si == length ) { 76 int n = 16 - s; 77 System.arraycopy( SPACE_CHARS, 0, c, ci, n * 3 ); 78 ci += n * 3; 79 System.arraycopy( SPACE_CHARS, 0, d, s, n ); 80 break; 81 } 82 c[ci++] = ' '; 83 i = src[srcIndex + si] & 0xFF; 84 toHexChars( i, c, ci, 2 ); 85 ci += 2; 86 if( i < 0 || Character.isISOControl( (char)i )) { 87 d[si % 16] = '.'; 88 } else { 89 d[si % 16] = (char)i; 90 } 91 } while(( ++si % 16 ) != 0 ); 92 c[ci++] = ' '; 93 c[ci++] = ' '; 94 c[ci++] = '|'; 95 System.arraycopy( d, 0, c, ci, 16 ); 96 ci += 16; 97 c[ci++] = '|'; 98 NL.getChars( 0, NL_LENGTH, c, ci ); 99 ci += NL_LENGTH; 100 } while( si < length ); 101 102 ps.println( c ); 103 } 104 105 110 public static String toHexString( int val, int size ) { 111 char[] c = new char[size]; 112 toHexChars( val, c, 0, size ); 113 return new String ( c ); 114 } 115 public static String toHexString( long val, int size ) { 116 char[] c = new char[size]; 117 toHexChars( val, c, 0, size ); 118 return new String ( c ); 119 } 120 public static String toHexString( byte[] src, int srcIndex, int size ) { 121 char[] c = new char[size]; 122 size = ( size % 2 == 0 ) ? size / 2 : size / 2 + 1; 123 for( int i = 0, j = 0; i < size; i++ ) { 124 c[j++] = HEX_DIGITS[(src[i] >> 4 ) & 0x0F]; 125 if( j == c.length ) { 126 break; 127 } 128 c[j++] = HEX_DIGITS[src[i] & 0x0F]; 129 } 130 return new String ( c ); 131 } 132 133 138 public static void toHexChars( int val, char dst[], int dstIndex, int size ) { 139 while( size > 0 ) { 140 int i = dstIndex + size - 1; 141 if( i < dst.length ) { 142 dst[i] = HEX_DIGITS[val & 0x000F]; 143 } 144 if( val != 0 ) { 145 val >>>= 4; 146 } 147 size--; 148 } 149 } 150 public static void toHexChars( long val, char dst[], int dstIndex, int size ) { 151 while( size > 0 ) { 152 dst[dstIndex + size - 1] = HEX_DIGITS[(int)( val & 0x000FL )]; 153 if( val != 0 ) { 154 val >>>= 4; 155 } 156 size--; 157 } 158 } 159 } 160 161 | Popular Tags |