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 * @(#)QPDecoderStream.java 1.9 02/04/02
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 QP Decoder. It is implemented as
325N/A * a FilterInputStream, so one can just wrap this class around
325N/A * any input stream and read bytes from this filter. The decoding
325N/A * is done as the bytes are read out.
325N/A *
325N/A * @author John Mani
325N/A */
325N/A
325N/Apublic class QPDecoderStream extends FilterInputStream {
325N/A protected byte[] ba = new byte[2];
325N/A protected int spaces = 0;
325N/A
325N/A /**
325N/A * Create a Quoted Printable decoder that decodes the specified
325N/A * input stream.
325N/A * @param in the input stream
325N/A */
325N/A public QPDecoderStream(InputStream in) {
325N/A super(new PushbackInputStream(in, 2)); // pushback of size=2
325N/A }
325N/A
325N/A /**
325N/A * Read the next decoded byte from this input stream. The byte
325N/A * is returned as an <code>int</code> in the range <code>0</code>
325N/A * to <code>255</code>. If no byte is available because the end of
325N/A * the stream has been reached, the value <code>-1</code> is returned.
325N/A * This method blocks until input data is available, the end of the
325N/A * stream is detected, or an exception is thrown.
325N/A *
325N/A * @return the next byte of data, or <code>-1</code> if the end of the
325N/A * stream is reached.
325N/A * @exception IOException if an I/O error occurs.
325N/A */
325N/A public int read() throws IOException {
325N/A if (spaces > 0) {
325N/A // We have cached space characters, return one
325N/A spaces--;
325N/A return ' ';
325N/A }
325N/A
325N/A int c = in.read();
325N/A
325N/A if (c == ' ') {
325N/A // Got space, keep reading till we get a non-space char
325N/A while ((c = in.read()) == ' ')
325N/A spaces++;
325N/A
325N/A if (c == '\r' || c == '\n' || c == -1)
325N/A // If the non-space char is CR/LF/EOF, the spaces we got
325N/A // so far is junk introduced during transport. Junk 'em.
325N/A spaces = 0;
325N/A else {
325N/A // The non-space char is NOT CR/LF, the spaces are valid.
325N/A ((PushbackInputStream)in).unread(c);
325N/A c = ' ';
325N/A }
325N/A return c; // return either <SPACE> or <CR/LF>
325N/A }
325N/A else if (c == '=') {
325N/A // QP Encoded atom. Decode the next two bytes
325N/A int a = in.read();
325N/A
325N/A if (a == '\n') {
325N/A /* Hmm ... not really confirming QP encoding, but lets
325N/A * allow this as a LF terminated encoded line .. and
325N/A * consider this a soft linebreak and recurse to fetch
325N/A * the next char.
325N/A */
325N/A return read();
325N/A } else if (a == '\r') {
325N/A // Expecting LF. This forms a soft linebreak to be ignored.
325N/A int b = in.read();
325N/A if (b != '\n')
325N/A /* Not really confirming QP encoding, but
325N/A * lets allow this as well.
325N/A */
325N/A ((PushbackInputStream)in).unread(b);
325N/A return read();
325N/A } else if (a == -1) {
325N/A // Not valid QP encoding, but we be nice and tolerant here !
325N/A return -1;
325N/A } else {
325N/A ba[0] = (byte)a;
325N/A ba[1] = (byte)in.read();
325N/A try {
325N/A return ASCIIUtility.parseInt(ba, 0, 2, 16);
325N/A } catch (NumberFormatException nex) {
325N/A /*
325N/A System.err.println(
325N/A "Illegal characters in QP encoded stream: " +
325N/A ASCIIUtility.toString(ba, 0, 2)
325N/A );
325N/A */
325N/A
325N/A ((PushbackInputStream)in).unread(ba);
325N/A return c;
325N/A }
325N/A }
325N/A }
325N/A return c;
325N/A }
325N/A
325N/A /**
325N/A * Reads up to <code>len</code> decoded bytes of data from this input stream
325N/A * into an array of bytes. This method blocks until some input is
325N/A * available.
325N/A * <p>
325N/A *
325N/A * @param buf the buffer into which the data is read.
325N/A * @param off the start offset of the data.
325N/A * @param len the maximum number of bytes read.
325N/A * @return the total number of bytes read into the buffer, or
325N/A * <code>-1</code> if there is no more data because the end of
325N/A * the stream has been reached.
325N/A * @exception IOException if an I/O error occurs.
325N/A */
325N/A public int read(byte[] buf, int off, int len) throws IOException {
325N/A int i, c;
325N/A for (i = 0; i < len; i++) {
325N/A if ((c = read()) == -1) {
325N/A if (i == 0) // At end of stream, so we should
325N/A i = -1; // return -1 , NOT 0.
325N/A break;
325N/A }
325N/A buf[off+i] = (byte)c;
325N/A }
325N/A return i;
325N/A }
325N/A
325N/A /**
325N/A * Tests if this input stream supports marks. Currently this class
325N/A * does not support marks
325N/A */
325N/A public boolean markSupported() {
325N/A return false;
325N/A }
325N/A
325N/A /**
325N/A * Returns the number of bytes that can be read from this input
325N/A * stream without blocking. The QP algorithm does not permit
325N/A * a priori knowledge of the number of bytes after decoding, so
325N/A * this method just invokes the <code>available</code> method
325N/A * of the original input stream.
325N/A */
325N/A public int available() throws IOException {
325N/A // This is bogus ! We don't really know how much
325N/A // bytes are available *after* decoding
325N/A return in.available();
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 QPDecoderStream decoder = new QPDecoderStream(infile);
325N/A int c;
325N/A
325N/A while ((c = decoder.read()) != -1)
325N/A System.out.print((char)c);
325N/A System.out.println();
325N/A }
325N/A *** end TEST program ****/
325N/A}