1 2 17 18 19 package org.apache.poi.contrib.poibrowser; 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 import java.io.InputStreamReader ; 24 25 import org.apache.poi.hpsf.ClassID; 26 27 28 29 37 public class Codec 38 { 39 40 43 protected static final byte hexval[] = 44 {(byte) '0', (byte) '1', (byte) '2', (byte) '3', 45 (byte) '4', (byte) '5', (byte) '6', (byte) '7', 46 (byte) '8', (byte) '9', (byte) 'A', (byte) 'B', 47 (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F'}; 48 49 50 51 54 public static String hexEncode(final String s) 55 { 56 return hexEncode(s.getBytes()); 57 } 58 59 60 61 64 public static String hexEncode(final byte[] s) 65 { 66 return hexEncode(s, 0, s.length); 67 } 68 69 70 71 75 public static String hexEncode(final byte[] s, final int offset, 76 final int length) 77 { 78 StringBuffer b = new StringBuffer (length * 2); 79 for (int i = offset; i < offset + length; i++) 80 { 81 int c = s[i]; 82 b.append((char) hexval[(c & 0xF0) >> 4]); 83 b.append((char) hexval[(c & 0x0F) >> 0]); 84 } 85 return b.toString(); 86 } 87 88 89 90 93 public static String hexEncode(final byte b) 94 { 95 StringBuffer sb = new StringBuffer (2); 96 sb.append((char) hexval[(b & 0xF0) >> 4]); 97 sb.append((char) hexval[(b & 0x0F) >> 0]); 98 return sb.toString(); 99 } 100 101 102 103 107 public static String hexEncode(final short s) 108 { 109 StringBuffer sb = new StringBuffer (4); 110 sb.append((char) hexval[(s & 0xF000) >> 12]); 111 sb.append((char) hexval[(s & 0x0F00) >> 8]); 112 sb.append((char) hexval[(s & 0x00F0) >> 4]); 113 sb.append((char) hexval[(s & 0x000F) >> 0]); 114 return sb.toString(); 115 } 116 117 118 119 123 public static String hexEncode(final int i) 124 { 125 StringBuffer sb = new StringBuffer (8); 126 sb.append((char) hexval[(i & 0xF0000000) >> 28]); 127 sb.append((char) hexval[(i & 0x0F000000) >> 24]); 128 sb.append((char) hexval[(i & 0x00F00000) >> 20]); 129 sb.append((char) hexval[(i & 0x000F0000) >> 16]); 130 sb.append((char) hexval[(i & 0x0000F000) >> 12]); 131 sb.append((char) hexval[(i & 0x00000F00) >> 8]); 132 sb.append((char) hexval[(i & 0x000000F0) >> 4]); 133 sb.append((char) hexval[(i & 0x0000000F) >> 0]); 134 return sb.toString(); 135 } 136 137 138 139 143 public static String hexEncode(final long l) 144 { 145 StringBuffer sb = new StringBuffer (16); 146 sb.append(hexEncode((int) (l & 0xFFFFFFFF00000000L) >> 32)); 147 sb.append(hexEncode((int) (l & 0x00000000FFFFFFFFL) >> 0)); 148 return sb.toString(); 149 } 150 151 152 153 156 public static String hexEncode(final ClassID classID) 157 { 158 return hexEncode(classID.getBytes()); 159 } 160 161 162 163 176 public static byte[] hexDecode(final String s) 177 { 178 final int length = s.length(); 179 180 182 if (length % 2 == 1) 183 throw new IllegalArgumentException 184 ("String has odd length " + length); 185 byte[] b = new byte[length / 2]; 186 char[] c = new char[length]; 187 s.toUpperCase().getChars(0, length, c, 0); 188 for (int i = 0; i < length; i += 2) 189 b[i/2] = (byte) (decodeNibble(c[i]) << 4 & 0xF0 | 190 decodeNibble(c[i+1]) & 0x0F); 191 return b; 192 } 193 194 195 196 207 protected static byte decodeNibble(final char c) 208 { 209 for (byte i = 0; i < hexval.length; i++) 210 if ((byte) c == hexval[i]) 211 return i; 212 throw new IllegalArgumentException ("\"" + c + "\"" + 213 " does not represent a nibble."); 214 } 215 216 217 218 221 public static void main(final String args[]) 222 throws IOException 223 { 224 final BufferedReader in = 225 new BufferedReader (new InputStreamReader (System.in)); 226 String s; 227 do 228 { 229 s = in.readLine(); 230 if (s != null) 231 { 232 String bytes = hexEncode(s); 233 System.out.print("Hex encoded (String): "); 234 System.out.println(bytes); 235 System.out.print("Hex encoded (byte[]): "); 236 System.out.println(hexEncode(s.getBytes())); 237 System.out.print("Re-decoded (byte[]): "); 238 System.out.println(new String (hexDecode(bytes))); 239 } 240 } 241 while (s != null); 242 } 243 244 } 245 | Popular Tags |