src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/BASE64EncoderStream.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

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 * @(#)BASE64EncoderStream.java 1.6 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 Encoder. It is implemented as
aoqi@0 38 * a FilterOutputStream, so one can just wrap this class around
aoqi@0 39 * any output stream and write bytes into this filter. The Encoding
aoqi@0 40 * is done as the bytes are written 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 BASE64EncoderStream extends FilterOutputStream {
aoqi@0 47 private byte[] buffer; // cache of bytes that are yet to be encoded
aoqi@0 48 private int bufsize = 0; // size of the cache
aoqi@0 49 private int count = 0; // number of bytes that have been output
aoqi@0 50 private int bytesPerLine; // number of bytes per line
aoqi@0 51
aoqi@0 52 /**
aoqi@0 53 * Create a BASE64 encoder that encodes the specified input stream
aoqi@0 54 * @param out the output stream
aoqi@0 55 * @param bytesPerLine number of bytes per line. The encoder inserts
aoqi@0 56 * a CRLF sequence after the specified number of bytes
aoqi@0 57 */
aoqi@0 58 public BASE64EncoderStream(OutputStream out, int bytesPerLine) {
aoqi@0 59 super(out);
aoqi@0 60 buffer = new byte[3];
aoqi@0 61 this.bytesPerLine = bytesPerLine;
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 /**
aoqi@0 65 * Create a BASE64 encoder that encodes the specified input stream.
aoqi@0 66 * Inserts the CRLF sequence after outputting 76 bytes.
aoqi@0 67 * @param out the output stream
aoqi@0 68 */
aoqi@0 69 public BASE64EncoderStream(OutputStream out) {
aoqi@0 70 this(out, 76);
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 /**
aoqi@0 74 * Encodes <code>len</code> bytes from the specified
aoqi@0 75 * <code>byte</code> array starting at offset <code>off</code> to
aoqi@0 76 * this output stream.
aoqi@0 77 *
aoqi@0 78 * @param b the data.
aoqi@0 79 * @param off the start offset in the data.
aoqi@0 80 * @param len the number of bytes to write.
aoqi@0 81 * @exception IOException if an I/O error occurs.
aoqi@0 82 */
aoqi@0 83 public void write(byte[] b, int off, int len) throws IOException {
aoqi@0 84 for (int i = 0; i < len; i++)
aoqi@0 85 write(b[off + i]);
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 /**
aoqi@0 89 * Encodes <code>b.length</code> bytes to this output stream.
aoqi@0 90 * @param b the data to be written.
aoqi@0 91 * @exception IOException if an I/O error occurs.
aoqi@0 92 */
aoqi@0 93 public void write(byte[] b) throws IOException {
aoqi@0 94 write(b, 0, b.length);
aoqi@0 95 }
aoqi@0 96
aoqi@0 97 /**
aoqi@0 98 * Encodes the specified <code>byte</code> to this output stream.
aoqi@0 99 * @param c the <code>byte</code>.
aoqi@0 100 * @exception IOException if an I/O error occurs.
aoqi@0 101 */
aoqi@0 102 public void write(int c) throws IOException {
aoqi@0 103 buffer[bufsize++] = (byte)c;
aoqi@0 104 if (bufsize == 3) { // Encoding unit = 3 bytes
aoqi@0 105 encode();
aoqi@0 106 bufsize = 0;
aoqi@0 107 }
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 /**
aoqi@0 111 * Flushes this output stream and forces any buffered output bytes
aoqi@0 112 * to be encoded out to the stream.
aoqi@0 113 * @exception IOException if an I/O error occurs.
aoqi@0 114 */
aoqi@0 115 public void flush() throws IOException {
aoqi@0 116 if (bufsize > 0) { // If there's unencoded characters in the buffer ..
aoqi@0 117 encode(); // .. encode them
aoqi@0 118 bufsize = 0;
aoqi@0 119 }
aoqi@0 120 out.flush();
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 /**
aoqi@0 124 * Forces any buffered output bytes to be encoded out to the stream
aoqi@0 125 * and closes this output stream
aoqi@0 126 */
aoqi@0 127 public void close() throws IOException {
aoqi@0 128 flush();
aoqi@0 129 out.close();
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 /** This array maps the characters to their 6 bit values */
aoqi@0 133 private final static char pem_array[] = {
aoqi@0 134 'A','B','C','D','E','F','G','H', // 0
aoqi@0 135 'I','J','K','L','M','N','O','P', // 1
aoqi@0 136 'Q','R','S','T','U','V','W','X', // 2
aoqi@0 137 'Y','Z','a','b','c','d','e','f', // 3
aoqi@0 138 'g','h','i','j','k','l','m','n', // 4
aoqi@0 139 'o','p','q','r','s','t','u','v', // 5
aoqi@0 140 'w','x','y','z','0','1','2','3', // 6
aoqi@0 141 '4','5','6','7','8','9','+','/' // 7
aoqi@0 142 };
aoqi@0 143
aoqi@0 144 private void encode() throws IOException {
aoqi@0 145 // If writing out this encoded unit will cause overflow,
aoqi@0 146 // start a new line.
aoqi@0 147 if (count + 4 > bytesPerLine) {
aoqi@0 148 out.write('\r');
aoqi@0 149 out.write('\n');
aoqi@0 150 count = 0;
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 byte a, b, c;
aoqi@0 154 if (bufsize == 1) {
aoqi@0 155 a = buffer[0];
aoqi@0 156 b = 0;
aoqi@0 157 c = 0;
aoqi@0 158 out.write(pem_array[(a >>> 2) & 0x3F]);
aoqi@0 159 out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
aoqi@0 160 out.write('='); // pad character
aoqi@0 161 out.write('='); // pad character
aoqi@0 162 } else if (bufsize == 2) {
aoqi@0 163 a = buffer[0];
aoqi@0 164 b = buffer[1];
aoqi@0 165 c = 0;
aoqi@0 166 out.write(pem_array[(a >>> 2) & 0x3F]);
aoqi@0 167 out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
aoqi@0 168 out.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
aoqi@0 169 out.write('='); // pad character
aoqi@0 170 } else {
aoqi@0 171 a = buffer[0];
aoqi@0 172 b = buffer[1];
aoqi@0 173 c = buffer[2];
aoqi@0 174 out.write(pem_array[(a >>> 2) & 0x3F]);
aoqi@0 175 out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
aoqi@0 176 out.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
aoqi@0 177 out.write(pem_array[c & 0x3F]);
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 // increment count
aoqi@0 181 count += 4;
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 /**
aoqi@0 185 * Base64 encode a byte array. No line breaks are inserted.
aoqi@0 186 * This method is suitable for short strings, such as those
aoqi@0 187 * in the IMAP AUTHENTICATE protocol, but not to encode the
aoqi@0 188 * entire content of a MIME part.
aoqi@0 189 */
aoqi@0 190 public static byte[] encode(byte[] inbuf) {
aoqi@0 191 if (inbuf.length == 0)
aoqi@0 192 return inbuf;
aoqi@0 193 byte[] outbuf = new byte[((inbuf.length + 2) / 3) * 4];
aoqi@0 194 int inpos = 0, outpos = 0;
aoqi@0 195 int size = inbuf.length;
aoqi@0 196 while (size > 0) {
aoqi@0 197 byte a, b, c;
aoqi@0 198 if (size == 1) {
aoqi@0 199 a = inbuf[inpos++];
aoqi@0 200 b = 0;
aoqi@0 201 c = 0;
aoqi@0 202 outbuf[outpos++] = (byte)pem_array[(a >>> 2) & 0x3F];
aoqi@0 203 outbuf[outpos++] =
aoqi@0 204 (byte)pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
aoqi@0 205 outbuf[outpos++] = (byte)'='; // pad character
aoqi@0 206 outbuf[outpos++] = (byte)'='; // pad character
aoqi@0 207 } else if (size == 2) {
aoqi@0 208 a = inbuf[inpos++];
aoqi@0 209 b = inbuf[inpos++];
aoqi@0 210 c = 0;
aoqi@0 211 outbuf[outpos++] = (byte)pem_array[(a >>> 2) & 0x3F];
aoqi@0 212 outbuf[outpos++] =
aoqi@0 213 (byte)pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
aoqi@0 214 outbuf[outpos++] =
aoqi@0 215 (byte)pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
aoqi@0 216 outbuf[outpos++] = (byte)'='; // pad character
aoqi@0 217 } else {
aoqi@0 218 a = inbuf[inpos++];
aoqi@0 219 b = inbuf[inpos++];
aoqi@0 220 c = inbuf[inpos++];
aoqi@0 221 outbuf[outpos++] = (byte)pem_array[(a >>> 2) & 0x3F];
aoqi@0 222 outbuf[outpos++] =
aoqi@0 223 (byte)pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
aoqi@0 224 outbuf[outpos++] =
aoqi@0 225 (byte)pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
aoqi@0 226 outbuf[outpos++] = (byte)pem_array[c & 0x3F];
aoqi@0 227 }
aoqi@0 228 size -= 3;
aoqi@0 229 }
aoqi@0 230 return outbuf;
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 /*** begin TEST program
aoqi@0 234 public static void main(String argv[]) throws Exception {
aoqi@0 235 FileInputStream infile = new FileInputStream(argv[0]);
aoqi@0 236 BASE64EncoderStream encoder = new BASE64EncoderStream(System.out);
aoqi@0 237 int c;
aoqi@0 238
aoqi@0 239 while ((c = infile.read()) != -1)
aoqi@0 240 encoder.write(c);
aoqi@0 241 encoder.close();
aoqi@0 242 }
aoqi@0 243 *** end TEST program **/
aoqi@0 244 }

mercurial