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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial