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: /* FROM mail.jar */ aoqi@0: package com.sun.xml.internal.org.jvnet.mimepull; aoqi@0: aoqi@0: import java.io.*; aoqi@0: aoqi@0: /** aoqi@0: * This class implements a QP Decoder. It is implemented as aoqi@0: * a FilterInputStream, so one can just wrap this class around aoqi@0: * any input stream and read bytes from this filter. The decoding aoqi@0: * is done as the bytes are read out. aoqi@0: * aoqi@0: * @author John Mani aoqi@0: */ aoqi@0: aoqi@0: final class QPDecoderStream extends FilterInputStream { aoqi@0: private byte[] ba = new byte[2]; aoqi@0: private int spaces = 0; aoqi@0: aoqi@0: /** aoqi@0: * Create a Quoted Printable decoder that decodes the specified aoqi@0: * input stream. aoqi@0: * @param in the input stream aoqi@0: */ aoqi@0: public QPDecoderStream(InputStream in) { aoqi@0: super(new PushbackInputStream(in, 2)); // pushback of size=2 aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Read the next decoded byte from this input stream. The byte aoqi@0: * is returned as an int in the range 0 aoqi@0: * to 255. If no byte is available because the end of aoqi@0: * the stream has been reached, the value -1 is returned. aoqi@0: * This method blocks until input data is available, the end of the aoqi@0: * stream is detected, or an exception is thrown. aoqi@0: * aoqi@0: * @return the next byte of data, or -1 if the end of the aoqi@0: * stream is reached. aoqi@0: * @exception IOException if an I/O error occurs. aoqi@0: */ aoqi@0: @Override aoqi@0: public int read() throws IOException { aoqi@0: if (spaces > 0) { aoqi@0: // We have cached space characters, return one aoqi@0: spaces--; aoqi@0: return ' '; aoqi@0: } aoqi@0: aoqi@0: int c = in.read(); aoqi@0: aoqi@0: if (c == ' ') { aoqi@0: // Got space, keep reading till we get a non-space char aoqi@0: while ((c = in.read()) == ' ') { aoqi@0: spaces++; aoqi@0: } aoqi@0: aoqi@0: if (c == '\r' || c == '\n' || c == -1) { aoqi@0: spaces = 0; aoqi@0: } else { aoqi@0: // The non-space char is NOT CR/LF, the spaces are valid. aoqi@0: ((PushbackInputStream)in).unread(c); aoqi@0: c = ' '; aoqi@0: } aoqi@0: return c; // return either or aoqi@0: } aoqi@0: else if (c == '=') { aoqi@0: // QP Encoded atom. Decode the next two bytes aoqi@0: int a = in.read(); aoqi@0: aoqi@0: if (a == '\n') { aoqi@0: /* Hmm ... not really confirming QP encoding, but lets aoqi@0: * allow this as a LF terminated encoded line .. and aoqi@0: * consider this a soft linebreak and recurse to fetch aoqi@0: * the next char. aoqi@0: */ aoqi@0: return read(); aoqi@0: } else if (a == '\r') { aoqi@0: // Expecting LF. This forms a soft linebreak to be ignored. aoqi@0: int b = in.read(); aoqi@0: if (b != '\n') { aoqi@0: ((PushbackInputStream)in).unread(b); aoqi@0: } aoqi@0: return read(); aoqi@0: } else if (a == -1) { aoqi@0: // Not valid QP encoding, but we be nice and tolerant here ! aoqi@0: return -1; aoqi@0: } else { aoqi@0: ba[0] = (byte)a; aoqi@0: ba[1] = (byte)in.read(); aoqi@0: try { aoqi@0: return ASCIIUtility.parseInt(ba, 0, 2, 16); aoqi@0: } catch (NumberFormatException nex) { aoqi@0: /* aoqi@0: System.err.println( aoqi@0: "Illegal characters in QP encoded stream: " + aoqi@0: ASCIIUtility.toString(ba, 0, 2) aoqi@0: ); aoqi@0: */ aoqi@0: aoqi@0: ((PushbackInputStream)in).unread(ba); aoqi@0: return c; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: return c; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Reads up to len decoded bytes of data from this input stream aoqi@0: * into an array of bytes. This method blocks until some input is aoqi@0: * available. aoqi@0: *

aoqi@0: * aoqi@0: * @param buf the buffer into which the data is read. aoqi@0: * @param off the start offset of the data. aoqi@0: * @param len the maximum number of bytes read. aoqi@0: * @return the total number of bytes read into the buffer, or aoqi@0: * -1 if there is no more data because the end of aoqi@0: * the stream has been reached. aoqi@0: * @exception IOException if an I/O error occurs. aoqi@0: */ aoqi@0: @Override aoqi@0: public int read(byte[] buf, int off, int len) throws IOException { aoqi@0: int i, c; aoqi@0: for (i = 0; i < len; i++) { aoqi@0: if ((c = read()) == -1) { aoqi@0: if (i == 0) { aoqi@0: i = -1; // return -1 , NOT 0. aoqi@0: } aoqi@0: break; aoqi@0: } aoqi@0: buf[off+i] = (byte)c; aoqi@0: } aoqi@0: return i; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Skips over and discards n bytes of data from this stream. aoqi@0: */ aoqi@0: @Override aoqi@0: public long skip(long n) throws IOException { aoqi@0: long skipped = 0; aoqi@0: while (n-- > 0 && read() >= 0) { aoqi@0: skipped++; aoqi@0: } aoqi@0: return skipped; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Tests if this input stream supports marks. Currently this class aoqi@0: * does not support marks aoqi@0: */ aoqi@0: @Override aoqi@0: public boolean markSupported() { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the number of bytes that can be read from this input aoqi@0: * stream without blocking. The QP algorithm does not permit aoqi@0: * a priori knowledge of the number of bytes after decoding, so aoqi@0: * this method just invokes the available method aoqi@0: * of the original input stream. aoqi@0: */ aoqi@0: @Override aoqi@0: public int available() throws IOException { aoqi@0: // This is bogus ! We don't really know how much aoqi@0: // bytes are available *after* decoding aoqi@0: return in.available(); 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: QPDecoderStream decoder = new QPDecoderStream(infile); aoqi@0: int c; aoqi@0: aoqi@0: while ((c = decoder.read()) != -1) aoqi@0: System.out.print((char)c); aoqi@0: System.out.println(); aoqi@0: } aoqi@0: *** end TEST program ****/ aoqi@0: }