KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > util > HexBinary


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

17 package org.apache.ws.jaxme.util;
18
19 /** <p>Utility class for xs:hexbinary.</p>
20  */

21 public class HexBinary {
22     /** Creates a clone of the given byte array.
23      */

24     public static byte[] getClone(byte[] pHexBinary) {
25         byte[] result = new byte[pHexBinary.length];
26         System.arraycopy(pHexBinary, 0, result, 0, pHexBinary.length);
27         return result;
28     }
29
30     /** Converts the string <code>pValue</code> into an
31      * array of hex bytes.
32      */

33     public static byte[] decode(String JavaDoc pValue) {
34         if ((pValue.length() % 2) != 0) {
35             throw new IllegalArgumentException JavaDoc("A HexBinary string must have even length.");
36         }
37         byte[] result = new byte[pValue.length() / 2];
38         int j = 0;
39         for (int i = 0; i < pValue.length(); ) {
40             byte b;
41             char c = pValue.charAt(i++);
42             char d = pValue.charAt(i++);
43             if (c >= '0' && c <= '9') {
44                 b = (byte) ((c - '0') << 4);
45             } else if (c >= 'A' && c <= 'F') {
46                 b = (byte) ((c - 'A' + 10) << 4);
47             } else if (c >= 'a' && c <= 'f') {
48                 b = (byte) ((c - 'a' + 10) << 4);
49             } else {
50                 throw new IllegalArgumentException JavaDoc("Invalid hex digit: " + c);
51             }
52             if (d >= '0' && d <= '9') {
53                 b += (byte) (d - '0');
54             } else if (d >= 'A' && d <= 'F') {
55                 b += (byte) (d - 'A' + 10);
56             } else if (d >= 'a' && d <= 'f') {
57                 b += (byte) (d - 'a' + 10);
58             } else {
59                 throw new IllegalArgumentException JavaDoc("Invalid hex digit: " + d);
60             }
61             result[j++] = b;
62         }
63         return result;
64     }
65
66     /** Converts the byte array <code>pHexBinary</code> into
67      * a string.
68      */

69     public static String JavaDoc encode(byte[] pHexBinary) {
70         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
71         for (int i = 0; i < pHexBinary.length; i++) {
72             byte b = pHexBinary[i];
73             byte c = (byte) ((b & 0xf0) >> 4);
74             if (c <= 9) {
75                 result.append((char) ('0' + c));
76             } else {
77                 result.append((char) ('A' + c - 10));
78             }
79             c = (byte) (b & 0x0f);
80             if (c <= 9) {
81                 result.append((char) ('0' + c));
82             } else {
83                 result.append((char) ('A' + c - 10));
84             }
85         }
86         return result.toString();
87     }
88 }
89
Popular Tags