1 17 18 package org.apache.geronimo.util.encoders; 19 20 23 public class HexTranslator 24 implements Translator 25 { 26 private static final byte[] hexTable = 27 { 28 (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', 29 (byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f' 30 }; 31 32 36 public int getEncodedBlockSize() 37 { 38 return 2; 39 } 40 41 public int encode( 42 byte[] in, 43 int inOff, 44 int length, 45 byte[] out, 46 int outOff) 47 { 48 for (int i = 0, j = 0; i < length; i++, j += 2) 49 { 50 out[outOff + j] = hexTable[(in[inOff] >> 4) & 0x0f]; 51 out[outOff + j + 1] = hexTable[in[inOff] & 0x0f]; 52 53 inOff++; 54 } 55 56 return length * 2; 57 } 58 59 63 public int getDecodedBlockSize() 64 { 65 return 1; 66 } 67 68 public int decode( 69 byte[] in, 70 int inOff, 71 int length, 72 byte[] out, 73 int outOff) 74 { 75 int halfLength = length / 2; 76 byte left, right; 77 for (int i = 0; i < halfLength; i++) 78 { 79 left = in[inOff + i * 2]; 80 right = in[inOff + i * 2 + 1]; 81 82 if (left < (byte)'a') 83 { 84 out[outOff] = (byte)((left - '0') << 4); 85 } 86 else 87 { 88 out[outOff] = (byte)((left - 'a' + 10) << 4); 89 } 90 if (right < (byte)'a') 91 { 92 out[outOff] += (byte)(right - '0'); 93 } 94 else 95 { 96 out[outOff] += (byte)(right - 'a' + 10); 97 } 98 99 outOff++; 100 } 101 102 return halfLength; 103 } 104 } 105 | Popular Tags |