1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.util.encoders;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStream;
24
25 public class Hex
26 {
27 private static final Encoder encoder = new HexEncoder();
28
29 /**
30 * encode the input data producing a Hex encoded byte array.
31 *
32 * @return a byte array containing the Hex encoded data.
33 */
34 public static byte[] encode(
35 byte[] data)
36 {
37 return encode(data, 0, data.length);
38 }
39
40 /**
41 * encode the input data producing a Hex encoded byte array.
42 *
43 * @return a byte array containing the Hex encoded data.
44 */
45 public static byte[] encode(
46 byte[] data,
47 int off,
48 int length)
49 {
50 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
51
52 try
53 {
54 encoder.encode(data, off, length, bOut);
55 }
56 catch (IOException e)
57 {
58 throw new RuntimeException("exception encoding Hex string: " + e);
59 }
60
61 return bOut.toByteArray();
62 }
63
64 /**
65 * Hex encode the byte data writing it to the given output stream.
66 *
67 * @return the number of bytes produced.
68 */
69 public static int encode(
70 byte[] data,
71 OutputStream out)
72 throws IOException
73 {
74 return encoder.encode(data, 0, data.length, out);
75 }
76
77 /**
78 * Hex encode the byte data writing it to the given output stream.
79 *
80 * @return the number of bytes produced.
81 */
82 public static int encode(
83 byte[] data,
84 int off,
85 int length,
86 OutputStream out)
87 throws IOException
88 {
89 return encoder.encode(data, 0, data.length, out);
90 }
91
92 /**
93 * decode the Hex encoded input data. It is assumed the input data is valid.
94 *
95 * @return a byte array representing the decoded data.
96 */
97 public static byte[] decode(
98 byte[] data)
99 {
100 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
101
102 try
103 {
104 encoder.decode(data, 0, data.length, bOut);
105 }
106 catch (IOException e)
107 {
108 throw new RuntimeException("exception decoding Hex string: " + e);
109 }
110
111 return bOut.toByteArray();
112 }
113
114 /**
115 * decode the Hex encoded String data - whitespace will be ignored.
116 *
117 * @return a byte array representing the decoded data.
118 */
119 public static byte[] decode(
120 String data)
121 {
122 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
123
124 try
125 {
126 encoder.decode(data, bOut);
127 }
128 catch (IOException e)
129 {
130 throw new RuntimeException("exception decoding Hex string: " + e);
131 }
132
133 return bOut.toByteArray();
134 }
135
136 /**
137 * decode the Hex encoded String data writing it to the given output stream,
138 * whitespace characters will be ignored.
139 *
140 * @return the number of bytes produced.
141 */
142 public static int decode(
143 String data,
144 OutputStream out)
145 throws IOException
146 {
147 return encoder.decode(data, out);
148 }
149 }