Commit | Line | Data |
---|---|---|
f28a134e NR |
1 | /* |
2 | * Copyright (C) 2010 The Android Open Source Project | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package be.nikiroo.utils.streams; | |
18 | ||
19 | import java.io.FilterOutputStream; | |
20 | import java.io.IOException; | |
21 | import java.io.OutputStream; | |
22 | ||
23 | /** | |
24 | * An OutputStream that does Base64 encoding on the data written to | |
25 | * it, writing the resulting data to another OutputStream. | |
26 | */ | |
27 | public class Base64OutputStream extends FilterOutputStream { | |
28 | private final Base64.Coder coder; | |
29 | private final int flags; | |
30 | ||
31 | private byte[] buffer = null; | |
32 | private int bpos = 0; | |
33 | ||
34 | private static byte[] EMPTY = new byte[0]; | |
35 | ||
36 | /** | |
37 | * Performs Base64 encoding on the data written to the stream, | |
38 | * writing the encoded data to another OutputStream. | |
39 | * | |
40 | * @param out the OutputStream to write the encoded data to | |
41 | */ | |
42 | public Base64OutputStream(OutputStream out) { | |
43 | this(out, true); | |
44 | } | |
45 | ||
46 | /** | |
47 | * Performs Base64 encoding or decoding on the data written to the | |
48 | * stream, writing the encoded/decoded data to another | |
49 | * OutputStream. | |
50 | * | |
51 | * @param out the OutputStream to write the encoded data to | |
52 | * @param encode true to encode, false to decode | |
53 | * | |
54 | * @hide | |
55 | */ | |
56 | public Base64OutputStream(OutputStream out, boolean encode) { | |
57 | super(out); | |
58 | this.flags = Base64.NO_WRAP; | |
59 | if (encode) { | |
60 | coder = new Base64.Encoder(flags, null); | |
61 | } else { | |
62 | coder = new Base64.Decoder(flags, null); | |
63 | } | |
64 | } | |
65 | ||
66 | public void write(int b) throws IOException { | |
67 | // To avoid invoking the encoder/decoder routines for single | |
68 | // bytes, we buffer up calls to write(int) in an internal | |
69 | // byte array to transform them into writes of decently-sized | |
70 | // arrays. | |
71 | ||
72 | if (buffer == null) { | |
73 | buffer = new byte[1024]; | |
74 | } | |
75 | if (bpos >= buffer.length) { | |
76 | // internal buffer full; write it out. | |
77 | internalWrite(buffer, 0, bpos, false); | |
78 | bpos = 0; | |
79 | } | |
80 | buffer[bpos++] = (byte) b; | |
81 | } | |
82 | ||
83 | /** | |
84 | * Flush any buffered data from calls to write(int). Needed | |
85 | * before doing a write(byte[], int, int) or a close(). | |
86 | */ | |
87 | private void flushBuffer() throws IOException { | |
88 | if (bpos > 0) { | |
89 | internalWrite(buffer, 0, bpos, false); | |
90 | bpos = 0; | |
91 | } | |
92 | } | |
93 | ||
94 | public void write(byte[] b, int off, int len) throws IOException { | |
95 | if (len <= 0) return; | |
96 | flushBuffer(); | |
97 | internalWrite(b, off, len, false); | |
98 | } | |
99 | ||
100 | public void close() throws IOException { | |
101 | IOException thrown = null; | |
102 | try { | |
103 | flushBuffer(); | |
104 | internalWrite(EMPTY, 0, 0, true); | |
105 | } catch (IOException e) { | |
106 | thrown = e; | |
107 | } | |
108 | ||
109 | try { | |
110 | if ((flags & Base64.NO_CLOSE) == 0) { | |
111 | out.close(); | |
112 | } else { | |
113 | out.flush(); | |
114 | } | |
115 | } catch (IOException e) { | |
116 | if (thrown != null) { | |
117 | thrown = e; | |
118 | } | |
119 | } | |
120 | ||
121 | if (thrown != null) { | |
122 | throw thrown; | |
123 | } | |
124 | } | |
125 | ||
126 | /** | |
127 | * Write the given bytes to the encoder/decoder. | |
128 | * | |
129 | * @param finish true if this is the last batch of input, to cause | |
130 | * encoder/decoder state to be finalized. | |
131 | */ | |
132 | private void internalWrite(byte[] b, int off, int len, boolean finish) throws IOException { | |
133 | coder.output = embiggen(coder.output, coder.maxOutputSize(len)); | |
134 | if (!coder.process(b, off, len, finish)) { | |
135 | throw new IOException("bad base-64"); | |
136 | } | |
137 | out.write(coder.output, 0, coder.op); | |
138 | } | |
139 | ||
140 | /** | |
141 | * If b.length is at least len, return b. Otherwise return a new | |
142 | * byte array of length len. | |
143 | */ | |
144 | private byte[] embiggen(byte[] b, int len) { | |
145 | if (b == null || b.length < len) { | |
146 | return new byte[len]; | |
147 | } else { | |
148 | return b; | |
149 | } | |
150 | } | |
151 | } |