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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 /*
ohair@286 27 * @(#)BASE64EncoderStream.java 1.6 02/03/27
ohair@286 28 */
ohair@286 29
ohair@286 30
ohair@286 31
ohair@286 32 package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
ohair@286 33
ohair@286 34 import java.io.*;
ohair@286 35
ohair@286 36 /**
ohair@286 37 * This class implements a BASE64 Encoder. It is implemented as
ohair@286 38 * a FilterOutputStream, so one can just wrap this class around
ohair@286 39 * any output stream and write bytes into this filter. The Encoding
ohair@286 40 * is done as the bytes are written out.
ohair@286 41 *
ohair@286 42 * @author John Mani
ohair@286 43 * @author Bill Shannon
ohair@286 44 */
ohair@286 45
ohair@286 46 public class BASE64EncoderStream extends FilterOutputStream {
ohair@286 47 private byte[] buffer; // cache of bytes that are yet to be encoded
ohair@286 48 private int bufsize = 0; // size of the cache
ohair@286 49 private int count = 0; // number of bytes that have been output
ohair@286 50 private int bytesPerLine; // number of bytes per line
ohair@286 51
ohair@286 52 /**
ohair@286 53 * Create a BASE64 encoder that encodes the specified input stream
ohair@286 54 * @param out the output stream
ohair@286 55 * @param bytesPerLine number of bytes per line. The encoder inserts
ohair@286 56 * a CRLF sequence after the specified number of bytes
ohair@286 57 */
ohair@286 58 public BASE64EncoderStream(OutputStream out, int bytesPerLine) {
ohair@286 59 super(out);
ohair@286 60 buffer = new byte[3];
ohair@286 61 this.bytesPerLine = bytesPerLine;
ohair@286 62 }
ohair@286 63
ohair@286 64 /**
ohair@286 65 * Create a BASE64 encoder that encodes the specified input stream.
ohair@286 66 * Inserts the CRLF sequence after outputting 76 bytes.
ohair@286 67 * @param out the output stream
ohair@286 68 */
ohair@286 69 public BASE64EncoderStream(OutputStream out) {
ohair@286 70 this(out, 76);
ohair@286 71 }
ohair@286 72
ohair@286 73 /**
ohair@286 74 * Encodes <code>len</code> bytes from the specified
ohair@286 75 * <code>byte</code> array starting at offset <code>off</code> to
ohair@286 76 * this output stream.
ohair@286 77 *
ohair@286 78 * @param b the data.
ohair@286 79 * @param off the start offset in the data.
ohair@286 80 * @param len the number of bytes to write.
ohair@286 81 * @exception IOException if an I/O error occurs.
ohair@286 82 */
ohair@286 83 public void write(byte[] b, int off, int len) throws IOException {
ohair@286 84 for (int i = 0; i < len; i++)
ohair@286 85 write(b[off + i]);
ohair@286 86 }
ohair@286 87
ohair@286 88 /**
ohair@286 89 * Encodes <code>b.length</code> bytes to this output stream.
ohair@286 90 * @param b the data to be written.
ohair@286 91 * @exception IOException if an I/O error occurs.
ohair@286 92 */
ohair@286 93 public void write(byte[] b) throws IOException {
ohair@286 94 write(b, 0, b.length);
ohair@286 95 }
ohair@286 96
ohair@286 97 /**
ohair@286 98 * Encodes the specified <code>byte</code> to this output stream.
ohair@286 99 * @param c the <code>byte</code>.
ohair@286 100 * @exception IOException if an I/O error occurs.
ohair@286 101 */
ohair@286 102 public void write(int c) throws IOException {
ohair@286 103 buffer[bufsize++] = (byte)c;
ohair@286 104 if (bufsize == 3) { // Encoding unit = 3 bytes
ohair@286 105 encode();
ohair@286 106 bufsize = 0;
ohair@286 107 }
ohair@286 108 }
ohair@286 109
ohair@286 110 /**
ohair@286 111 * Flushes this output stream and forces any buffered output bytes
ohair@286 112 * to be encoded out to the stream.
ohair@286 113 * @exception IOException if an I/O error occurs.
ohair@286 114 */
ohair@286 115 public void flush() throws IOException {
ohair@286 116 if (bufsize > 0) { // If there's unencoded characters in the buffer ..
ohair@286 117 encode(); // .. encode them
ohair@286 118 bufsize = 0;
ohair@286 119 }
ohair@286 120 out.flush();
ohair@286 121 }
ohair@286 122
ohair@286 123 /**
ohair@286 124 * Forces any buffered output bytes to be encoded out to the stream
ohair@286 125 * and closes this output stream
ohair@286 126 */
ohair@286 127 public void close() throws IOException {
ohair@286 128 flush();
ohair@286 129 out.close();
ohair@286 130 }
ohair@286 131
ohair@286 132 /** This array maps the characters to their 6 bit values */
ohair@286 133 private final static char pem_array[] = {
ohair@286 134 'A','B','C','D','E','F','G','H', // 0
ohair@286 135 'I','J','K','L','M','N','O','P', // 1
ohair@286 136 'Q','R','S','T','U','V','W','X', // 2
ohair@286 137 'Y','Z','a','b','c','d','e','f', // 3
ohair@286 138 'g','h','i','j','k','l','m','n', // 4
ohair@286 139 'o','p','q','r','s','t','u','v', // 5
ohair@286 140 'w','x','y','z','0','1','2','3', // 6
ohair@286 141 '4','5','6','7','8','9','+','/' // 7
ohair@286 142 };
ohair@286 143
ohair@286 144 private void encode() throws IOException {
ohair@286 145 // If writing out this encoded unit will cause overflow,
ohair@286 146 // start a new line.
ohair@286 147 if (count + 4 > bytesPerLine) {
ohair@286 148 out.write('\r');
ohair@286 149 out.write('\n');
ohair@286 150 count = 0;
ohair@286 151 }
ohair@286 152
ohair@286 153 byte a, b, c;
ohair@286 154 if (bufsize == 1) {
ohair@286 155 a = buffer[0];
ohair@286 156 b = 0;
ohair@286 157 c = 0;
ohair@286 158 out.write(pem_array[(a >>> 2) & 0x3F]);
ohair@286 159 out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
ohair@286 160 out.write('='); // pad character
ohair@286 161 out.write('='); // pad character
ohair@286 162 } else if (bufsize == 2) {
ohair@286 163 a = buffer[0];
ohair@286 164 b = buffer[1];
ohair@286 165 c = 0;
ohair@286 166 out.write(pem_array[(a >>> 2) & 0x3F]);
ohair@286 167 out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
ohair@286 168 out.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
ohair@286 169 out.write('='); // pad character
ohair@286 170 } else {
ohair@286 171 a = buffer[0];
ohair@286 172 b = buffer[1];
ohair@286 173 c = buffer[2];
ohair@286 174 out.write(pem_array[(a >>> 2) & 0x3F]);
ohair@286 175 out.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
ohair@286 176 out.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
ohair@286 177 out.write(pem_array[c & 0x3F]);
ohair@286 178 }
ohair@286 179
ohair@286 180 // increment count
ohair@286 181 count += 4;
ohair@286 182 }
ohair@286 183
ohair@286 184 /**
ohair@286 185 * Base64 encode a byte array. No line breaks are inserted.
ohair@286 186 * This method is suitable for short strings, such as those
ohair@286 187 * in the IMAP AUTHENTICATE protocol, but not to encode the
ohair@286 188 * entire content of a MIME part.
ohair@286 189 */
ohair@286 190 public static byte[] encode(byte[] inbuf) {
ohair@286 191 if (inbuf.length == 0)
ohair@286 192 return inbuf;
ohair@286 193 byte[] outbuf = new byte[((inbuf.length + 2) / 3) * 4];
ohair@286 194 int inpos = 0, outpos = 0;
ohair@286 195 int size = inbuf.length;
ohair@286 196 while (size > 0) {
ohair@286 197 byte a, b, c;
ohair@286 198 if (size == 1) {
ohair@286 199 a = inbuf[inpos++];
ohair@286 200 b = 0;
ohair@286 201 c = 0;
ohair@286 202 outbuf[outpos++] = (byte)pem_array[(a >>> 2) & 0x3F];
ohair@286 203 outbuf[outpos++] =
ohair@286 204 (byte)pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
ohair@286 205 outbuf[outpos++] = (byte)'='; // pad character
ohair@286 206 outbuf[outpos++] = (byte)'='; // pad character
ohair@286 207 } else if (size == 2) {
ohair@286 208 a = inbuf[inpos++];
ohair@286 209 b = inbuf[inpos++];
ohair@286 210 c = 0;
ohair@286 211 outbuf[outpos++] = (byte)pem_array[(a >>> 2) & 0x3F];
ohair@286 212 outbuf[outpos++] =
ohair@286 213 (byte)pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
ohair@286 214 outbuf[outpos++] =
ohair@286 215 (byte)pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
ohair@286 216 outbuf[outpos++] = (byte)'='; // pad character
ohair@286 217 } else {
ohair@286 218 a = inbuf[inpos++];
ohair@286 219 b = inbuf[inpos++];
ohair@286 220 c = inbuf[inpos++];
ohair@286 221 outbuf[outpos++] = (byte)pem_array[(a >>> 2) & 0x3F];
ohair@286 222 outbuf[outpos++] =
ohair@286 223 (byte)pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
ohair@286 224 outbuf[outpos++] =
ohair@286 225 (byte)pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
ohair@286 226 outbuf[outpos++] = (byte)pem_array[c & 0x3F];
ohair@286 227 }
ohair@286 228 size -= 3;
ohair@286 229 }
ohair@286 230 return outbuf;
ohair@286 231 }
ohair@286 232
ohair@286 233 /*** begin TEST program
ohair@286 234 public static void main(String argv[]) throws Exception {
ohair@286 235 FileInputStream infile = new FileInputStream(argv[0]);
ohair@286 236 BASE64EncoderStream encoder = new BASE64EncoderStream(System.out);
ohair@286 237 int c;
ohair@286 238
ohair@286 239 while ((c = infile.read()) != -1)
ohair@286 240 encoder.write(c);
ohair@286 241 encoder.close();
ohair@286 242 }
ohair@286 243 *** end TEST program **/
ohair@286 244 }

mercurial