View Javadoc

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