Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » util » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans.impl.util;
   17   
   18   import java.io.UnsupportedEncodingException;
   19   /**
   20    * format validation
   21    *
   22    * This class encodes/decodes hexadecimal data
   23    * @author Jeffrey Rodriguez
   24    * @version $Id: HexBin.java 125124 2005-01-14 00:23:54Z kkrouse $
   25    */
   26   public final class  HexBin {
   27       static private final int  BASELENGTH   = 255;
   28       static private final int  LOOKUPLENGTH = 16;
   29       static private byte [] hexNumberTable    = new byte[BASELENGTH];
   30       static private byte [] lookUpHexAlphabet = new byte[LOOKUPLENGTH];
   31   
   32   
   33       static {
   34           for (int i = 0; i<BASELENGTH; i++ ) {
   35               hexNumberTable[i] = -1;
   36           }
   37           for ( int i = '9'; i >= '0'; i--) {
   38               hexNumberTable[i] = (byte) (i-'0');
   39           }
   40           for ( int i = 'F'; i>= 'A'; i--) {
   41               hexNumberTable[i] = (byte) ( i-'A' + 10 );
   42           }
   43           for ( int i = 'f'; i>= 'a'; i--) {
   44              hexNumberTable[i] = (byte) ( i-'a' + 10 );
   45           }
   46   
   47           for(int i = 0; i<10; i++ )
   48               lookUpHexAlphabet[i] = (byte) ('0'+i );
   49           for(int i = 10; i<=15; i++ )
   50               lookUpHexAlphabet[i] = (byte) ('A'+i -10);
   51       }
   52   
   53       /**
   54        * byte to be tested if it is Base64 alphabet
   55        *
   56        * @param octect
   57        * @return
   58        */
   59       static boolean isHex(byte octect) {
   60           return (hexNumberTable[octect] != -1);
   61       }
   62   
   63       /**
   64        * Converts bytes to a hex string
   65        */
   66       static public String bytesToString(byte[] binaryData)
   67       {
   68           if (binaryData == null)
   69               return null;
   70           return new String(encode(binaryData));
   71       }
   72   
   73       /**
   74        * Converts a hex string to a byte array.
   75        */
   76       static public byte[] stringToBytes(String hexEncoded)
   77       {
   78           return decode(hexEncoded.getBytes());
   79       }
   80   
   81       /**
   82        * array of byte to encode
   83        *
   84        * @param binaryData
   85        * @return return encode binary array
   86        */
   87       static public byte[] encode(byte[] binaryData) {
   88           if (binaryData == null)
   89               return null;
   90           int lengthData   = binaryData.length;
   91           int lengthEncode = lengthData * 2;
   92           byte[] encodedData = new byte[lengthEncode];
   93           for( int i = 0; i<lengthData; i++ ){
   94               encodedData[i*2] = lookUpHexAlphabet[(binaryData[i] >> 4) & 0xf];
   95               encodedData[i*2+1] = lookUpHexAlphabet[ binaryData[i] & 0xf];
   96           }
   97           return encodedData;
   98       }
   99   
  100       static public byte[] decode(byte[] binaryData) {
  101           if (binaryData == null)
  102               return null;
  103           int lengthData   = binaryData.length;
  104           if (lengthData % 2 != 0)
  105               return null;
  106   
  107           int lengthDecode = lengthData / 2;
  108           byte[] decodedData = new byte[lengthDecode];
  109           for( int i = 0; i<lengthDecode; i++ ){
  110               if (!isHex(binaryData[i*2]) || !isHex(binaryData[i*2+1])) {
  111                   return null;
  112               }
  113               decodedData[i] = (byte)((hexNumberTable[binaryData[i*2]] << 4) | hexNumberTable[binaryData[i*2+1]]);
  114           }
  115           return decodedData;
  116       }
  117   
  118       /**
  119        * Decodes Hex data into octects
  120        *
  121        * @param binaryData String containing Hex data
  122        * @return string containing decoded data.
  123        */
  124       public static String decode(String binaryData) {
  125           if (binaryData == null)
  126               return null;
  127   
  128           byte[] decoded = null;
  129           try {
  130             decoded = decode(binaryData.getBytes("utf-8"));
  131           }
  132           catch(UnsupportedEncodingException e) {
  133           }
  134           return decoded == null ? null : new String(decoded);
  135       }
  136   
  137       /**
  138        * Encodes octects (using utf-8) into Hex data
  139        *
  140        * @param binaryData String containing Hex data
  141        * @return string containing decoded data.
  142        */
  143       public static String encode(String binaryData) {
  144           if (binaryData == null)
  145               return null;
  146   
  147           byte[] encoded = null;
  148           try {
  149             encoded = encode(binaryData.getBytes("utf-8"));
  150           }
  151           catch(UnsupportedEncodingException e) {}
  152           return encoded == null ? null : new String(encoded);
  153       }
  154   
  155   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.impl » util » [javadoc | source]