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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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

mercurial