KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > encoders > Hex


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 package org.apache.geronimo.util.encoders;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 public class Hex
25 {
26     private static final Encoder encoder = new HexEncoder();
27
28     /**
29      * encode the input data producing a Hex encoded byte array.
30      *
31      * @return a byte array containing the Hex encoded data.
32      */

33     public static byte[] encode(
34         byte[] data)
35     {
36         return encode(data, 0, data.length);
37     }
38
39     /**
40      * encode the input data producing a Hex encoded byte array.
41      *
42      * @return a byte array containing the Hex encoded data.
43      */

44     public static byte[] encode(
45         byte[] data,
46         int off,
47         int length)
48     {
49         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
50
51         try
52         {
53             encoder.encode(data, off, length, bOut);
54         }
55         catch (IOException JavaDoc e)
56         {
57             throw new RuntimeException JavaDoc("exception encoding Hex string: " + e);
58         }
59
60         return bOut.toByteArray();
61     }
62
63     /**
64      * Hex encode the byte data writing it to the given output stream.
65      *
66      * @return the number of bytes produced.
67      */

68     public static int encode(
69         byte[] data,
70         OutputStream JavaDoc out)
71         throws IOException JavaDoc
72     {
73         return encoder.encode(data, 0, data.length, out);
74     }
75
76     /**
77      * Hex encode the byte data writing it to the given output stream.
78      *
79      * @return the number of bytes produced.
80      */

81     public static int encode(
82         byte[] data,
83         int off,
84         int length,
85         OutputStream JavaDoc out)
86         throws IOException JavaDoc
87     {
88         return encoder.encode(data, 0, data.length, out);
89     }
90
91     /**
92      * decode the Hex encoded input data. It is assumed the input data is valid.
93      *
94      * @return a byte array representing the decoded data.
95      */

96     public static byte[] decode(
97         byte[] data)
98     {
99         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
100
101         try
102         {
103             encoder.decode(data, 0, data.length, bOut);
104         }
105         catch (IOException JavaDoc e)
106         {
107             throw new RuntimeException JavaDoc("exception decoding Hex string: " + e);
108         }
109
110         return bOut.toByteArray();
111     }
112
113     /**
114      * decode the Hex encoded String data - whitespace will be ignored.
115      *
116      * @return a byte array representing the decoded data.
117      */

118     public static byte[] decode(
119         String JavaDoc data)
120     {
121         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
122
123         try
124         {
125             encoder.decode(data, bOut);
126         }
127         catch (IOException JavaDoc e)
128         {
129             throw new RuntimeException JavaDoc("exception decoding Hex string: " + e);
130         }
131
132         return bOut.toByteArray();
133     }
134
135     /**
136      * decode the Hex encoded String data writing it to the given output stream,
137      * whitespace characters will be ignored.
138      *
139      * @return the number of bytes produced.
140      */

141     public static int decode(
142         String JavaDoc data,
143         OutputStream JavaDoc out)
144         throws IOException JavaDoc
145     {
146         return encoder.decode(data, out);
147     }
148 }
149
Popular Tags