KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > encoding > Hex


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.apache.soap.encoding ;
56
57 import java.io.ByteArrayOutputStream JavaDoc;
58
59 /**
60  * Custom class for supporting primitive XSD data type hexBinary.
61  *
62  * @author Davanum Srinivas <dims@yahoo.com>
63  */

64 public class Hex extends Object JavaDoc{
65
66     byte[] m_value = null;
67
68     public Hex() {
69     }
70
71     public Hex(String JavaDoc string){
72         m_value = decode(string);
73     }
74
75     public byte[] getBytes(){
76         return m_value;
77     }
78
79     public String JavaDoc toString(){
80         return encode(m_value);
81     }
82
83     public int hashCode(){
84         //TODO: How do we hash this?
85
return super.hashCode();
86     }
87
88     public boolean equals(java.lang.Object JavaDoc object){
89         //TODO: Is this good enough?
90
String JavaDoc s1 = object.toString();
91         String JavaDoc s2 = this.toString();
92         return s1.equals(s2);
93     }
94
95     public static final String JavaDoc ERROR_ODD_NUMBER_OF_DIGITS =
96             "Odd number of digits in hex string";
97     public static final String JavaDoc ERROR_BAD_CHARACTER_IN_HEX_STRING =
98             "Bad character or insufficient number of characters in hex string";
99
100     // Code from Ajp11, from Apache's JServ
101

102     // Table for HEX to DEC byte translation
103
public static final int[] DEC = {
104         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
105         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
106         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
107         00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1,
108         -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
109         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
110         -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
111         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
112         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
113         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
114         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
115         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
116         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
117         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
118         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
119         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
120     };
121
122     /**
123      * Convert a String of hexadecimal digits into the corresponding
124      * byte array by encoding each two hexadecimal digits as a byte.
125      *
126      * @param digits Hexadecimal digits representation
127      *
128      * @exception IllegalArgumentException if an invalid hexadecimal digit
129      * is found, or the input string contains an odd number of hexadecimal
130      * digits
131      */

132     public static byte[] decode(String JavaDoc digits) {
133
134         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
135         for (int i = 0; i < digits.length(); i += 2) {
136             char c1 = digits.charAt(i);
137             if ((i+1) >= digits.length())
138                 throw new IllegalArgumentException JavaDoc
139                     (ERROR_ODD_NUMBER_OF_DIGITS);
140             char c2 = digits.charAt(i + 1);
141             byte b = 0;
142             if ((c1 >= '0') && (c1 <= '9'))
143                 b += ((c1 - '0') * 16);
144             else if ((c1 >= 'a') && (c1 <= 'f'))
145                 b += ((c1 - 'a' + 10) * 16);
146             else if ((c1 >= 'A') && (c1 <= 'F'))
147                 b += ((c1 - 'A' + 10) * 16);
148             else
149                 throw new IllegalArgumentException JavaDoc
150                     (ERROR_BAD_CHARACTER_IN_HEX_STRING);
151             if ((c2 >= '0') && (c2 <= '9'))
152                 b += (c2 - '0');
153             else if ((c2 >= 'a') && (c2 <= 'f'))
154                 b += (c2 - 'a' + 10);
155             else if ((c2 >= 'A') && (c2 <= 'F'))
156                 b += (c2 - 'A' + 10);
157             else
158                 throw new IllegalArgumentException JavaDoc
159                     (ERROR_BAD_CHARACTER_IN_HEX_STRING);
160             baos.write(b);
161         }
162         return (baos.toByteArray());
163
164     }
165
166
167     /**
168      * Convert a byte array into a printable format containing a
169      * String of hexadecimal digit characters (two per byte).
170      *
171      * @param bytes Byte array representation
172      */

173     public static String JavaDoc encode(byte bytes[]) {
174
175         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(bytes.length * 2);
176         for (int i = 0; i < bytes.length; i++) {
177             sb.append(convertDigit((int) (bytes[i] >> 4)));
178             sb.append(convertDigit((int) (bytes[i] & 0x0f)));
179         }
180         return (sb.toString());
181
182     }
183
184     /**
185      * Convert 4 hex digits to an int, and return the number of converted
186      * bytes.
187      *
188      * @param hex Byte array containing exactly four hexadecimal digits
189      *
190      * @exception IllegalArgumentException if an invalid hexadecimal digit
191      * is included
192      */

193     public static int convert2Int( byte[] hex ) {
194         // Code from Ajp11, from Apache's JServ
195

196         // assert b.length==4
197
// assert valid data
198
int len;
199         if(hex.length < 4 ) return 0;
200         if( DEC[hex[0]]<0 )
201             throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
202         len = DEC[hex[0]];
203         len = len << 4;
204         if( DEC[hex[1]]<0 )
205             throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
206         len += DEC[hex[1]];
207         len = len << 4;
208         if( DEC[hex[2]]<0 )
209             throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
210         len += DEC[hex[2]];
211         len = len << 4;
212         if( DEC[hex[3]]<0 )
213             throw new IllegalArgumentException JavaDoc(ERROR_BAD_CHARACTER_IN_HEX_STRING);
214         len += DEC[hex[3]];
215         return len;
216     }
217
218     /**
219      * [Private] Convert the specified value (0 .. 15) to the corresponding
220      * hexadecimal digit.
221      *
222      * @param value Value to be converted
223      */

224     private static char convertDigit(int value) {
225
226         value &= 0x0f;
227         if (value >= 10)
228             return ((char) (value - 10 + 'a'));
229         else
230             return ((char) (value + '0'));
231
232     }
233 }
234
Popular Tags