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