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