src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/Base64EncoderStream.java

changeset 286
f50545b5e2f1
child 408
b0610cd08440
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/staxex/Base64EncoderStream.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,202 @@
     1.4 +/*
     1.5 + * Copyright (c) 2011, 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 +package com.sun.xml.internal.org.jvnet.staxex;
    1.30 +
    1.31 +import java.io.*;
    1.32 +import java.util.logging.Level;
    1.33 +import java.util.logging.Logger;
    1.34 +import javax.xml.stream.XMLStreamException;
    1.35 +import javax.xml.stream.XMLStreamWriter;
    1.36 +
    1.37 +// for testing method
    1.38 +//import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;
    1.39 +
    1.40 +/**
    1.41 + * This class implements a BASE64 Encoder. It is implemented as
    1.42 + * a FilterOutputStream, so one can just wrap this class around
    1.43 + * any output stream and write bytes into this filter. The Encoding
    1.44 + * is done as the bytes are written out.
    1.45 + *
    1.46 + * @author John Mani
    1.47 + * @author Bill Shannon
    1.48 + * @author Martin Grebac
    1.49 + */
    1.50 +
    1.51 +public class Base64EncoderStream extends FilterOutputStream {
    1.52 +    private byte[] buffer;      // cache of bytes that are yet to be encoded
    1.53 +    private int bufsize = 0;    // size of the cache
    1.54 +
    1.55 +    private XMLStreamWriter outWriter;
    1.56 +
    1.57 +    public Base64EncoderStream(OutputStream out) {
    1.58 +        super(out);
    1.59 +        buffer = new byte[3];
    1.60 +    }
    1.61 +
    1.62 +    /**
    1.63 +     * Create a BASE64 encoder that encodes the specified input stream
    1.64 +     */
    1.65 +    public Base64EncoderStream(XMLStreamWriter outWriter, OutputStream out) {
    1.66 +        super(out);
    1.67 +        buffer = new byte[3];
    1.68 +        this.outWriter = outWriter;
    1.69 +    }
    1.70 +
    1.71 +    /**
    1.72 +     * Encodes <code>len</code> bytes from the specified
    1.73 +     * <code>byte</code> array starting at offset <code>off</code> to
    1.74 +     * this output stream.
    1.75 +     *
    1.76 +     * @param      b     the data.
    1.77 +     * @param      off   the start offset in the data.
    1.78 +     * @param      len   the number of bytes to write.
    1.79 +     * @exception  IOException  if an I/O error occurs.
    1.80 +     */
    1.81 +    @Override
    1.82 +    public void write(byte[] b, int off, int len) throws IOException {
    1.83 +        for (int i = 0; i < len; i++)
    1.84 +            write(b[off + i]);
    1.85 +    }
    1.86 +
    1.87 +    /**
    1.88 +     * Encodes <code>b.length</code> bytes to this output stream.
    1.89 +     * @param      b   the data to be written.
    1.90 +     * @exception  IOException  if an I/O error occurs.
    1.91 +     */
    1.92 +    @Override
    1.93 +    public void write(byte[] b) throws IOException {
    1.94 +        write(b, 0, b.length);
    1.95 +    }
    1.96 +
    1.97 +    /**
    1.98 +     * Encodes the specified <code>byte</code> to this output stream.
    1.99 +     * @param      c   the <code>byte</code>.
   1.100 +     * @exception  IOException  if an I/O error occurs.
   1.101 +     */
   1.102 +    @Override
   1.103 +    public void write(int c) throws IOException {
   1.104 +        buffer[bufsize++] = (byte)c;
   1.105 +        if (bufsize == 3) { // Encoding unit = 3 bytes
   1.106 +            encode();
   1.107 +            bufsize = 0;
   1.108 +        }
   1.109 +    }
   1.110 +
   1.111 +    /**
   1.112 +     * Flushes this output stream and forces any buffered output bytes
   1.113 +     * to be encoded out to the stream.
   1.114 +     * @exception  IOException  if an I/O error occurs.
   1.115 +     */
   1.116 +    @Override
   1.117 +    public void flush() throws IOException {
   1.118 +        if (bufsize > 0) { // If there's unencoded characters in the buffer ..
   1.119 +            encode();      // .. encode them
   1.120 +            bufsize = 0;
   1.121 +        }
   1.122 +        out.flush();
   1.123 +        try {
   1.124 +            outWriter.flush();
   1.125 +        } catch (XMLStreamException ex) {
   1.126 +            Logger.getLogger(Base64EncoderStream.class.getName()).log(Level.SEVERE, null, ex);
   1.127 +            throw new IOException(ex);
   1.128 +        }
   1.129 +    }
   1.130 +
   1.131 +    /**
   1.132 +     * Forces any buffered output bytes to be encoded out to the stream
   1.133 +     * and closes this output stream
   1.134 +     */
   1.135 +    @Override
   1.136 +    public void close() throws IOException {
   1.137 +        flush();
   1.138 +        out.close();
   1.139 +    }
   1.140 +
   1.141 +    /** This array maps the characters to their 6 bit values */
   1.142 +    private final static char pem_array[] = {
   1.143 +        'A','B','C','D','E','F','G','H', // 0
   1.144 +        'I','J','K','L','M','N','O','P', // 1
   1.145 +        'Q','R','S','T','U','V','W','X', // 2
   1.146 +        'Y','Z','a','b','c','d','e','f', // 3
   1.147 +        'g','h','i','j','k','l','m','n', // 4
   1.148 +        'o','p','q','r','s','t','u','v', // 5
   1.149 +        'w','x','y','z','0','1','2','3', // 6
   1.150 +        '4','5','6','7','8','9','+','/'  // 7
   1.151 +    };
   1.152 +
   1.153 +    private void encode() throws IOException {
   1.154 +        byte a, b, c;
   1.155 +        char[] buf = new char[4];
   1.156 +        if (bufsize == 1) {
   1.157 +            a = buffer[0];
   1.158 +            b = 0;
   1.159 +            c = 0;
   1.160 +            buf[0] = pem_array[(a >>> 2) & 0x3F];
   1.161 +            buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
   1.162 +            buf[2] = '='; // pad character
   1.163 +            buf[3] = '='; // pad character
   1.164 +        } else if (bufsize == 2) {
   1.165 +            a = buffer[0];
   1.166 +            b = buffer[1];
   1.167 +            c = 0;
   1.168 +            buf[0] = pem_array[(a >>> 2) & 0x3F];
   1.169 +            buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
   1.170 +            buf[2] = pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
   1.171 +            buf[3] = '='; // pad character
   1.172 +        } else {
   1.173 +            a = buffer[0];
   1.174 +            b = buffer[1];
   1.175 +            c = buffer[2];
   1.176 +            buf[0] = pem_array[(a >>> 2) & 0x3F];
   1.177 +            buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
   1.178 +            buf[2] = pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
   1.179 +            buf[3] = pem_array[c & 0x3F];
   1.180 +        }
   1.181 +        try {
   1.182 +            outWriter.writeCharacters(buf, 0, 4);
   1.183 +        } catch (XMLStreamException ex) {
   1.184 +            Logger.getLogger(Base64EncoderStream.class.getName()).log(Level.SEVERE, null, ex);
   1.185 +            throw new IOException(ex);
   1.186 +        }
   1.187 +    }
   1.188 +
   1.189 +//    public static void main(String argv[]) throws Exception {
   1.190 +//      FileInputStream infile = new FileInputStream(new File(argv[0]));
   1.191 +//        StringWriter sw = new StringWriter();
   1.192 +//        XMLStreamWriterImpl wi = new XMLStreamWriterImpl(sw, null);
   1.193 +//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1.194 +//      Base64EncoderStream encoder = new Base64EncoderStream(wi, baos);
   1.195 +//      int c;
   1.196 +//
   1.197 +//      while ((c = infile.read()) != -1)
   1.198 +//          encoder.write(c);
   1.199 +//      encoder.close();
   1.200 +//
   1.201 +//        System.out.println("SW: " + sw.toString());
   1.202 +//        System.out.println("BAOS: " + baos.toString());
   1.203 +//
   1.204 +//    }
   1.205 +}

mercurial