Hex.java revision e6bf3e8dfa2804891a82075cb469b736321b4827
1package org.bouncycastle.util.encoders;
2
3import java.io.ByteArrayOutputStream;
4import java.io.IOException;
5import java.io.OutputStream;
6
7public class Hex
8{
9    private static final Encoder encoder = new HexEncoder();
10
11    /**
12     * encode the input data producing a Hex encoded byte array.
13     *
14     * @return a byte array containing the Hex encoded data.
15     */
16    public static byte[] encode(
17        byte[]    data)
18    {
19        return encode(data, 0, data.length);
20    }
21
22    /**
23     * encode the input data producing a Hex encoded byte array.
24     *
25     * @return a byte array containing the Hex encoded data.
26     */
27    public static byte[] encode(
28        byte[]    data,
29        int       off,
30        int       length)
31    {
32        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
33
34        try
35        {
36            encoder.encode(data, off, length, bOut);
37        }
38        catch (IOException e)
39        {
40            throw new RuntimeException("exception encoding Hex string: " + e);
41        }
42
43        return bOut.toByteArray();
44    }
45
46    /**
47     * Hex encode the byte data writing it to the given output stream.
48     *
49     * @return the number of bytes produced.
50     */
51    public static int encode(
52        byte[]         data,
53        OutputStream   out)
54        throws IOException
55    {
56        return encoder.encode(data, 0, data.length, out);
57    }
58
59    /**
60     * Hex encode the byte data writing it to the given output stream.
61     *
62     * @return the number of bytes produced.
63     */
64    public static int encode(
65        byte[]         data,
66        int            off,
67        int            length,
68        OutputStream   out)
69        throws IOException
70    {
71        return encoder.encode(data, off, length, out);
72    }
73
74    /**
75     * decode the Hex encoded input data. It is assumed the input data is valid.
76     *
77     * @return a byte array representing the decoded data.
78     */
79    public static byte[] decode(
80        byte[]    data)
81    {
82        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
83
84        try
85        {
86            encoder.decode(data, 0, data.length, bOut);
87        }
88        catch (IOException e)
89        {
90            throw new RuntimeException("exception decoding Hex string: " + e);
91        }
92
93        return bOut.toByteArray();
94    }
95
96    /**
97     * decode the Hex encoded String data - whitespace will be ignored.
98     *
99     * @return a byte array representing the decoded data.
100     */
101    public static byte[] decode(
102        String    data)
103    {
104        ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
105
106        try
107        {
108            encoder.decode(data, bOut);
109        }
110        catch (IOException e)
111        {
112            throw new RuntimeException("exception decoding Hex string: " + e);
113        }
114
115        return bOut.toByteArray();
116    }
117
118    /**
119     * decode the Hex encoded String data writing it to the given output stream,
120     * whitespace characters will be ignored.
121     *
122     * @return the number of bytes produced.
123     */
124    public static int decode(
125        String          data,
126        OutputStream    out)
127        throws IOException
128    {
129        return encoder.decode(data, out);
130    }
131}
132