src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/UUEncoderStream.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 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 /*
ohair@286 27 * @(#)UUEncoderStream.java 1.3 02/03/27
ohair@286 28 */
ohair@286 29
ohair@286 30
ohair@286 31
ohair@286 32 package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
ohair@286 33
ohair@286 34 import java.io.*;
ohair@286 35
ohair@286 36 /**
ohair@286 37 * This class implements a UUEncoder. It is implemented as
ohair@286 38 * a FilterOutputStream, so one can just wrap this class around
ohair@286 39 * any output stream and write bytes into this filter. The Encoding
ohair@286 40 * is done as the bytes are written out.
ohair@286 41 *
ohair@286 42 * @author John Mani
ohair@286 43 */
ohair@286 44
ohair@286 45 public class UUEncoderStream extends FilterOutputStream {
ohair@286 46 private byte[] buffer; // cache of bytes that are yet to be encoded
ohair@286 47 private int bufsize = 0; // size of the cache
ohair@286 48 private boolean wrotePrefix = false;
ohair@286 49
ohair@286 50 protected String name; // name of file
ohair@286 51 protected int mode; // permissions mode
ohair@286 52
ohair@286 53 /**
ohair@286 54 * Create a UUencoder that encodes the specified input stream
ohair@286 55 * @param out the output stream
ohair@286 56 */
ohair@286 57 public UUEncoderStream(OutputStream out) {
ohair@286 58 this(out, "encoder.buf", 644);
ohair@286 59 }
ohair@286 60
ohair@286 61 /**
ohair@286 62 * Create a UUencoder that encodes the specified input stream
ohair@286 63 * @param out the output stream
ohair@286 64 * @param name Specifies a name for the encoded buffer
ohair@286 65 */
ohair@286 66 public UUEncoderStream(OutputStream out, String name) {
ohair@286 67 this(out, name, 644);
ohair@286 68 }
ohair@286 69
ohair@286 70 /**
ohair@286 71 * Create a UUencoder that encodes the specified input stream
ohair@286 72 * @param out the output stream
ohair@286 73 * @param name Specifies a name for the encoded buffer
ohair@286 74 * @param mode Specifies permission mode for the encoded buffer
ohair@286 75 */
ohair@286 76 public UUEncoderStream(OutputStream out, String name, int mode) {
ohair@286 77 super(out);
ohair@286 78 this.name = name;
ohair@286 79 this.mode = mode;
ohair@286 80 buffer = new byte[45];
ohair@286 81 }
ohair@286 82
ohair@286 83 /**
ohair@286 84 * Set up the buffer name and permission mode.
ohair@286 85 * This method has any effect only if it is invoked before
ohair@286 86 * you start writing into the output stream
ohair@286 87 */
ohair@286 88 public void setNameMode(String name, int mode) {
ohair@286 89 this.name = name;
ohair@286 90 this.mode = mode;
ohair@286 91 }
ohair@286 92
ohair@286 93 public void write(byte[] b, int off, int len) throws IOException {
ohair@286 94 for (int i = 0; i < len; i++)
ohair@286 95 write(b[off + i]);
ohair@286 96 }
ohair@286 97
ohair@286 98 public void write(byte[] data) throws IOException {
ohair@286 99 write(data, 0, data.length);
ohair@286 100 }
ohair@286 101
ohair@286 102 public void write(int c) throws IOException {
ohair@286 103 /* buffer up characters till we get a line's worth, then encode
ohair@286 104 * and write them out. Max number of characters allowed per
ohair@286 105 * line is 45.
ohair@286 106 */
ohair@286 107 buffer[bufsize++] = (byte)c;
ohair@286 108 if (bufsize == 45) {
ohair@286 109 writePrefix();
ohair@286 110 encode();
ohair@286 111 bufsize = 0;
ohair@286 112 }
ohair@286 113 }
ohair@286 114
ohair@286 115 public void flush() throws IOException {
ohair@286 116 if (bufsize > 0) { // If there's unencoded characters in the buffer
ohair@286 117 writePrefix();
ohair@286 118 encode(); // .. encode them
ohair@286 119 }
ohair@286 120 writeSuffix();
ohair@286 121 out.flush();
ohair@286 122 }
ohair@286 123
ohair@286 124 public void close() throws IOException {
ohair@286 125 flush();
ohair@286 126 out.close();
ohair@286 127 }
ohair@286 128
ohair@286 129 /**
ohair@286 130 * Write out the prefix: "begin <mode> <name>"
ohair@286 131 */
ohair@286 132 private void writePrefix() throws IOException {
ohair@286 133 if (!wrotePrefix) {
ohair@286 134 PrintStream ps = new PrintStream(out);
ohair@286 135 ps.println("begin " + mode + " " + name);
ohair@286 136 ps.flush();
ohair@286 137 wrotePrefix = true;
ohair@286 138 }
ohair@286 139 }
ohair@286 140
ohair@286 141 /**
ohair@286 142 * Write a single line containing space and the suffix line
ohair@286 143 * containing the single word "end" (terminated by a newline)
ohair@286 144 */
ohair@286 145 private void writeSuffix() throws IOException {
ohair@286 146 PrintStream ps = new PrintStream(out);
ohair@286 147 ps.println(" \nend");
ohair@286 148 ps.flush();
ohair@286 149 }
ohair@286 150
ohair@286 151 /**
ohair@286 152 * Encode a line.
ohair@286 153 * Start off with the character count, followed by the encoded atoms
ohair@286 154 * and terminate with LF. (or is it CRLF or the local line-terminator ?)
ohair@286 155 * Take three bytes and encodes them into 4 characters
ohair@286 156 * If bufsize if not a multiple of 3, the remaining bytes are filled
ohair@286 157 * with '1'. This insures that the last line won't end in spaces
ohair@286 158 * and potentiallly be truncated.
ohair@286 159 */
ohair@286 160 private void encode() throws IOException {
ohair@286 161 byte a, b, c;
ohair@286 162 int c1, c2, c3, c4;
ohair@286 163 int i = 0;
ohair@286 164
ohair@286 165 // Start off with the count of characters in the line
ohair@286 166 out.write((bufsize & 0x3f) + ' ');
ohair@286 167
ohair@286 168 while (i < bufsize) {
ohair@286 169 a = buffer[i++];
ohair@286 170 if (i < bufsize) {
ohair@286 171 b = buffer[i++];
ohair@286 172 if (i < bufsize)
ohair@286 173 c = buffer[i++];
ohair@286 174 else // default c to 1
ohair@286 175 c = 1;
ohair@286 176 }
ohair@286 177 else { // default b & c to 1
ohair@286 178 b = 1;
ohair@286 179 c = 1;
ohair@286 180 }
ohair@286 181
ohair@286 182 c1 = (a >>> 2) & 0x3f;
ohair@286 183 c2 = ((a << 4) & 0x30) | ((b >>> 4) & 0xf);
ohair@286 184 c3 = ((b << 2) & 0x3c) | ((c >>> 6) & 0x3);
ohair@286 185 c4 = c & 0x3f;
ohair@286 186 out.write(c1 + ' ');
ohair@286 187 out.write(c2 + ' ');
ohair@286 188 out.write(c3 + ' ');
ohair@286 189 out.write(c4 + ' ');
ohair@286 190 }
ohair@286 191 // Terminate with LF. (should it be CRLF or local line-terminator ?)
ohair@286 192 out.write('\n');
ohair@286 193 }
ohair@286 194
ohair@286 195 /**** begin TEST program *****
ohair@286 196 public static void main(String argv[]) throws Exception {
ohair@286 197 FileInputStream infile = new FileInputStream(argv[0]);
ohair@286 198 UUEncoderStream encoder = new UUEncoderStream(System.out);
ohair@286 199 int c;
ohair@286 200
ohair@286 201 while ((c = infile.read()) != -1)
ohair@286 202 encoder.write(c);
ohair@286 203 encoder.close();
ohair@286 204 }
ohair@286 205 **** end TEST program *****/
ohair@286 206 }

mercurial