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.FilterInputStream; | |
20 | import java.io.IOException; | |
21 | import java.io.InputStream; | |
22 | ||
23 | /** | |
24 | * An InputStream that does Base64 decoding on the data read through | |
25 | * it. | |
26 | */ | |
27 | public class Base64InputStream extends FilterInputStream { | |
28 | private final Base64.Coder coder; | |
29 | ||
30 | private static byte[] EMPTY = new byte[0]; | |
31 | ||
32 | private static final int BUFFER_SIZE = 2048; | |
33 | private boolean eof; | |
34 | private byte[] inputBuffer; | |
35 | private int outputStart; | |
36 | private int outputEnd; | |
37 | ||
38 | /** | |
39 | * An InputStream that performs Base64 decoding on the data read | |
40 | * from the wrapped stream. | |
41 | * | |
42 | * @param in the InputStream to read the source data from | |
43 | */ | |
44 | public Base64InputStream(InputStream in) { | |
45 | this(in, false); | |
46 | } | |
47 | ||
48 | /** | |
49 | * Performs Base64 encoding or decoding on the data read from the | |
50 | * wrapped InputStream. | |
51 | * | |
52 | * @param in the InputStream to read the source data from | |
53 | * @param flags bit flags for controlling the decoder; see the | |
54 | * constants in {@link Base64} | |
55 | * @param encode true to encode, false to decode | |
56 | * | |
57 | * @hide | |
58 | */ | |
59 | public Base64InputStream(InputStream in, boolean encode) { | |
60 | super(in); | |
61 | eof = false; | |
62 | inputBuffer = new byte[BUFFER_SIZE]; | |
63 | if (encode) { | |
64 | coder = new Base64.Encoder(Base64.NO_WRAP, null); | |
65 | } else { | |
66 | coder = new Base64.Decoder(Base64.NO_WRAP, null); | |
67 | } | |
68 | coder.output = new byte[coder.maxOutputSize(BUFFER_SIZE)]; | |
69 | outputStart = 0; | |
70 | outputEnd = 0; | |
71 | } | |
72 | ||
7194ac50 NR |
73 | @Override |
74 | public boolean markSupported() { | |
f28a134e NR |
75 | return false; |
76 | } | |
77 | ||
7194ac50 NR |
78 | @SuppressWarnings("sync-override") |
79 | @Override | |
80 | public void mark(int readlimit) { | |
f28a134e NR |
81 | throw new UnsupportedOperationException(); |
82 | } | |
83 | ||
7194ac50 NR |
84 | @SuppressWarnings("sync-override") |
85 | @Override | |
86 | public void reset() { | |
f28a134e NR |
87 | throw new UnsupportedOperationException(); |
88 | } | |
89 | ||
7194ac50 NR |
90 | @Override |
91 | public void close() throws IOException { | |
f28a134e NR |
92 | in.close(); |
93 | inputBuffer = null; | |
94 | } | |
95 | ||
7194ac50 NR |
96 | @Override |
97 | public int available() { | |
f28a134e NR |
98 | return outputEnd - outputStart; |
99 | } | |
100 | ||
7194ac50 NR |
101 | @Override |
102 | public long skip(long n) throws IOException { | |
f28a134e NR |
103 | if (outputStart >= outputEnd) { |
104 | refill(); | |
105 | } | |
106 | if (outputStart >= outputEnd) { | |
107 | return 0; | |
108 | } | |
109 | long bytes = Math.min(n, outputEnd-outputStart); | |
110 | outputStart += bytes; | |
111 | return bytes; | |
112 | } | |
113 | ||
7194ac50 NR |
114 | @Override |
115 | public int read() throws IOException { | |
f28a134e NR |
116 | if (outputStart >= outputEnd) { |
117 | refill(); | |
118 | } | |
119 | if (outputStart >= outputEnd) { | |
120 | return -1; | |
f28a134e | 121 | } |
7194ac50 NR |
122 | |
123 | return coder.output[outputStart++] & 0xff; | |
f28a134e NR |
124 | } |
125 | ||
7194ac50 NR |
126 | @Override |
127 | public int read(byte[] b, int off, int len) throws IOException { | |
f28a134e NR |
128 | if (outputStart >= outputEnd) { |
129 | refill(); | |
130 | } | |
131 | if (outputStart >= outputEnd) { | |
132 | return -1; | |
133 | } | |
134 | int bytes = Math.min(len, outputEnd-outputStart); | |
135 | System.arraycopy(coder.output, outputStart, b, off, bytes); | |
136 | outputStart += bytes; | |
137 | return bytes; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Read data from the input stream into inputBuffer, then | |
142 | * decode/encode it into the empty coder.output, and reset the | |
143 | * outputStart and outputEnd pointers. | |
144 | */ | |
145 | private void refill() throws IOException { | |
146 | if (eof) return; | |
147 | int bytesRead = in.read(inputBuffer); | |
148 | boolean success; | |
149 | if (bytesRead == -1) { | |
150 | eof = true; | |
151 | success = coder.process(EMPTY, 0, 0, true); | |
152 | } else { | |
153 | success = coder.process(inputBuffer, 0, bytesRead, false); | |
154 | } | |
155 | if (!success) { | |
156 | throw new IOException("bad base-64"); | |
157 | } | |
158 | outputEnd = coder.op; | |
159 | outputStart = 0; | |
160 | } | |
161 | } |