src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/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 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2011, 2012, 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  */
    26 package com.sun.xml.internal.org.jvnet.staxex;
    28 import java.io.*;
    29 import java.util.logging.Level;
    30 import java.util.logging.Logger;
    31 import javax.xml.stream.XMLStreamException;
    32 import javax.xml.stream.XMLStreamWriter;
    34 // for testing method
    35 //import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;
    37 /**
    38  * This class implements a BASE64 Encoder. It is implemented as
    39  * a FilterOutputStream, so one can just wrap this class around
    40  * any output stream and write bytes into this filter. The Encoding
    41  * is done as the bytes are written out.
    42  *
    43  * @author John Mani
    44  * @author Bill Shannon
    45  * @author Martin Grebac
    46  */
    48 public class Base64EncoderStream extends FilterOutputStream {
    49     private byte[] buffer;      // cache of bytes that are yet to be encoded
    50     private int bufsize = 0;    // size of the cache
    52     private XMLStreamWriter outWriter;
    54     public Base64EncoderStream(OutputStream out) {
    55         super(out);
    56         buffer = new byte[3];
    57     }
    59     /**
    60      * Create a BASE64 encoder that encodes the specified input stream
    61      */
    62     public Base64EncoderStream(XMLStreamWriter outWriter, OutputStream out) {
    63         super(out);
    64         buffer = new byte[3];
    65         this.outWriter = outWriter;
    66     }
    68     /**
    69      * Encodes <code>len</code> bytes from the specified
    70      * <code>byte</code> array starting at offset <code>off</code> to
    71      * this output stream.
    72      *
    73      * @param      b     the data.
    74      * @param      off   the start offset in the data.
    75      * @param      len   the number of bytes to write.
    76      * @exception  IOException  if an I/O error occurs.
    77      */
    78     @Override
    79     public void write(byte[] b, int off, int len) throws IOException {
    80         for (int i = 0; i < len; i++)
    81             write(b[off + i]);
    82     }
    84     /**
    85      * Encodes <code>b.length</code> bytes to this output stream.
    86      * @param      b   the data to be written.
    87      * @exception  IOException  if an I/O error occurs.
    88      */
    89     @Override
    90     public void write(byte[] b) throws IOException {
    91         write(b, 0, b.length);
    92     }
    94     /**
    95      * Encodes the specified <code>byte</code> to this output stream.
    96      * @param      c   the <code>byte</code>.
    97      * @exception  IOException  if an I/O error occurs.
    98      */
    99     @Override
   100     public void write(int c) throws IOException {
   101         buffer[bufsize++] = (byte)c;
   102         if (bufsize == 3) { // Encoding unit = 3 bytes
   103             encode();
   104             bufsize = 0;
   105         }
   106     }
   108     /**
   109      * Flushes this output stream and forces any buffered output bytes
   110      * to be encoded out to the stream.
   111      * @exception  IOException  if an I/O error occurs.
   112      */
   113     @Override
   114     public void flush() throws IOException {
   115         if (bufsize > 0) { // If there's unencoded characters in the buffer ..
   116             encode();      // .. encode them
   117             bufsize = 0;
   118         }
   119         out.flush();
   120         try {
   121             outWriter.flush();
   122         } catch (XMLStreamException ex) {
   123             Logger.getLogger(Base64EncoderStream.class.getName()).log(Level.SEVERE, null, ex);
   124             throw new IOException(ex);
   125         }
   126     }
   128     /**
   129      * Forces any buffered output bytes to be encoded out to the stream
   130      * and closes this output stream
   131      */
   132     @Override
   133     public void close() throws IOException {
   134         flush();
   135         out.close();
   136     }
   138     /** This array maps the characters to their 6 bit values */
   139     private final static char pem_array[] = {
   140         'A','B','C','D','E','F','G','H', // 0
   141         'I','J','K','L','M','N','O','P', // 1
   142         'Q','R','S','T','U','V','W','X', // 2
   143         'Y','Z','a','b','c','d','e','f', // 3
   144         'g','h','i','j','k','l','m','n', // 4
   145         'o','p','q','r','s','t','u','v', // 5
   146         'w','x','y','z','0','1','2','3', // 6
   147         '4','5','6','7','8','9','+','/'  // 7
   148     };
   150     private void encode() throws IOException {
   151         byte a, b, c;
   152         char[] buf = new char[4];
   153         if (bufsize == 1) {
   154             a = buffer[0];
   155             b = 0;
   156             c = 0;
   157             buf[0] = pem_array[(a >>> 2) & 0x3F];
   158             buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
   159             buf[2] = '='; // pad character
   160             buf[3] = '='; // pad character
   161         } else if (bufsize == 2) {
   162             a = buffer[0];
   163             b = buffer[1];
   164             c = 0;
   165             buf[0] = pem_array[(a >>> 2) & 0x3F];
   166             buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
   167             buf[2] = pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
   168             buf[3] = '='; // pad character
   169         } else {
   170             a = buffer[0];
   171             b = buffer[1];
   172             c = buffer[2];
   173             buf[0] = pem_array[(a >>> 2) & 0x3F];
   174             buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
   175             buf[2] = pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
   176             buf[3] = pem_array[c & 0x3F];
   177         }
   178         try {
   179             outWriter.writeCharacters(buf, 0, 4);
   180         } catch (XMLStreamException ex) {
   181             Logger.getLogger(Base64EncoderStream.class.getName()).log(Level.SEVERE, null, ex);
   182             throw new IOException(ex);
   183         }
   184     }
   186 //    public static void main(String argv[]) throws Exception {
   187 //      FileInputStream infile = new FileInputStream(new File(argv[0]));
   188 //        StringWriter sw = new StringWriter();
   189 //        XMLStreamWriterImpl wi = new XMLStreamWriterImpl(sw, null);
   190 //        ByteArrayOutputStream baos = new ByteArrayOutputStream();
   191 //      Base64EncoderStream encoder = new Base64EncoderStream(wi, baos);
   192 //      int c;
   193 //
   194 //      while ((c = infile.read()) != -1)
   195 //          encoder.write(c);
   196 //      encoder.close();
   197 //
   198 //        System.out.println("SW: " + sw.toString());
   199 //        System.out.println("BAOS: " + baos.toString());
   200 //
   201 //    }
   202 }

mercurial