001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.dv.util;
019:
020: /**
021: * format validation
022: *
023: * This class encodes/decodes hexadecimal data
024: *
025: * @xerces.internal
026: *
027: * @author Jeffrey Rodriguez
028: * @version $Id: HexBin.java 446747 2006-09-15 21:46:20Z mrglavas $
029: */
030: public final class HexBin {
031: static private final int BASELENGTH = 128;
032: static private final int LOOKUPLENGTH = 16;
033: static final private byte[] hexNumberTable = new byte[BASELENGTH];
034: static final private char[] lookUpHexAlphabet = new char[LOOKUPLENGTH];
035:
036: static {
037: for (int i = 0; i < BASELENGTH; i++) {
038: hexNumberTable[i] = -1;
039: }
040: for (int i = '9'; i >= '0'; i--) {
041: hexNumberTable[i] = (byte) (i - '0');
042: }
043: for (int i = 'F'; i >= 'A'; i--) {
044: hexNumberTable[i] = (byte) (i - 'A' + 10);
045: }
046: for (int i = 'f'; i >= 'a'; i--) {
047: hexNumberTable[i] = (byte) (i - 'a' + 10);
048: }
049:
050: for (int i = 0; i < 10; i++) {
051: lookUpHexAlphabet[i] = (char) ('0' + i);
052: }
053: for (int i = 10; i <= 15; i++) {
054: lookUpHexAlphabet[i] = (char) ('A' + i - 10);
055: }
056: }
057:
058: /**
059: * Encode a byte array to hex string
060: *
061: * @param binaryData array of byte to encode
062: * @return return encoded string
063: */
064: static public String encode(byte[] binaryData) {
065: if (binaryData == null)
066: return null;
067: int lengthData = binaryData.length;
068: int lengthEncode = lengthData * 2;
069: char[] encodedData = new char[lengthEncode];
070: int temp;
071: for (int i = 0; i < lengthData; i++) {
072: temp = binaryData[i];
073: if (temp < 0)
074: temp += 256;
075: encodedData[i * 2] = lookUpHexAlphabet[temp >> 4];
076: encodedData[i * 2 + 1] = lookUpHexAlphabet[temp & 0xf];
077: }
078: return new String(encodedData);
079: }
080:
081: /**
082: * Decode hex string to a byte array
083: *
084: * @param encoded encoded string
085: * @return return array of byte to encode
086: */
087: static public byte[] decode(String encoded) {
088: if (encoded == null)
089: return null;
090: int lengthData = encoded.length();
091: if (lengthData % 2 != 0)
092: return null;
093:
094: char[] binaryData = encoded.toCharArray();
095: int lengthDecode = lengthData / 2;
096: byte[] decodedData = new byte[lengthDecode];
097: byte temp1, temp2;
098: char tempChar;
099: for (int i = 0; i < lengthDecode; i++) {
100: tempChar = binaryData[i * 2];
101: temp1 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar]
102: : -1;
103: if (temp1 == -1)
104: return null;
105: tempChar = binaryData[i * 2 + 1];
106: temp2 = (tempChar < BASELENGTH) ? hexNumberTable[tempChar]
107: : -1;
108: if (temp2 == -1)
109: return null;
110: decodedData[i] = (byte) ((temp1 << 4) | temp2);
111: }
112: return decodedData;
113: }
114: }
|