1 /* 2 * Copyright 1999-2002,2004 The Apache Software Foundation. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.servingxml.util; 18 19 /** 20 * format validation 21 * 22 * This class encodes/decodes hexadecimal data 23 * 24 * @author Jeffrey Rodriguez 25 * @version $Id: HexBin.java 320108 2004-10-14 15:20:18Z mrglavas $ 26 */ 27 public final class HexBin { 28 static private final int BASELENGTH = 128; 29 static private final int LOOKUPLENGTH = 16; 30 static final public byte[] hexNumberTable = new byte[BASELENGTH]; 31 static final public char[] lookUpHexAlphabet = { '0', '1', '2', '3', '4', '5', '6', '7', '8', 32 '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 33 34 static { 35 for (int i = 0; i < BASELENGTH; i++ ) { 36 hexNumberTable[i] = -1; 37 } 38 for ( int i = '9'; i >= '0'; i--) { 39 hexNumberTable[i] = (byte) (i-'0'); 40 } 41 for ( int i = 'F'; i >= 'A'; i--) { 42 hexNumberTable[i] = (byte) ( i-'A' + 10 ); 43 } 44 for ( int i = 'f'; i >= 'a'; i--) { 45 hexNumberTable[i] = (byte) ( i-'a' + 10 ); 46 } 47 } 48 49 /** 50 * Encode a byte array to hex string 51 * 52 * @param binaryData array of byte to encode 53 * @return return encoded string 54 */ encode(byte[] binaryData)55 static public String encode(byte[] binaryData) { 56 if (binaryData == null) 57 return null; 58 int lengthData = binaryData.length; 59 int lengthEncode = lengthData * 2; 60 char[] encodedData = new char[lengthEncode]; 61 int temp; 62 for (int i = 0; i < lengthData; i++) { 63 temp = binaryData[i]; 64 if (temp < 0) 65 temp += 256; 66 encodedData[i*2] = lookUpHexAlphabet[temp >> 4]; 67 encodedData[i*2+1] = lookUpHexAlphabet[temp & 0xf]; 68 } 69 return new String(encodedData); 70 } encode(byte[] binaryData, int start, int lengthData)71 static public String encode(byte[] binaryData, int start, int lengthData) { 72 if (binaryData == null) 73 return null; 74 //int lengthData = binaryData.length; 75 int lengthEncode = lengthData * 2; 76 char[] encodedData = new char[lengthEncode]; 77 int temp; 78 for (int i = start; i < lengthData; i++) { 79 temp = binaryData[i]; 80 if (temp < 0) 81 temp += 256; 82 encodedData[i*2] = lookUpHexAlphabet[temp >> 4]; 83 encodedData[i*2+1] = lookUpHexAlphabet[temp & 0xf]; 84 } 85 return new String(encodedData); 86 } 87 88 /** 89 * Decode hex string to a byte array 90 * 91 * @param encoded encoded string 92 * @return return array of byte to encode 93 */ decode(String encoded)94 static public byte[] decode(String encoded) { 95 if (encoded == null) 96 return null; 97 int lengthData = encoded.length(); 98 if (lengthData % 2 != 0) 99 return null; 100 101 char[] binaryData = encoded.toCharArray(); 102 int lengthDecode = lengthData / 2; 103 byte[] decodedData = new byte[lengthDecode]; 104 byte temp1, temp2; 105 char tempChar; 106 for ( int i = 0; i<lengthDecode; i++ ) { 107 tempChar = binaryData[i*2]; 108 temp1 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar] : -1; 109 if (temp1 == -1) 110 return null; 111 tempChar = binaryData[i*2+1]; 112 temp2 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar] : -1; 113 if (temp2 == -1) 114 return null; 115 decodedData[i] = (byte)((temp1 << 4) | temp2); 116 } 117 return decodedData; 118 } 119 } 120