1 /*
   2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @(#)QPDecoderStream.java   1.9 02/04/02
  28  */
  29 
  30 
  31 
  32 package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
  33 
  34 import java.io.*;
  35 
  36 /**
  37  * This class implements a QP Decoder. It is implemented as
  38  * a FilterInputStream, so one can just wrap this class around
  39  * any input stream and read bytes from this filter. The decoding
  40  * is done as the bytes are read out.
  41  *
  42  * @author John Mani
  43  */
  44 
  45 public class QPDecoderStream extends FilterInputStream {
  46     protected byte[] ba = new byte[2];
  47     protected int spaces = 0;
  48 
  49     /**
  50      * Create a Quoted Printable decoder that decodes the specified
  51      * input stream.
  52      * @param in        the input stream
  53      */
  54     public QPDecoderStream(InputStream in) {
  55         super(new PushbackInputStream(in, 2)); // pushback of size=2
  56     }
  57 
  58     /**
  59      * Read the next decoded byte from this input stream. The byte
  60      * is returned as an <code>int</code> in the range <code>0</code>
  61      * to <code>255</code>. If no byte is available because the end of
  62      * the stream has been reached, the value <code>-1</code> is returned.
  63      * This method blocks until input data is available, the end of the
  64      * stream is detected, or an exception is thrown.
  65      *
  66      * @return     the next byte of data, or <code>-1</code> if the end of the
  67      *             stream is reached.
  68      * @exception  IOException  if an I/O error occurs.
  69      */
  70     public int read() throws IOException {
  71         if (spaces > 0) {
  72             // We have cached space characters, return one
  73             spaces--;
  74             return ' ';
  75         }
  76 
  77         int c = in.read();
  78 
  79         if (c == ' ') {
  80             // Got space, keep reading till we get a non-space char
  81             while ((c = in.read()) == ' ')
  82                 spaces++;
  83 
  84             if (c == '\r' || c == '\n' || c == -1)
  85                 // If the non-space char is CR/LF/EOF, the spaces we got
  86                 // so far is junk introduced during transport. Junk 'em.
  87                 spaces = 0;
  88             else {
  89                 // The non-space char is NOT CR/LF, the spaces are valid.
  90                 ((PushbackInputStream)in).unread(c);
  91                 c = ' ';
  92             }
  93             return c; // return either <SPACE> or <CR/LF>
  94         }
  95         else if (c == '=') {
  96             // QP Encoded atom. Decode the next two bytes
  97             int a = in.read();
  98 
  99             if (a == '\n') {
 100                 /* Hmm ... not really confirming QP encoding, but lets
 101                  * allow this as a LF terminated encoded line .. and
 102                  * consider this a soft linebreak and recurse to fetch
 103                  * the next char.
 104                  */
 105                 return read();
 106             } else if (a == '\r') {
 107                 // Expecting LF. This forms a soft linebreak to be ignored.
 108                 int b = in.read();
 109                 if (b != '\n')
 110                     /* Not really confirming QP encoding, but
 111                      * lets allow this as well.
 112                      */
 113                     ((PushbackInputStream)in).unread(b);
 114                 return read();
 115             } else if (a == -1) {
 116                 // Not valid QP encoding, but we be nice and tolerant here !
 117                 return -1;
 118             } else {
 119                 ba[0] = (byte)a;
 120                 ba[1] = (byte)in.read();
 121                 try {
 122                     return ASCIIUtility.parseInt(ba, 0, 2, 16);
 123                 } catch (NumberFormatException nex) {
 124                     /*
 125                     System.err.println(
 126                         "Illegal characters in QP encoded stream: " +
 127                         ASCIIUtility.toString(ba, 0, 2)
 128                     );
 129                     */
 130 
 131                     ((PushbackInputStream)in).unread(ba);
 132                     return c;
 133                 }
 134             }
 135         }
 136         return c;
 137     }
 138 
 139     /**
 140      * Reads up to <code>len</code> decoded bytes of data from this input stream
 141      * into an array of bytes. This method blocks until some input is
 142      * available.
 143      * <p>
 144      *
 145      * @param      buf   the buffer into which the data is read.
 146      * @param      off   the start offset of the data.
 147      * @param      len   the maximum number of bytes read.
 148      * @return     the total number of bytes read into the buffer, or
 149      *             <code>-1</code> if there is no more data because the end of
 150      *             the stream has been reached.
 151      * @exception  IOException  if an I/O error occurs.
 152      */
 153     public int read(byte[] buf, int off, int len) throws IOException {
 154         int i, c;
 155         for (i = 0; i < len; i++) {
 156             if ((c = read()) == -1) {
 157                 if (i == 0) // At end of stream, so we should
 158                     i = -1; // return -1 , NOT 0.
 159                 break;
 160             }
 161             buf[off+i] = (byte)c;
 162         }
 163         return i;
 164     }
 165 
 166     /**
 167      * Tests if this input stream supports marks. Currently this class
 168      * does not support marks
 169      */
 170     public boolean markSupported() {
 171         return false;
 172     }
 173 
 174     /**
 175      * Returns the number of bytes that can be read from this input
 176      * stream without blocking. The QP algorithm does not permit
 177      * a priori knowledge of the number of bytes after decoding, so
 178      * this method just invokes the <code>available</code> method
 179      * of the original input stream.
 180      */
 181     public int available() throws IOException {
 182         // This is bogus ! We don't really know how much
 183         // bytes are available *after* decoding
 184         return in.available();
 185     }
 186 
 187     /**** begin TEST program
 188     public static void main(String argv[]) throws Exception {
 189         FileInputStream infile = new FileInputStream(argv[0]);
 190         QPDecoderStream decoder = new QPDecoderStream(infile);
 191         int c;
 192 
 193         while ((c = decoder.read()) != -1)
 194             System.out.print((char)c);
 195         System.out.println();
 196     }
 197     *** end TEST program ****/
 198 }