Home » apache-tomcat-6.0.26-src » org.apache » tomcat » util » [javadoc | source]

    1   /*
    2    * $Header: /u/cvs/Projects/EnhydraOrg/enhydra3x/Enhydra/modules/Tomcat/src/share/org/apache/tomcat/util/HexUtils.java,v 1.2 2000/02/26 02:32:26 shawn Exp $
    3    * $Revision: 1.2 $
    4    * $Date: 2000/02/26 02:32:26 $
    5    *
    6    * ====================================================================
    7    *
    8    * The Apache Software License, Version 1.1
    9    *
   10    * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   11    * reserved.
   12    *
   13    * Redistribution and use in source and binary forms, with or without
   14    * modification, are permitted provided that the following conditions
   15    * are met:
   16    *
   17    * 1. Redistributions of source code must retain the above copyright
   18    *    notice, this list of conditions and the following disclaimer. 
   19    *
   20    * 2. Redistributions in binary form must reproduce the above copyright
   21    *    notice, this list of conditions and the following disclaimer in
   22    *    the documentation and/or other materials provided with the
   23    *    distribution.
   24    *
   25    * 3. The end-user documentation included with the redistribution, if
   26    *    any, must include the following acknowlegement:  
   27    *       "This product includes software developed by the 
   28    *        Apache Software Foundation (http://www.apache.org/)."
   29    *    Alternately, this acknowlegement may appear in the software itself,
   30    *    if and wherever such third-party acknowlegements normally appear.
   31    *
   32    * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   33    *    Foundation" must not be used to endorse or promote products derived
   34    *    from this software without prior written permission. For written 
   35    *    permission, please contact apache@apache.org.
   36    *
   37    * 5. Products derived from this software may not be called "Apache"
   38    *    nor may "Apache" appear in their names without prior written
   39    *    permission of the Apache Group.
   40    *
   41    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   42    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   43    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   44    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   45    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   46    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   47    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   48    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   49    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   50    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   51    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   52    * SUCH DAMAGE.
   53    * ====================================================================
   54    *
   55    * This software consists of voluntary contributions made by many
   56    * individuals on behalf of the Apache Software Foundation.  For more
   57    * information on the Apache Software Foundation, please see
   58    * <http://www.apache.org/>.
   59    *
   60    * [Additional notices, if required by prior licensing conditions]
   61    *
   62    */ 
   63   
   64   
   65   package org.apache.tomcat.util;
   66   
   67   import java.io.ByteArrayOutputStream;
   68   
   69   /**
   70    * Library of utility methods useful in dealing with converting byte arrays
   71    * to and from strings of hexadecimal digits.
   72    *
   73    * @author Craig R. McClanahan
   74    */
   75   
   76   public final class HexUtils {
   77       // Code from Ajp11, from Apache's JServ
   78       
   79       // Table for HEX to DEC byte translation
   80       public static final int[] DEC = {
   81           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   82           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   83           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   84           00, 01, 02, 03, 04, 05, 06, 07,  8,  9, -1, -1, -1, -1, -1, -1,
   85           -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   86           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   87           -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   88           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   89           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   90           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   91           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   92           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   93           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   94           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   95           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   96           -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
   97       };
   98       
   99   
  100   
  101       /**
  102        * The string manager for this package.
  103        */
  104       private static StringManager sm =
  105   	StringManager.getManager(Constants.Package);
  106   
  107   
  108       /**
  109        * Convert a String of hexadecimal digits into the corresponding
  110        * byte array by encoding each two hexadecimal digits as a byte.
  111        *
  112        * @param digits Hexadecimal digits representation
  113        *
  114        * @exception IllegalArgumentException if an invalid hexadecimal digit
  115        *  is found, or the input string contains an odd number of hexadecimal
  116        *  digits
  117        */
  118       public static byte[] convert(String digits) {
  119   
  120   	ByteArrayOutputStream baos = new ByteArrayOutputStream();
  121   	for (int i = 0; i < digits.length(); i += 2) {
  122   	    char c1 = digits.charAt(i);
  123   	    if ((i+1) >= digits.length())
  124   		throw new IllegalArgumentException
  125   		    (sm.getString("hexUtil.odd"));
  126   	    char c2 = digits.charAt(i + 1);
  127   	    byte b = 0;
  128   	    if ((c1 >= '0') && (c1 <= '9'))
  129   		b += ((c1 - '0') * 16);
  130   	    else if ((c1 >= 'a') && (c1 <= 'f'))
  131   		b += ((c1 - 'a' + 10) * 16);
  132   	    else if ((c1 >= 'A') && (c1 <= 'F'))
  133   		b += ((c1 - 'A' + 10) * 16);
  134   	    else
  135   		throw new IllegalArgumentException
  136   		    (sm.getString("hexUtil.bad"));
  137   	    if ((c2 >= '0') && (c2 <= '9'))
  138   		b += (c2 - '0');
  139   	    else if ((c2 >= 'a') && (c2 <= 'f'))
  140   		b += (c2 - 'a' + 10);
  141   	    else if ((c2 >= 'A') && (c2 <= 'F'))
  142   		b += (c2 - 'A' + 10);
  143   	    else
  144   		throw new IllegalArgumentException
  145   		    (sm.getString("hexUtil.bad"));
  146   	    baos.write(b);
  147   	}
  148   	return (baos.toByteArray());
  149   
  150       }
  151   
  152   
  153       /**
  154        * Convert a byte array into a printable format containing a
  155        * String of hexadecimal digit characters (two per byte).
  156        *
  157        * @param bytes Byte array representation
  158        */
  159       public static String convert(byte bytes[]) {
  160   
  161   	StringBuffer sb = new StringBuffer(bytes.length * 2);
  162   	for (int i = 0; i < bytes.length; i++) {
  163   	    sb.append(convertDigit((int) (bytes[i] >> 4)));
  164   	    sb.append(convertDigit((int) (bytes[i] & 0x0f)));
  165   	}
  166   	return (sb.toString());
  167   
  168       }
  169   
  170       /**
  171        * Convert 4 hex digits to an int, and return the number of converted
  172        * bytes.
  173        *
  174        * @param hex Byte array containing exactly four hexadecimal digits
  175        *
  176        * @exception IllegalArgumentException if an invalid hexadecimal digit
  177        *  is included
  178        */
  179       public static int convert2Int( byte[] hex ) {
  180   	// Code from Ajp11, from Apache's JServ
  181       
  182   	// assert b.length==4
  183   	// assert valid data
  184   	int len;
  185   	if(hex.length < 4 ) return 0;
  186   	if( DEC[hex[0]]<0 )
  187   	    throw new IllegalArgumentException(sm.getString("hexUtil.bad"));
  188   	len = DEC[hex[0]];
  189   	len = len << 4;
  190   	if( DEC[hex[1]]<0 )
  191   	    throw new IllegalArgumentException(sm.getString("hexUtil.bad"));
  192   	len += DEC[hex[1]];
  193   	len = len << 4;
  194   	if( DEC[hex[2]]<0 )
  195   	    throw new IllegalArgumentException(sm.getString("hexUtil.bad"));
  196   	len += DEC[hex[2]];
  197   	len = len << 4;
  198   	if( DEC[hex[3]]<0 )
  199   	    throw new IllegalArgumentException(sm.getString("hexUtil.bad"));
  200   	len += DEC[hex[3]];
  201   	return len;
  202       }
  203   
  204   
  205   
  206       /**
  207        * [Private] Convert the specified value (0 .. 15) to the corresponding
  208        * hexadecimal digit.
  209        *
  210        * @param value Value to be converted
  211        */
  212       private static char convertDigit(int value) {
  213   
  214   	value &= 0x0f;
  215   	if (value >= 10)
  216   	    return ((char) (value - 10 + 'a'));
  217   	else
  218   	    return ((char) (value + '0'));
  219   
  220       }
  221   
  222   
  223   }

Home » apache-tomcat-6.0.26-src » org.apache » tomcat » util » [javadoc | source]