jdk/src/share/classes/sun/jkernel/ByteArrayToFromHexDigits.java
changeset 8197 e45f21c2a40b
parent 7867 f83cd8bd35c6
child 8198 aca2f99e4b52
equal deleted inserted replaced
7867:f83cd8bd35c6 8197:e45f21c2a40b
     1 /*
       
     2  * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 package sun.jkernel;
       
    27 
       
    28 /**
       
    29  * TODO: The JRE and deploy build code (SplitJRE) can be made a bit smarter
       
    30  * then cryto hashcode byte arrays can be used directly, eliminating the need
       
    31  * for this class altogether. So just leave this alone until it can be removed.
       
    32  * TODO: Change "Digits" to "String" for uniformity and more intuitive names.
       
    33  * A lightweight class to provide convert between hex digits and
       
    34  * <code>byte[]</code>.
       
    35  *<p>
       
    36  * TODO: Try to get this built without the -source 1.3 -target -1.3 options,
       
    37  * which prevent use of java.text.Format, assuming this wouldn't bloat the
       
    38  * JK rt.jar. Also, there still might be equivalent code hiding in the JDK
       
    39  * already, but preliminary searches havn't found it.
       
    40  */
       
    41 
       
    42 public final class ByteArrayToFromHexDigits {
       
    43 
       
    44     private static final char [] chars = new char[]
       
    45         {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F'};
       
    46 
       
    47     private static final boolean debug = false;
       
    48 
       
    49     /**
       
    50      * Converts the <code>byte[] b</code> into a <code>String</code> of
       
    51      * hex digits representing the integer values of all the bytes.
       
    52      *
       
    53      * @param b byte array to be converted
       
    54      * @return String representing <code>b</code> in hexadecimal
       
    55      * @throws IllegalArgumentException if <code>b</code> is null or zero length
       
    56      */
       
    57     public static String bytesToHexString(byte[] b) {
       
    58         if (debug ) {
       
    59             System.out.print("I: ");
       
    60             for(int i=0;i<b.length;i++) {
       
    61                 System.out.format("%02X",b[i]);
       
    62             }
       
    63             System.out.println();
       
    64         }
       
    65         if ((b == null) || (b.length == 0)) {
       
    66             throw new IllegalArgumentException("argument null or zero length");
       
    67         }
       
    68         StringBuffer buff = new StringBuffer(b.length * 2);
       
    69         for (int i = 0; i < b.length; i++ ) {
       
    70             buff.insert(i*2,chars[(b[i] >> 4) & 0xf]);
       
    71             buff.insert(i*2+1,chars[b[i] & 0xf]);
       
    72         }
       
    73         if (debug ) {
       
    74             System.out.println("O: " + buff.toString());
       
    75         }
       
    76         return buff.toString();
       
    77     }
       
    78 
       
    79     // Convert one hex character to a 4 bit byte value
       
    80 
       
    81     private static byte hexCharToByte(char c) throws IllegalArgumentException {
       
    82         if ((c < '0') ||
       
    83             ( ((c < 'A') && (c > 'F')) && ((c < 'a') && (c > 'f'))) ) {
       
    84 
       
    85             throw new IllegalArgumentException("not a hex digit");
       
    86         }
       
    87 
       
    88         if (c > '9') {
       
    89             if (c > 'F') {
       
    90                 return (byte) ((c - 'a' + 10) & 0xf);
       
    91             } else {
       
    92                 return (byte) ((c - 'A' + 10) & 0xf);
       
    93             }
       
    94         } else {
       
    95             return (byte) ((c - '0') & 0xf);
       
    96         }
       
    97 
       
    98     }
       
    99 
       
   100     /**
       
   101      * Converts the <code>String d</code> assumed to contain a sequence
       
   102      * of hexadecimal digit characters into a <code>byte[]</code>.
       
   103      *
       
   104      * @param d String to be converted
       
   105      * @return  byte array representing the hex string
       
   106      * @throws IllegalArgumentException if <code>d</code> is odd length,
       
   107      *     contains a character outside the ranges of 0-9, a-f, and A-F,
       
   108      *     or is zero length or null
       
   109      */
       
   110 
       
   111     public static byte[] hexStringToBytes(String d) throws IllegalArgumentException {
       
   112         if (d == null) {
       
   113             throw new IllegalArgumentException(
       
   114                 "parameter cannot be null");
       
   115         }
       
   116 
       
   117         if (d.length() == 0) {
       
   118             throw new IllegalArgumentException(
       
   119                 "parameter cannot be zero length");
       
   120         }
       
   121 
       
   122         if ((d.length() & 1) != 0) {
       
   123             throw new IllegalArgumentException(
       
   124                 "odd length string");
       
   125         }
       
   126 
       
   127         byte[] b = new byte[d.length() / 2];
       
   128 
       
   129         // TODO Might be code in the JK initial bundle to do this better (i.e.
       
   130         // method that tests for a hex char?)
       
   131 
       
   132         for (int i=0;i<d.length();i+=2) {
       
   133             b[i/2] =  (byte) (( (byte) (hexCharToByte(d.charAt(i))) << 4) +
       
   134                 (byte) hexCharToByte(d.charAt(i+1)));
       
   135         }
       
   136         return b;
       
   137     }
       
   138 }