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