1 /*
   2  * Copyright (c) 1997, 2013, 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  * @(#)QDecoderStream.java    1.5 02/03/27
  28  */
  29 
  30 
  31 
  32 package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
  33 
  34 import java.io.IOException;
  35 import java.io.InputStream;
  36 
  37 /**
  38  * This class implements a Q Decoder as defined in RFC 2047
  39  * for decoding MIME headers. It subclasses the QPDecoderStream class.
  40  *
  41  * @author John Mani
  42  */
  43 
  44 public class QDecoderStream extends QPDecoderStream {
  45 
  46     /**
  47      * Create a Q-decoder that decodes the specified input stream.
  48      * @param in        the input stream
  49      */
  50     public QDecoderStream(InputStream in) {
  51         super(in);
  52     }
  53 
  54     /**
  55      * Read the next decoded byte from this input stream. The byte
  56      * is returned as an <code>int</code> in the range <code>0</code>
  57      * to <code>255</code>. If no byte is available because the end of
  58      * the stream has been reached, the value <code>-1</code> is returned.
  59      * This method blocks until input data is available, the end of the
  60      * stream is detected, or an exception is thrown.
  61      *
  62      * @return     the next byte of data, or <code>-1</code> if the end of the
  63      *             stream is reached.
  64      * @exception  IOException  if an I/O error occurs.
  65      */

  66     public int read() throws IOException {
  67         int c = in.read();
  68 
  69         if (c == '_') // Return '_' as ' '
  70             return ' ';
  71         else if (c == '=') {
  72             // QP Encoded atom. Get the next two bytes ..
  73             ba[0] = (byte)in.read();
  74             ba[1] = (byte)in.read();
  75             // .. and decode them
  76             try {
  77                 return ASCIIUtility.parseInt(ba, 0, 2, 16);
  78             } catch (NumberFormatException nex) {
  79                 throw new IOException("Error in QP stream " + nex.getMessage());
  80             }
  81         } else
  82             return c;
  83     }
  84 }
--- EOF ---