1 20 package org.apache.mina.common.support; 21 22 import org.apache.mina.common.ByteBuffer; 23 24 30 public class ByteBufferHexDumper { 31 private static final byte[] highDigits; 32 33 private static final byte[] lowDigits; 34 35 static { 37 final byte[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', 38 '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 39 40 int i; 41 byte[] high = new byte[256]; 42 byte[] low = new byte[256]; 43 44 for (i = 0; i < 256; i++) { 45 high[i] = digits[i >>> 4]; 46 low[i] = digits[i & 0x0F]; 47 } 48 49 highDigits = high; 50 lowDigits = low; 51 } 52 53 public static String getHexdump(ByteBuffer in) { 54 int size = in.remaining(); 55 56 if (size == 0) { 57 return "empty"; 58 } 59 60 StringBuffer out = new StringBuffer ((in.remaining() * 3) - 1); 61 62 int mark = in.position(); 63 64 int byteValue = in.get() & 0xFF; 66 out.append((char) highDigits[byteValue]); 67 out.append((char) lowDigits[byteValue]); 68 size--; 69 70 for (; size > 0; size--) { 72 out.append(' '); 73 byteValue = in.get() & 0xFF; 74 out.append((char) highDigits[byteValue]); 75 out.append((char) lowDigits[byteValue]); 76 } 77 78 in.position(mark); 79 80 return out.toString(); 81 } 82 } | Popular Tags |