change b64 implem step1, fix test
[nikiroo-utils.git] / src / be / nikiroo / utils / streams / Base64InputStream.java
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
73 public boolean markSupported() {
74 return false;
75 }
76
77 public void mark(int readlimit) {
78 throw new UnsupportedOperationException();
79 }
80
81 public void reset() {
82 throw new UnsupportedOperationException();
83 }
84
85 public void close() throws IOException {
86 in.close();
87 inputBuffer = null;
88 }
89
90 public int available() {
91 return outputEnd - outputStart;
92 }
93
94 public long skip(long n) throws IOException {
95 if (outputStart >= outputEnd) {
96 refill();
97 }
98 if (outputStart >= outputEnd) {
99 return 0;
100 }
101 long bytes = Math.min(n, outputEnd-outputStart);
102 outputStart += bytes;
103 return bytes;
104 }
105
106 public int read() throws IOException {
107 if (outputStart >= outputEnd) {
108 refill();
109 }
110 if (outputStart >= outputEnd) {
111 return -1;
112 } else {
113 return coder.output[outputStart++] & 0xff;
114 }
115 }
116
117 public int read(byte[] b, int off, int len) throws IOException {
118 if (outputStart >= outputEnd) {
119 refill();
120 }
121 if (outputStart >= outputEnd) {
122 return -1;
123 }
124 int bytes = Math.min(len, outputEnd-outputStart);
125 System.arraycopy(coder.output, outputStart, b, off, bytes);
126 outputStart += bytes;
127 return bytes;
128 }
129
130 /**
131 * Read data from the input stream into inputBuffer, then
132 * decode/encode it into the empty coder.output, and reset the
133 * outputStart and outputEnd pointers.
134 */
135 private void refill() throws IOException {
136 if (eof) return;
137 int bytesRead = in.read(inputBuffer);
138 boolean success;
139 if (bytesRead == -1) {
140 eof = true;
141 success = coder.process(EMPTY, 0, 0, true);
142 } else {
143 success = coder.process(inputBuffer, 0, bytesRead, false);
144 }
145 if (!success) {
146 throw new IOException("bad base-64");
147 }
148 outputEnd = coder.op;
149 outputStart = 0;
150 }
151 }