|
1 /* |
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 /* |
|
27 * @(#)BASE64DecoderStream.java 1.8 02/03/27 |
|
28 */ |
|
29 |
|
30 |
|
31 |
|
32 package com.sun.xml.internal.messaging.saaj.packaging.mime.util; |
|
33 |
|
34 import java.io.*; |
|
35 |
|
36 /** |
|
37 * This class implements a BASE64 Decoder. It is implemented as |
|
38 * a FilterInputStream, so one can just wrap this class around |
|
39 * any input stream and read bytes from this filter. The decoding |
|
40 * is done as the bytes are read out. |
|
41 * |
|
42 * @author John Mani |
|
43 * @author Bill Shannon |
|
44 */ |
|
45 |
|
46 public class BASE64DecoderStream extends FilterInputStream { |
|
47 private byte[] buffer; // cache of decoded bytes |
|
48 private int bufsize = 0; // size of the cache |
|
49 private int index = 0; // index into the cache |
|
50 |
|
51 /** |
|
52 * Create a BASE64 decoder that decodes the specified input stream |
|
53 * @param in the input stream |
|
54 */ |
|
55 public BASE64DecoderStream(InputStream in) { |
|
56 super(in); |
|
57 buffer = new byte[3]; |
|
58 } |
|
59 |
|
60 /** |
|
61 * Read the next decoded byte from this input stream. The byte |
|
62 * is returned as an <code>int</code> in the range <code>0</code> |
|
63 * to <code>255</code>. If no byte is available because the end of |
|
64 * the stream has been reached, the value <code>-1</code> is returned. |
|
65 * This method blocks until input data is available, the end of the |
|
66 * stream is detected, or an exception is thrown. |
|
67 * |
|
68 * @return next byte of data, or <code>-1</code> if the end of the |
|
69 * stream is reached. |
|
70 * @exception IOException if an I/O error occurs. |
|
71 * @see java.io.FilterInputStream#in |
|
72 */ |
|
73 public int read() throws IOException { |
|
74 if (index >= bufsize) { |
|
75 decode(); // Fills up buffer |
|
76 if (bufsize == 0) // buffer is empty |
|
77 return -1; |
|
78 index = 0; // reset index into buffer |
|
79 } |
|
80 return buffer[index++] & 0xff; // Zero off the MSB |
|
81 } |
|
82 |
|
83 /** |
|
84 * Reads up to <code>len</code> decoded bytes of data from this input stream |
|
85 * into an array of bytes. This method blocks until some input is |
|
86 * available. |
|
87 * <p> |
|
88 * |
|
89 * @param buf the buffer into which the data is read. |
|
90 * @param off the start offset of the data. |
|
91 * @param len the maximum number of bytes read. |
|
92 * @return the total number of bytes read into the buffer, or |
|
93 * <code>-1</code> if there is no more data because the end of |
|
94 * the stream has been reached. |
|
95 * @exception IOException if an I/O error occurs. |
|
96 */ |
|
97 public int read(byte[] buf, int off, int len) throws IOException { |
|
98 int i, c; |
|
99 for (i = 0; i < len; i++) { |
|
100 if ((c = read()) == -1) { |
|
101 if (i == 0) // At end of stream, so we should |
|
102 i = -1; // return -1 , NOT 0. |
|
103 break; |
|
104 } |
|
105 buf[off+i] = (byte)c; |
|
106 } |
|
107 |
|
108 return i; |
|
109 } |
|
110 |
|
111 /** |
|
112 * Tests if this input stream supports marks. Currently this class |
|
113 * does not support marks |
|
114 */ |
|
115 public boolean markSupported() { |
|
116 return false; // Maybe later .. |
|
117 } |
|
118 |
|
119 /** |
|
120 * Returns the number of bytes that can be read from this input |
|
121 * stream without blocking. However, this figure is only |
|
122 * a close approximation in case the original encoded stream |
|
123 * contains embedded CRLFs; since the CRLFs are discarded, not decoded |
|
124 */ |
|
125 public int available() throws IOException { |
|
126 // This is only an estimate, since in.available() |
|
127 // might include CRLFs too .. |
|
128 return ((in.available() * 3)/4 + (bufsize-index)); |
|
129 } |
|
130 |
|
131 /** |
|
132 * This character array provides the character to value map |
|
133 * based on RFC1521. |
|
134 */ |
|
135 private final static char pem_array[] = { |
|
136 'A','B','C','D','E','F','G','H', // 0 |
|
137 'I','J','K','L','M','N','O','P', // 1 |
|
138 'Q','R','S','T','U','V','W','X', // 2 |
|
139 'Y','Z','a','b','c','d','e','f', // 3 |
|
140 'g','h','i','j','k','l','m','n', // 4 |
|
141 'o','p','q','r','s','t','u','v', // 5 |
|
142 'w','x','y','z','0','1','2','3', // 6 |
|
143 '4','5','6','7','8','9','+','/' // 7 |
|
144 }; |
|
145 |
|
146 private final static byte pem_convert_array[] = new byte[256]; |
|
147 |
|
148 static { |
|
149 for (int i = 0; i < 255; i++) |
|
150 pem_convert_array[i] = -1; |
|
151 for(int i = 0; i < pem_array.length; i++) |
|
152 pem_convert_array[pem_array[i]] = (byte) i; |
|
153 } |
|
154 |
|
155 /* The decoder algorithm */ |
|
156 private byte[] decode_buffer = new byte[4]; |
|
157 private void decode() throws IOException { |
|
158 bufsize = 0; |
|
159 /* |
|
160 * We need 4 valid base64 characters before we start decoding. |
|
161 * We skip anything that's not a valid base64 character (usually |
|
162 * just CRLF). |
|
163 */ |
|
164 int got = 0; |
|
165 while (got < 4) { |
|
166 int i = in.read(); |
|
167 if (i == -1) { |
|
168 if (got == 0) |
|
169 return; // EOF before any data is ok |
|
170 throw new IOException("Error in encoded stream, got " + got); |
|
171 } |
|
172 if (i >= 0 && i < 256 && i == '=' || pem_convert_array[i] != -1) |
|
173 decode_buffer[got++] = (byte)i; |
|
174 } |
|
175 |
|
176 byte a, b; |
|
177 a = pem_convert_array[decode_buffer[0] & 0xff]; |
|
178 b = pem_convert_array[decode_buffer[1] & 0xff]; |
|
179 // The first decoded byte |
|
180 buffer[bufsize++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)); |
|
181 |
|
182 if (decode_buffer[2] == '=') // End of this BASE64 encoding |
|
183 return; |
|
184 a = b; |
|
185 b = pem_convert_array[decode_buffer[2] & 0xff]; |
|
186 // The second decoded byte |
|
187 buffer[bufsize++] = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf)); |
|
188 |
|
189 if (decode_buffer[3] == '=') // End of this BASE64 encoding |
|
190 return; |
|
191 a = b; |
|
192 b = pem_convert_array[decode_buffer[3] & 0xff]; |
|
193 // The third decoded byte |
|
194 buffer[bufsize++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f)); |
|
195 } |
|
196 |
|
197 /** |
|
198 * Base64 decode a byte array. No line breaks are allowed. |
|
199 * This method is suitable for short strings, such as those |
|
200 * in the IMAP AUTHENTICATE protocol, but not to decode the |
|
201 * entire content of a MIME part. |
|
202 * |
|
203 * NOTE: inbuf may only contain valid base64 characters. |
|
204 * Whitespace is not ignored. |
|
205 */ |
|
206 public static byte[] decode(byte[] inbuf) { |
|
207 int size = (inbuf.length / 4) * 3; |
|
208 if (size == 0) |
|
209 return inbuf; |
|
210 |
|
211 if (inbuf[inbuf.length - 1] == '=') { |
|
212 size--; |
|
213 if (inbuf[inbuf.length - 2] == '=') |
|
214 size--; |
|
215 } |
|
216 byte[] outbuf = new byte[size]; |
|
217 |
|
218 int inpos = 0, outpos = 0; |
|
219 size = inbuf.length; |
|
220 while (size > 0) { |
|
221 byte a, b; |
|
222 a = pem_convert_array[inbuf[inpos++] & 0xff]; |
|
223 b = pem_convert_array[inbuf[inpos++] & 0xff]; |
|
224 // The first decoded byte |
|
225 outbuf[outpos++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)); |
|
226 |
|
227 if (inbuf[inpos] == '=') // End of this BASE64 encoding |
|
228 return outbuf; |
|
229 a = b; |
|
230 b = pem_convert_array[inbuf[inpos++] & 0xff]; |
|
231 // The second decoded byte |
|
232 outbuf[outpos++] = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf)); |
|
233 |
|
234 if (inbuf[inpos] == '=') // End of this BASE64 encoding |
|
235 return outbuf; |
|
236 a = b; |
|
237 b = pem_convert_array[inbuf[inpos++] & 0xff]; |
|
238 // The third decoded byte |
|
239 outbuf[outpos++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f)); |
|
240 size -= 4; |
|
241 } |
|
242 return outbuf; |
|
243 } |
|
244 |
|
245 /*** begin TEST program *** |
|
246 public static void main(String argv[]) throws Exception { |
|
247 FileInputStream infile = new FileInputStream(argv[0]); |
|
248 BASE64DecoderStream decoder = new BASE64DecoderStream(infile); |
|
249 int c; |
|
250 |
|
251 while ((c = decoder.read()) != -1) |
|
252 System.out.print((char)c); |
|
253 System.out.flush(); |
|
254 } |
|
255 *** end TEST program ***/ |
|
256 } |