1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/BASE64EncoderStream.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,244 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +/* 1.30 + * @(#)BASE64EncoderStream.java 1.6 02/03/27 1.31 + */ 1.32 + 1.33 + 1.34 + 1.35 +package com.sun.xml.internal.messaging.saaj.packaging.mime.util; 1.36 + 1.37 +import java.io.*; 1.38 + 1.39 +/** 1.40 + * This class implements a BASE64 Encoder. It is implemented as 1.41 + * a FilterOutputStream, so one can just wrap this class around 1.42 + * any output stream and write bytes into this filter. The Encoding 1.43 + * is done as the bytes are written out. 1.44 + * 1.45 + * @author John Mani 1.46 + * @author Bill Shannon 1.47 + */ 1.48 + 1.49 +public class BASE64EncoderStream extends FilterOutputStream { 1.50 + private byte[] buffer; // cache of bytes that are yet to be encoded 1.51 + private int bufsize = 0; // size of the cache 1.52 + private int count = 0; // number of bytes that have been output 1.53 + private int bytesPerLine; // number of bytes per line 1.54 + 1.55 + /** 1.56 + * Create a BASE64 encoder that encodes the specified input stream 1.57 + * @param out the output stream 1.58 + * @param bytesPerLine number of bytes per line. The encoder inserts 1.59 + * a CRLF sequence after the specified number of bytes 1.60 + */ 1.61 + public BASE64EncoderStream(OutputStream out, int bytesPerLine) { 1.62 + super(out); 1.63 + buffer = new byte[3]; 1.64 + this.bytesPerLine = bytesPerLine; 1.65 + } 1.66 + 1.67 + /** 1.68 + * Create a BASE64 encoder that encodes the specified input stream. 1.69 + * Inserts the CRLF sequence after outputting 76 bytes. 1.70 + * @param out the output stream 1.71 + */ 1.72 + public BASE64EncoderStream(OutputStream out) { 1.73 + this(out, 76); 1.74 + } 1.75 + 1.76 + /** 1.77 + * Encodes <code>len</code> bytes from the specified 1.78 + * <code>byte</code> array starting at offset <code>off</code> to 1.79 + * this output stream. 1.80 + * 1.81 + * @param b the data. 1.82 + * @param off the start offset in the data. 1.83 + * @param len the number of bytes to write. 1.84 + * @exception IOException if an I/O error occurs. 1.85 + */ 1.86 + public void write(byte[] b, int off, int len) throws IOException { 1.87 + for (int i = 0; i < len; i++) 1.88 + write(b[off + i]); 1.89 + } 1.90 + 1.91 + /** 1.92 + * Encodes <code>b.length</code> bytes to this output stream. 1.93 + * @param b the data to be written. 1.94 + * @exception IOException if an I/O error occurs. 1.95 + */ 1.96 + public void write(byte[] b) throws IOException { 1.97 + write(b, 0, b.length); 1.98 + } 1.99 + 1.100 + /** 1.101 + * Encodes the specified <code>byte</code> to this output stream. 1.102 + * @param c the <code>byte</code>. 1.103 + * @exception IOException if an I/O error occurs. 1.104 + */ 1.105 + public void write(int c) throws IOException { 1.106 + buffer[bufsize++] = (byte)c; 1.107 + if (bufsize == 3) { // Encoding unit = 3 bytes 1.108 + encode(); 1.109 + bufsize = 0; 1.110 + } 1.111 + } 1.112 + 1.113 + /** 1.114 + * Flushes this output stream and forces any buffered output bytes 1.115 + * to be encoded out to the stream. 1.116 + * @exception IOException if an I/O error occurs. 1.117 + */ 1.118 + public void flush() throws IOException { 1.119 + if (bufsize > 0) { // If there's unencoded characters in the buffer .. 1.120 + encode(); // .. encode them 1.121 + bufsize = 0; 1.122 + } 1.123 + out.flush(); 1.124 + } 1.125 + 1.126 + /** 1.127 + * Forces any buffered output bytes to be encoded out to the stream 1.128 + * and closes this output stream 1.129 + */ 1.130 + public void close() throws IOException { 1.131 + flush(); 1.132 + out.close(); 1.133 + } 1.134 + 1.135 + /** This array maps the characters to their 6 bit values */ 1.136 + private final static char pem_array[] = { 1.137 + 'A','B','C','D','E','F','G','H', // 0 1.138 + 'I','J','K','L','M','N','O','P', // 1 1.139 + 'Q','R','S','T','U','V','W','X', // 2 1.140 + 'Y','Z','a','b','c','d','e','f', // 3 1.141 + 'g','h','i','j','k','l','m','n', // 4 1.142 + 'o','p','q','r','s','t','u','v', // 5 1.143 + 'w','x','y','z','0','1','2','3', // 6 1.144 + '4','5','6','7','8','9','+','/' // 7 1.145 + }; 1.146 + 1.147 + private void encode() throws IOException { 1.148 + // If writing out this encoded unit will cause overflow, 1.149 + // start a new line. 1.150 + if (count + 4 > bytesPerLine) { 1.151 + out.write('\r'); 1.152 + out.write('\n'); 1.153 + count = 0; 1.154 + } 1.155 + 1.156 + byte a, b, c; 1.157 + if (bufsize == 1) { 1.158 + a = buffer[0]; 1.159 + b = 0; 1.160 + c = 0; 1.161 + out.write(pem_array[(a >>> 2) & 0x3F]); 1.162 + out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]); 1.163 + out.write('='); // pad character 1.164 + out.write('='); // pad character 1.165 + } else if (bufsize == 2) { 1.166 + a = buffer[0]; 1.167 + b = buffer[1]; 1.168 + c = 0; 1.169 + out.write(pem_array[(a >>> 2) & 0x3F]); 1.170 + out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]); 1.171 + out.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]); 1.172 + out.write('='); // pad character 1.173 + } else { 1.174 + a = buffer[0]; 1.175 + b = buffer[1]; 1.176 + c = buffer[2]; 1.177 + out.write(pem_array[(a >>> 2) & 0x3F]); 1.178 + out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]); 1.179 + out.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]); 1.180 + out.write(pem_array[c & 0x3F]); 1.181 + } 1.182 + 1.183 + // increment count 1.184 + count += 4; 1.185 + } 1.186 + 1.187 + /** 1.188 + * Base64 encode a byte array. No line breaks are inserted. 1.189 + * This method is suitable for short strings, such as those 1.190 + * in the IMAP AUTHENTICATE protocol, but not to encode the 1.191 + * entire content of a MIME part. 1.192 + */ 1.193 + public static byte[] encode(byte[] inbuf) { 1.194 + if (inbuf.length == 0) 1.195 + return inbuf; 1.196 + byte[] outbuf = new byte[((inbuf.length + 2) / 3) * 4]; 1.197 + int inpos = 0, outpos = 0; 1.198 + int size = inbuf.length; 1.199 + while (size > 0) { 1.200 + byte a, b, c; 1.201 + if (size == 1) { 1.202 + a = inbuf[inpos++]; 1.203 + b = 0; 1.204 + c = 0; 1.205 + outbuf[outpos++] = (byte)pem_array[(a >>> 2) & 0x3F]; 1.206 + outbuf[outpos++] = 1.207 + (byte)pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]; 1.208 + outbuf[outpos++] = (byte)'='; // pad character 1.209 + outbuf[outpos++] = (byte)'='; // pad character 1.210 + } else if (size == 2) { 1.211 + a = inbuf[inpos++]; 1.212 + b = inbuf[inpos++]; 1.213 + c = 0; 1.214 + outbuf[outpos++] = (byte)pem_array[(a >>> 2) & 0x3F]; 1.215 + outbuf[outpos++] = 1.216 + (byte)pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]; 1.217 + outbuf[outpos++] = 1.218 + (byte)pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]; 1.219 + outbuf[outpos++] = (byte)'='; // pad character 1.220 + } else { 1.221 + a = inbuf[inpos++]; 1.222 + b = inbuf[inpos++]; 1.223 + c = inbuf[inpos++]; 1.224 + outbuf[outpos++] = (byte)pem_array[(a >>> 2) & 0x3F]; 1.225 + outbuf[outpos++] = 1.226 + (byte)pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]; 1.227 + outbuf[outpos++] = 1.228 + (byte)pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]; 1.229 + outbuf[outpos++] = (byte)pem_array[c & 0x3F]; 1.230 + } 1.231 + size -= 3; 1.232 + } 1.233 + return outbuf; 1.234 + } 1.235 + 1.236 + /*** begin TEST program 1.237 + public static void main(String argv[]) throws Exception { 1.238 + FileInputStream infile = new FileInputStream(argv[0]); 1.239 + BASE64EncoderStream encoder = new BASE64EncoderStream(System.out); 1.240 + int c; 1.241 + 1.242 + while ((c = infile.read()) != -1) 1.243 + encoder.write(c); 1.244 + encoder.close(); 1.245 + } 1.246 + *** end TEST program **/ 1.247 +}