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

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.org.jvnet.staxex;
aoqi@0 27
aoqi@0 28 import java.io.*;
aoqi@0 29 import java.util.logging.Level;
aoqi@0 30 import java.util.logging.Logger;
aoqi@0 31 import javax.xml.stream.XMLStreamException;
aoqi@0 32 import javax.xml.stream.XMLStreamWriter;
aoqi@0 33
aoqi@0 34 // for testing method
aoqi@0 35 //import com.sun.xml.internal.stream.writers.XMLStreamWriterImpl;
aoqi@0 36
aoqi@0 37 /**
aoqi@0 38 * This class implements a BASE64 Encoder. It is implemented as
aoqi@0 39 * a FilterOutputStream, so one can just wrap this class around
aoqi@0 40 * any output stream and write bytes into this filter. The Encoding
aoqi@0 41 * is done as the bytes are written out.
aoqi@0 42 *
aoqi@0 43 * @author John Mani
aoqi@0 44 * @author Bill Shannon
aoqi@0 45 * @author Martin Grebac
aoqi@0 46 */
aoqi@0 47
aoqi@0 48 public class Base64EncoderStream extends FilterOutputStream {
aoqi@0 49 private byte[] buffer; // cache of bytes that are yet to be encoded
aoqi@0 50 private int bufsize = 0; // size of the cache
aoqi@0 51
aoqi@0 52 private XMLStreamWriter outWriter;
aoqi@0 53
aoqi@0 54 public Base64EncoderStream(OutputStream out) {
aoqi@0 55 super(out);
aoqi@0 56 buffer = new byte[3];
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * Create a BASE64 encoder that encodes the specified input stream
aoqi@0 61 */
aoqi@0 62 public Base64EncoderStream(XMLStreamWriter outWriter, OutputStream out) {
aoqi@0 63 super(out);
aoqi@0 64 buffer = new byte[3];
aoqi@0 65 this.outWriter = outWriter;
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 /**
aoqi@0 69 * Encodes <code>len</code> bytes from the specified
aoqi@0 70 * <code>byte</code> array starting at offset <code>off</code> to
aoqi@0 71 * this output stream.
aoqi@0 72 *
aoqi@0 73 * @param b the data.
aoqi@0 74 * @param off the start offset in the data.
aoqi@0 75 * @param len the number of bytes to write.
aoqi@0 76 * @exception IOException if an I/O error occurs.
aoqi@0 77 */
aoqi@0 78 @Override
aoqi@0 79 public void write(byte[] b, int off, int len) throws IOException {
aoqi@0 80 for (int i = 0; i < len; i++)
aoqi@0 81 write(b[off + i]);
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 /**
aoqi@0 85 * Encodes <code>b.length</code> bytes to this output stream.
aoqi@0 86 * @param b the data to be written.
aoqi@0 87 * @exception IOException if an I/O error occurs.
aoqi@0 88 */
aoqi@0 89 @Override
aoqi@0 90 public void write(byte[] b) throws IOException {
aoqi@0 91 write(b, 0, b.length);
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 /**
aoqi@0 95 * Encodes the specified <code>byte</code> to this output stream.
aoqi@0 96 * @param c the <code>byte</code>.
aoqi@0 97 * @exception IOException if an I/O error occurs.
aoqi@0 98 */
aoqi@0 99 @Override
aoqi@0 100 public void write(int c) throws IOException {
aoqi@0 101 buffer[bufsize++] = (byte)c;
aoqi@0 102 if (bufsize == 3) { // Encoding unit = 3 bytes
aoqi@0 103 encode();
aoqi@0 104 bufsize = 0;
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 /**
aoqi@0 109 * Flushes this output stream and forces any buffered output bytes
aoqi@0 110 * to be encoded out to the stream.
aoqi@0 111 * @exception IOException if an I/O error occurs.
aoqi@0 112 */
aoqi@0 113 @Override
aoqi@0 114 public void flush() throws IOException {
aoqi@0 115 if (bufsize > 0) { // If there's unencoded characters in the buffer ..
aoqi@0 116 encode(); // .. encode them
aoqi@0 117 bufsize = 0;
aoqi@0 118 }
aoqi@0 119 out.flush();
aoqi@0 120 try {
aoqi@0 121 outWriter.flush();
aoqi@0 122 } catch (XMLStreamException ex) {
aoqi@0 123 Logger.getLogger(Base64EncoderStream.class.getName()).log(Level.SEVERE, null, ex);
aoqi@0 124 throw new IOException(ex);
aoqi@0 125 }
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 /**
aoqi@0 129 * Forces any buffered output bytes to be encoded out to the stream
aoqi@0 130 * and closes this output stream
aoqi@0 131 */
aoqi@0 132 @Override
aoqi@0 133 public void close() throws IOException {
aoqi@0 134 flush();
aoqi@0 135 out.close();
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 /** This array maps the characters to their 6 bit values */
aoqi@0 139 private final static char pem_array[] = {
aoqi@0 140 'A','B','C','D','E','F','G','H', // 0
aoqi@0 141 'I','J','K','L','M','N','O','P', // 1
aoqi@0 142 'Q','R','S','T','U','V','W','X', // 2
aoqi@0 143 'Y','Z','a','b','c','d','e','f', // 3
aoqi@0 144 'g','h','i','j','k','l','m','n', // 4
aoqi@0 145 'o','p','q','r','s','t','u','v', // 5
aoqi@0 146 'w','x','y','z','0','1','2','3', // 6
aoqi@0 147 '4','5','6','7','8','9','+','/' // 7
aoqi@0 148 };
aoqi@0 149
aoqi@0 150 private void encode() throws IOException {
aoqi@0 151 byte a, b, c;
aoqi@0 152 char[] buf = new char[4];
aoqi@0 153 if (bufsize == 1) {
aoqi@0 154 a = buffer[0];
aoqi@0 155 b = 0;
aoqi@0 156 c = 0;
aoqi@0 157 buf[0] = pem_array[(a >>> 2) & 0x3F];
aoqi@0 158 buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
aoqi@0 159 buf[2] = '='; // pad character
aoqi@0 160 buf[3] = '='; // pad character
aoqi@0 161 } else if (bufsize == 2) {
aoqi@0 162 a = buffer[0];
aoqi@0 163 b = buffer[1];
aoqi@0 164 c = 0;
aoqi@0 165 buf[0] = pem_array[(a >>> 2) & 0x3F];
aoqi@0 166 buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
aoqi@0 167 buf[2] = pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
aoqi@0 168 buf[3] = '='; // pad character
aoqi@0 169 } else {
aoqi@0 170 a = buffer[0];
aoqi@0 171 b = buffer[1];
aoqi@0 172 c = buffer[2];
aoqi@0 173 buf[0] = pem_array[(a >>> 2) & 0x3F];
aoqi@0 174 buf[1] = pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)];
aoqi@0 175 buf[2] = pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)];
aoqi@0 176 buf[3] = pem_array[c & 0x3F];
aoqi@0 177 }
aoqi@0 178 try {
aoqi@0 179 outWriter.writeCharacters(buf, 0, 4);
aoqi@0 180 } catch (XMLStreamException ex) {
aoqi@0 181 Logger.getLogger(Base64EncoderStream.class.getName()).log(Level.SEVERE, null, ex);
aoqi@0 182 throw new IOException(ex);
aoqi@0 183 }
aoqi@0 184 }
aoqi@0 185
aoqi@0 186 // public static void main(String argv[]) throws Exception {
aoqi@0 187 // FileInputStream infile = new FileInputStream(new File(argv[0]));
aoqi@0 188 // StringWriter sw = new StringWriter();
aoqi@0 189 // XMLStreamWriterImpl wi = new XMLStreamWriterImpl(sw, null);
aoqi@0 190 // ByteArrayOutputStream baos = new ByteArrayOutputStream();
aoqi@0 191 // Base64EncoderStream encoder = new Base64EncoderStream(wi, baos);
aoqi@0 192 // int c;
aoqi@0 193 //
aoqi@0 194 // while ((c = infile.read()) != -1)
aoqi@0 195 // encoder.write(c);
aoqi@0 196 // encoder.close();
aoqi@0 197 //
aoqi@0 198 // System.out.println("SW: " + sw.toString());
aoqi@0 199 // System.out.println("BAOS: " + baos.toString());
aoqi@0 200 //
aoqi@0 201 // }
aoqi@0 202 }

mercurial