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