ohair@286: /* mkos@408: * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.org.jvnet.staxex; ohair@286: ohair@286: import java.io.*; ohair@286: import java.util.logging.Level; ohair@286: import java.util.logging.Logger; ohair@286: import javax.xml.stream.XMLStreamException; ohair@286: import javax.xml.stream.XMLStreamWriter; ohair@286: ohair@286: // for testing method ohair@286: //import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl; ohair@286: ohair@286: /** ohair@286: * This class implements a BASE64 Encoder. It is implemented as ohair@286: * a FilterOutputStream, so one can just wrap this class around ohair@286: * any output stream and write bytes into this filter. The Encoding ohair@286: * is done as the bytes are written out. ohair@286: * ohair@286: * @author John Mani ohair@286: * @author Bill Shannon ohair@286: * @author Martin Grebac ohair@286: */ ohair@286: ohair@286: public class Base64EncoderStream extends FilterOutputStream { ohair@286: private byte[] buffer; // cache of bytes that are yet to be encoded ohair@286: private int bufsize = 0; // size of the cache ohair@286: ohair@286: private XMLStreamWriter outWriter; ohair@286: ohair@286: public Base64EncoderStream(OutputStream out) { ohair@286: super(out); ohair@286: buffer = new byte[3]; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Create a BASE64 encoder that encodes the specified input stream ohair@286: */ ohair@286: public Base64EncoderStream(XMLStreamWriter outWriter, OutputStream out) { ohair@286: super(out); ohair@286: buffer = new byte[3]; ohair@286: this.outWriter = outWriter; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Encodes len bytes from the specified ohair@286: * byte array starting at offset off to ohair@286: * this output stream. ohair@286: * ohair@286: * @param b the data. ohair@286: * @param off the start offset in the data. ohair@286: * @param len the number of bytes to write. ohair@286: * @exception IOException if an I/O error occurs. ohair@286: */ ohair@286: @Override ohair@286: public void write(byte[] b, int off, int len) throws IOException { ohair@286: for (int i = 0; i < len; i++) ohair@286: write(b[off + i]); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Encodes b.length bytes to this output stream. ohair@286: * @param b the data to be written. ohair@286: * @exception IOException if an I/O error occurs. ohair@286: */ ohair@286: @Override ohair@286: public void write(byte[] b) throws IOException { ohair@286: write(b, 0, b.length); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Encodes the specified byte to this output stream. ohair@286: * @param c the byte. ohair@286: * @exception IOException if an I/O error occurs. ohair@286: */ ohair@286: @Override ohair@286: public void write(int c) throws IOException { ohair@286: buffer[bufsize++] = (byte)c; ohair@286: if (bufsize == 3) { // Encoding unit = 3 bytes ohair@286: encode(); ohair@286: bufsize = 0; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Flushes this output stream and forces any buffered output bytes ohair@286: * to be encoded out to the stream. ohair@286: * @exception IOException if an I/O error occurs. ohair@286: */ ohair@286: @Override ohair@286: public void flush() throws IOException { ohair@286: if (bufsize > 0) { // If there's unencoded characters in the buffer .. ohair@286: encode(); // .. encode them ohair@286: bufsize = 0; ohair@286: } ohair@286: out.flush(); ohair@286: try { ohair@286: outWriter.flush(); ohair@286: } catch (XMLStreamException ex) { ohair@286: Logger.getLogger(Base64EncoderStream.class.getName()).log(Level.SEVERE, null, ex); ohair@286: throw new IOException(ex); ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Forces any buffered output bytes to be encoded out to the stream ohair@286: * and closes this output stream ohair@286: */ ohair@286: @Override ohair@286: public void close() throws IOException { ohair@286: flush(); ohair@286: out.close(); ohair@286: } ohair@286: ohair@286: /** This array maps the characters to their 6 bit values */ ohair@286: private final static char pem_array[] = { ohair@286: 'A','B','C','D','E','F','G','H', // 0 ohair@286: 'I','J','K','L','M','N','O','P', // 1 ohair@286: 'Q','R','S','T','U','V','W','X', // 2 ohair@286: 'Y','Z','a','b','c','d','e','f', // 3 ohair@286: 'g','h','i','j','k','l','m','n', // 4 ohair@286: 'o','p','q','r','s','t','u','v', // 5 ohair@286: 'w','x','y','z','0','1','2','3', // 6 ohair@286: '4','5','6','7','8','9','+','/' // 7 ohair@286: }; ohair@286: ohair@286: private void encode() throws IOException { ohair@286: byte a, b, c; ohair@286: char[] buf = new char[4]; ohair@286: if (bufsize == 1) { ohair@286: a = buffer[0]; ohair@286: b = 0; ohair@286: c = 0; ohair@286: buf[0] = pem_array[(a >>> 2) & 0x3F]; ohair@286: buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]; ohair@286: buf[2] = '='; // pad character ohair@286: buf[3] = '='; // pad character ohair@286: } else if (bufsize == 2) { ohair@286: a = buffer[0]; ohair@286: b = buffer[1]; ohair@286: c = 0; ohair@286: buf[0] = pem_array[(a >>> 2) & 0x3F]; ohair@286: buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]; ohair@286: buf[2] = pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]; ohair@286: buf[3] = '='; // pad character ohair@286: } else { ohair@286: a = buffer[0]; ohair@286: b = buffer[1]; ohair@286: c = buffer[2]; ohair@286: buf[0] = pem_array[(a >>> 2) & 0x3F]; ohair@286: buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]; ohair@286: buf[2] = pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]; ohair@286: buf[3] = pem_array[c & 0x3F]; ohair@286: } ohair@286: try { ohair@286: outWriter.writeCharacters(buf, 0, 4); ohair@286: } catch (XMLStreamException ex) { ohair@286: Logger.getLogger(Base64EncoderStream.class.getName()).log(Level.SEVERE, null, ex); ohair@286: throw new IOException(ex); ohair@286: } ohair@286: } ohair@286: ohair@286: // public static void main(String argv[]) throws Exception { ohair@286: // FileInputStream infile = new FileInputStream(new File(argv[0])); ohair@286: // StringWriter sw = new StringWriter(); ohair@286: // XMLStreamWriterImpl wi = new XMLStreamWriterImpl(sw, null); ohair@286: // ByteArrayOutputStream baos = new ByteArrayOutputStream(); ohair@286: // Base64EncoderStream encoder = new Base64EncoderStream(wi, baos); ohair@286: // int c; ohair@286: // ohair@286: // while ((c = infile.read()) != -1) ohair@286: // encoder.write(c); ohair@286: // encoder.close(); ohair@286: // ohair@286: // System.out.println("SW: " + sw.toString()); ohair@286: // System.out.println("BAOS: " + baos.toString()); ohair@286: // ohair@286: // } ohair@286: }