src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/QPEncoderStream.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 * @(#)QPEncoderStream.java 1.6 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 Quoted Printable Encoder. 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 QPEncoderStream extends FilterOutputStream {
ohair@286 46 private int count = 0; // number of bytes that have been output
ohair@286 47 private int bytesPerLine; // number of bytes per line
ohair@286 48 private boolean gotSpace = false;
ohair@286 49 private boolean gotCR = false;
ohair@286 50
ohair@286 51 /**
ohair@286 52 * Create a QP encoder that encodes the specified input stream
ohair@286 53 * @param out the output stream
ohair@286 54 * @param bytesPerLine the number of bytes per line. The encoder
ohair@286 55 * inserts a CRLF sequence after this many number
ohair@286 56 * of bytes.
ohair@286 57 */
ohair@286 58 public QPEncoderStream(OutputStream out, int bytesPerLine) {
ohair@286 59 super(out);
ohair@286 60 // Subtract 1 to account for the '=' in the soft-return
ohair@286 61 // at the end of a line
ohair@286 62 this.bytesPerLine = bytesPerLine - 1;
ohair@286 63 }
ohair@286 64
ohair@286 65 /**
ohair@286 66 * Create a QP encoder that encodes the specified input stream.
ohair@286 67 * Inserts the CRLF sequence after outputting 76 bytes.
ohair@286 68 * @param out the output stream
ohair@286 69 */
ohair@286 70 public QPEncoderStream(OutputStream out) {
ohair@286 71 this(out, 76);
ohair@286 72 }
ohair@286 73
ohair@286 74 /**
ohair@286 75 * Encodes <code>len</code> bytes from the specified
ohair@286 76 * <code>byte</code> array starting at offset <code>off</code> to
ohair@286 77 * this output stream.
ohair@286 78 *
ohair@286 79 * @param b the data.
ohair@286 80 * @param off the start offset in the data.
ohair@286 81 * @param len the number of bytes to write.
ohair@286 82 * @exception IOException if an I/O error occurs.
ohair@286 83 */
ohair@286 84 public void write(byte[] b, int off, int len) throws IOException {
ohair@286 85 for (int i = 0; i < len; i++)
ohair@286 86 write(b[off + i]);
ohair@286 87 }
ohair@286 88
ohair@286 89 /**
ohair@286 90 * Encodes <code>b.length</code> bytes to this output stream.
ohair@286 91 * @param b the data to be written.
ohair@286 92 * @exception IOException if an I/O error occurs.
ohair@286 93 */
ohair@286 94 public void write(byte[] b) throws IOException {
ohair@286 95 write(b, 0, b.length);
ohair@286 96 }
ohair@286 97
ohair@286 98 /**
ohair@286 99 * Encodes the specified <code>byte</code> to this output stream.
ohair@286 100 * @param c the <code>byte</code>.
ohair@286 101 * @exception IOException if an I/O error occurs.
ohair@286 102 */
ohair@286 103 public void write(int c) throws IOException {
ohair@286 104 c = c & 0xff; // Turn off the MSB.
ohair@286 105 if (gotSpace) { // previous character was <SPACE>
ohair@286 106 if (c == '\r' || c == '\n')
ohair@286 107 // if CR/LF, we need to encode the <SPACE> char
ohair@286 108 output(' ', true);
ohair@286 109 else // no encoding required, just output the char
ohair@286 110 output(' ', false);
ohair@286 111 gotSpace = false;
ohair@286 112 }
ohair@286 113
ohair@286 114 if (c == '\r') {
ohair@286 115 gotCR = true;
ohair@286 116 outputCRLF();
ohair@286 117 } else {
ohair@286 118 if (c == '\n') {
ohair@286 119 if (gotCR)
ohair@286 120 // This is a CRLF sequence, we already output the
ohair@286 121 // corresponding CRLF when we got the CR, so ignore this
ohair@286 122 ;
ohair@286 123 else
ohair@286 124 outputCRLF();
ohair@286 125 } else if (c == ' ') {
ohair@286 126 gotSpace = true;
ohair@286 127 } else if (c < 040 || c >= 0177 || c == '=')
ohair@286 128 // Encoding required.
ohair@286 129 output(c, true);
ohair@286 130 else // No encoding required
ohair@286 131 output(c, false);
ohair@286 132 // whatever it was, it wasn't a CR
ohair@286 133 gotCR = false;
ohair@286 134 }
ohair@286 135 }
ohair@286 136
ohair@286 137 /**
ohair@286 138 * Flushes this output stream and forces any buffered output bytes
ohair@286 139 * to be encoded out to the stream.
ohair@286 140 * @exception IOException if an I/O error occurs.
ohair@286 141 */
ohair@286 142 public void flush() throws IOException {
ohair@286 143 out.flush();
ohair@286 144 }
ohair@286 145
ohair@286 146 /**
ohair@286 147 * Forces any buffered output bytes to be encoded out to the stream
ohair@286 148 * and closes this output stream
ohair@286 149 */
ohair@286 150 public void close() throws IOException {
ohair@286 151 out.close();
ohair@286 152 }
ohair@286 153
ohair@286 154 private void outputCRLF() throws IOException {
ohair@286 155 out.write('\r');
ohair@286 156 out.write('\n');
ohair@286 157 count = 0;
ohair@286 158 }
ohair@286 159
ohair@286 160 // The encoding table
ohair@286 161 private final static char hex[] = {
ohair@286 162 '0','1', '2', '3', '4', '5', '6', '7',
ohair@286 163 '8','9', 'A', 'B', 'C', 'D', 'E', 'F'
ohair@286 164 };
ohair@286 165
ohair@286 166 protected void output(int c, boolean encode) throws IOException {
ohair@286 167 if (encode) {
ohair@286 168 if ((count += 3) > bytesPerLine) {
ohair@286 169 out.write('=');
ohair@286 170 out.write('\r');
ohair@286 171 out.write('\n');
ohair@286 172 count = 3; // set the next line's length
ohair@286 173 }
ohair@286 174 out.write('=');
ohair@286 175 out.write(hex[c >> 4]);
ohair@286 176 out.write(hex[c & 0xf]);
ohair@286 177 } else {
ohair@286 178 if (++count > bytesPerLine) {
ohair@286 179 out.write('=');
ohair@286 180 out.write('\r');
ohair@286 181 out.write('\n');
ohair@286 182 count = 1; // set the next line's length
ohair@286 183 }
ohair@286 184 out.write(c);
ohair@286 185 }
ohair@286 186 }
ohair@286 187
ohair@286 188 /**** begin TEST program ***
ohair@286 189 public static void main(String argv[]) throws Exception {
ohair@286 190 FileInputStream infile = new FileInputStream(argv[0]);
ohair@286 191 QPEncoderStream encoder = new QPEncoderStream(System.out);
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 *** end TEST program ***/
ohair@286 199 }

mercurial