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