1 /*
2 * Copyright (c) 1997, 2013, 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
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 };
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;
|
1 /*
2 * Copyright (c) 1997, 2017, 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
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 @Override
74 public int read() throws IOException {
75 if (index >= bufsize) {
76 decode(); // Fills up buffer
77 if (bufsize == 0) // buffer is empty
78 return -1;
79 index = 0; // reset index into buffer
80 }
81 return buffer[index++] & 0xff; // Zero off the MSB
82 }
83
84 /**
85 * Reads up to <code>len</code> decoded bytes of data from this input stream
86 * into an array of bytes. This method blocks until some input is
87 * available.
88 * <p>
89 *
90 * @param buf the buffer into which the data is read.
91 * @param off the start offset of the data.
92 * @param len the maximum number of bytes read.
93 * @return the total number of bytes read into the buffer, or
94 * <code>-1</code> if there is no more data because the end of
95 * the stream has been reached.
96 * @exception IOException if an I/O error occurs.
97 */
98 @Override
99 public int read(byte[] buf, int off, int len) throws IOException {
100 int i, c;
101 for (i = 0; i < len; i++) {
102 if ((c = read()) == -1) {
103 if (i == 0) // At end of stream, so we should
104 i = -1; // return -1 , NOT 0.
105 break;
106 }
107 buf[off+i] = (byte)c;
108 }
109
110 return i;
111 }
112
113 /**
114 * Tests if this input stream supports marks. Currently this class
115 * does not support marks
116 */
117 @Override
118 public boolean markSupported() {
119 return false; // Maybe later ..
120 }
121
122 /**
123 * Returns the number of bytes that can be read from this input
124 * stream without blocking. However, this figure is only
125 * a close approximation in case the original encoded stream
126 * contains embedded CRLFs; since the CRLFs are discarded, not decoded
127 */
128 @Override
129 public int available() throws IOException {
130 // This is only an estimate, since in.available()
131 // might include CRLFs too ..
132 return ((in.available() * 3)/4 + (bufsize-index));
133 }
134
135 /**
136 * This character array provides the character to value map
137 * based on RFC1521.
138 */
139 private final static char pem_array[] = {
140 'A','B','C','D','E','F','G','H', // 0
141 'I','J','K','L','M','N','O','P', // 1
142 'Q','R','S','T','U','V','W','X', // 2
143 'Y','Z','a','b','c','d','e','f', // 3
144 'g','h','i','j','k','l','m','n', // 4
145 'o','p','q','r','s','t','u','v', // 5
146 'w','x','y','z','0','1','2','3', // 6
147 '4','5','6','7','8','9','+','/' // 7
148 };
186 if (decode_buffer[2] == '=') // End of this BASE64 encoding
187 return;
188 a = b;
189 b = pem_convert_array[decode_buffer[2] & 0xff];
190 // The second decoded byte
191 buffer[bufsize++] = (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf));
192
193 if (decode_buffer[3] == '=') // End of this BASE64 encoding
194 return;
195 a = b;
196 b = pem_convert_array[decode_buffer[3] & 0xff];
197 // The third decoded byte
198 buffer[bufsize++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f));
199 }
200
201 /**
202 * Base64 decode a byte array. No line breaks are allowed.
203 * This method is suitable for short strings, such as those
204 * in the IMAP AUTHENTICATE protocol, but not to decode the
205 * entire content of a MIME part.
206 *
207 * @param inbuf byte array to decode
208 *
209 * @return decoded byte array
210 *
211 * NOTE: inbuf may only contain valid base64 characters.
212 * Whitespace is not ignored.
213 */
214 public static byte[] decode(byte[] inbuf) {
215 int size = (inbuf.length / 4) * 3;
216 if (size == 0)
217 return inbuf;
218
219 if (inbuf[inbuf.length - 1] == '=') {
220 size--;
221 if (inbuf[inbuf.length - 2] == '=')
222 size--;
223 }
224 byte[] outbuf = new byte[size];
225
226 int inpos = 0, outpos = 0;
227 size = inbuf.length;
228 while (size > 0) {
229 byte a, b;
|