aoqi@0: /*
aoqi@0: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: /*
aoqi@0: * @(#)QPEncoderStream.java 1.6 02/03/27
aoqi@0: */
aoqi@0:
aoqi@0:
aoqi@0:
aoqi@0: package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
aoqi@0:
aoqi@0: import java.io.*;
aoqi@0:
aoqi@0: /**
aoqi@0: * This class implements a Quoted Printable Encoder. It is implemented as
aoqi@0: * a FilterOutputStream, so one can just wrap this class around
aoqi@0: * any output stream and write bytes into this filter. The Encoding
aoqi@0: * is done as the bytes are written out.
aoqi@0: *
aoqi@0: * @author John Mani
aoqi@0: */
aoqi@0:
aoqi@0: public class QPEncoderStream extends FilterOutputStream {
aoqi@0: private int count = 0; // number of bytes that have been output
aoqi@0: private int bytesPerLine; // number of bytes per line
aoqi@0: private boolean gotSpace = false;
aoqi@0: private boolean gotCR = false;
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a QP encoder that encodes the specified input stream
aoqi@0: * @param out the output stream
aoqi@0: * @param bytesPerLine the number of bytes per line. The encoder
aoqi@0: * inserts a CRLF sequence after this many number
aoqi@0: * of bytes.
aoqi@0: */
aoqi@0: public QPEncoderStream(OutputStream out, int bytesPerLine) {
aoqi@0: super(out);
aoqi@0: // Subtract 1 to account for the '=' in the soft-return
aoqi@0: // at the end of a line
aoqi@0: this.bytesPerLine = bytesPerLine - 1;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Create a QP encoder that encodes the specified input stream.
aoqi@0: * Inserts the CRLF sequence after outputting 76 bytes.
aoqi@0: * @param out the output stream
aoqi@0: */
aoqi@0: public QPEncoderStream(OutputStream out) {
aoqi@0: this(out, 76);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Encodes len
bytes from the specified
aoqi@0: * byte
array starting at offset off
to
aoqi@0: * this output stream.
aoqi@0: *
aoqi@0: * @param b the data.
aoqi@0: * @param off the start offset in the data.
aoqi@0: * @param len the number of bytes to write.
aoqi@0: * @exception IOException if an I/O error occurs.
aoqi@0: */
aoqi@0: public void write(byte[] b, int off, int len) throws IOException {
aoqi@0: for (int i = 0; i < len; i++)
aoqi@0: write(b[off + i]);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Encodes b.length
bytes to this output stream.
aoqi@0: * @param b the data to be written.
aoqi@0: * @exception IOException if an I/O error occurs.
aoqi@0: */
aoqi@0: public void write(byte[] b) throws IOException {
aoqi@0: write(b, 0, b.length);
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Encodes the specified byte
to this output stream.
aoqi@0: * @param c the byte
.
aoqi@0: * @exception IOException if an I/O error occurs.
aoqi@0: */
aoqi@0: public void write(int c) throws IOException {
aoqi@0: c = c & 0xff; // Turn off the MSB.
aoqi@0: if (gotSpace) { // previous character was
aoqi@0: if (c == '\r' || c == '\n')
aoqi@0: // if CR/LF, we need to encode the char
aoqi@0: output(' ', true);
aoqi@0: else // no encoding required, just output the char
aoqi@0: output(' ', false);
aoqi@0: gotSpace = false;
aoqi@0: }
aoqi@0:
aoqi@0: if (c == '\r') {
aoqi@0: gotCR = true;
aoqi@0: outputCRLF();
aoqi@0: } else {
aoqi@0: if (c == '\n') {
aoqi@0: if (gotCR)
aoqi@0: // This is a CRLF sequence, we already output the
aoqi@0: // corresponding CRLF when we got the CR, so ignore this
aoqi@0: ;
aoqi@0: else
aoqi@0: outputCRLF();
aoqi@0: } else if (c == ' ') {
aoqi@0: gotSpace = true;
aoqi@0: } else if (c < 040 || c >= 0177 || c == '=')
aoqi@0: // Encoding required.
aoqi@0: output(c, true);
aoqi@0: else // No encoding required
aoqi@0: output(c, false);
aoqi@0: // whatever it was, it wasn't a CR
aoqi@0: gotCR = false;
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Flushes this output stream and forces any buffered output bytes
aoqi@0: * to be encoded out to the stream.
aoqi@0: * @exception IOException if an I/O error occurs.
aoqi@0: */
aoqi@0: public void flush() throws IOException {
aoqi@0: out.flush();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Forces any buffered output bytes to be encoded out to the stream
aoqi@0: * and closes this output stream
aoqi@0: */
aoqi@0: public void close() throws IOException {
aoqi@0: out.close();
aoqi@0: }
aoqi@0:
aoqi@0: private void outputCRLF() throws IOException {
aoqi@0: out.write('\r');
aoqi@0: out.write('\n');
aoqi@0: count = 0;
aoqi@0: }
aoqi@0:
aoqi@0: // The encoding table
aoqi@0: private final static char hex[] = {
aoqi@0: '0','1', '2', '3', '4', '5', '6', '7',
aoqi@0: '8','9', 'A', 'B', 'C', 'D', 'E', 'F'
aoqi@0: };
aoqi@0:
aoqi@0: protected void output(int c, boolean encode) throws IOException {
aoqi@0: if (encode) {
aoqi@0: if ((count += 3) > bytesPerLine) {
aoqi@0: out.write('=');
aoqi@0: out.write('\r');
aoqi@0: out.write('\n');
aoqi@0: count = 3; // set the next line's length
aoqi@0: }
aoqi@0: out.write('=');
aoqi@0: out.write(hex[c >> 4]);
aoqi@0: out.write(hex[c & 0xf]);
aoqi@0: } else {
aoqi@0: if (++count > bytesPerLine) {
aoqi@0: out.write('=');
aoqi@0: out.write('\r');
aoqi@0: out.write('\n');
aoqi@0: count = 1; // set the next line's length
aoqi@0: }
aoqi@0: out.write(c);
aoqi@0: }
aoqi@0: }
aoqi@0:
aoqi@0: /**** begin TEST program ***
aoqi@0: public static void main(String argv[]) throws Exception {
aoqi@0: FileInputStream infile = new FileInputStream(argv[0]);
aoqi@0: QPEncoderStream encoder = new QPEncoderStream(System.out);
aoqi@0: int c;
aoqi@0:
aoqi@0: while ((c = infile.read()) != -1)
aoqi@0: encoder.write(c);
aoqi@0: encoder.close();
aoqi@0: }
aoqi@0: *** end TEST program ***/
aoqi@0: }