KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > HexUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.util;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22
23 /**
24  * Library of utility methods useful in dealing with converting byte arrays
25  * to and from strings of hexadecimal digits.
26  *
27  * @author Craig R. McClanahan
28  */

29
30 public final class HexUtils {
31     // Code from Ajp11, from Apache's JServ
32

33     // Table for HEX to DEC byte translation
34
public static final int[] DEC = {
35         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
37         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
38         00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1,
39         -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41         -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51     };
52
53
54
55     /**
56      * The string manager for this package.
57      */

58     private static StringManager sm =
59         StringManager.getManager("org.apache.catalina.util");
60
61
62     /**
63      * Convert a String of hexadecimal digits into the corresponding
64      * byte array by encoding each two hexadecimal digits as a byte.
65      *
66      * @param digits Hexadecimal digits representation
67      *
68      * @exception IllegalArgumentException if an invalid hexadecimal digit
69      * is found, or the input string contains an odd number of hexadecimal
70      * digits
71      */

72     public static byte[] convert(String JavaDoc digits) {
73
74         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
75         for (int i = 0; i < digits.length(); i += 2) {
76             char c1 = digits.charAt(i);
77             if ((i+1) >= digits.length())
78                 throw new IllegalArgumentException JavaDoc
79                     (sm.getString("hexUtil.odd"));
80             char c2 = digits.charAt(i + 1);
81             byte b = 0;
82             if ((c1 >= '0') && (c1 <= '9'))
83                 b += ((c1 - '0') * 16);
84             else if ((c1 >= 'a') && (c1 <= 'f'))
85                 b += ((c1 - 'a' + 10) * 16);
86             else if ((c1 >= 'A') && (c1 <= 'F'))
87                 b += ((c1 - 'A' + 10) * 16);
88             else
89                 throw new IllegalArgumentException JavaDoc
90                     (sm.getString("hexUtil.bad"));
91             if ((c2 >= '0') && (c2 <= '9'))
92                 b += (c2 - '0');
93             else if ((c2 >= 'a') && (c2 <= 'f'))
94                 b += (c2 - 'a' + 10);
95             else if ((c2 >= 'A') && (c2 <= 'F'))
96                 b += (c2 - 'A' + 10);
97             else
98                 throw new IllegalArgumentException JavaDoc
99                     (sm.getString("hexUtil.bad"));
100             baos.write(b);
101         }
102         return (baos.toByteArray());
103
104     }
105
106
107     /**
108      * Convert a byte array into a printable format containing a
109      * String of hexadecimal digit characters (two per byte).
110      *
111      * @param bytes Byte array representation
112      */

113     public static String JavaDoc convert(byte bytes[]) {
114
115         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(bytes.length * 2);
116         for (int i = 0; i < bytes.length; i++) {
117             sb.append(convertDigit((int) (bytes[i] >> 4)));
118             sb.append(convertDigit((int) (bytes[i] & 0x0f)));
119         }
120         return (sb.toString());
121
122     }
123
124     /**
125      * Convert 4 hex digits to an int, and return the number of converted
126      * bytes.
127      *
128      * @param hex Byte array containing exactly four hexadecimal digits
129      *
130      * @exception IllegalArgumentException if an invalid hexadecimal digit
131      * is included
132      */

133     public static int convert2Int( byte[] hex ) {
134         // Code from Ajp11, from Apache's JServ
135

136         // assert b.length==4
137
// assert valid data
138
int len;
139         if(hex.length < 4 ) return 0;
140         if( DEC[hex[0]]<0 )
141             throw new IllegalArgumentException JavaDoc(sm.getString("hexUtil.bad"));
142         len = DEC[hex[0]];
143         len = len << 4;
144         if( DEC[hex[1]]<0 )
145             throw new IllegalArgumentException JavaDoc(sm.getString("hexUtil.bad"));
146         len += DEC[hex[1]];
147         len = len << 4;
148         if( DEC[hex[2]]<0 )
149             throw new IllegalArgumentException JavaDoc(sm.getString("hexUtil.bad"));
150         len += DEC[hex[2]];
151         len = len << 4;
152         if( DEC[hex[3]]<0 )
153             throw new IllegalArgumentException JavaDoc(sm.getString("hexUtil.bad"));
154         len += DEC[hex[3]];
155         return len;
156     }
157
158
159
160     /**
161      * [Private] Convert the specified value (0 .. 15) to the corresponding
162      * hexadecimal digit.
163      *
164      * @param value Value to be converted
165      */

166     private static char convertDigit(int value) {
167
168         value &= 0x0f;
169         if (value >= 10)
170             return ((char) (value - 10 + 'a'));
171         else
172             return ((char) (value + '0'));
173
174     }
175
176
177 }
178
Popular Tags