src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/QPDecoderStream.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/QPDecoderStream.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,198 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +/*
    1.30 + * @(#)QPDecoderStream.java   1.9 02/04/02
    1.31 + */
    1.32 +
    1.33 +
    1.34 +
    1.35 +package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
    1.36 +
    1.37 +import java.io.*;
    1.38 +
    1.39 +/**
    1.40 + * This class implements a QP Decoder. It is implemented as
    1.41 + * a FilterInputStream, so one can just wrap this class around
    1.42 + * any input stream and read bytes from this filter. The decoding
    1.43 + * is done as the bytes are read out.
    1.44 + *
    1.45 + * @author John Mani
    1.46 + */
    1.47 +
    1.48 +public class QPDecoderStream extends FilterInputStream {
    1.49 +    protected byte[] ba = new byte[2];
    1.50 +    protected int spaces = 0;
    1.51 +
    1.52 +    /**
    1.53 +     * Create a Quoted Printable decoder that decodes the specified
    1.54 +     * input stream.
    1.55 +     * @param in        the input stream
    1.56 +     */
    1.57 +    public QPDecoderStream(InputStream in) {
    1.58 +        super(new PushbackInputStream(in, 2)); // pushback of size=2
    1.59 +    }
    1.60 +
    1.61 +    /**
    1.62 +     * Read the next decoded byte from this input stream. The byte
    1.63 +     * is returned as an <code>int</code> in the range <code>0</code>
    1.64 +     * to <code>255</code>. If no byte is available because the end of
    1.65 +     * the stream has been reached, the value <code>-1</code> is returned.
    1.66 +     * This method blocks until input data is available, the end of the
    1.67 +     * stream is detected, or an exception is thrown.
    1.68 +     *
    1.69 +     * @return     the next byte of data, or <code>-1</code> if the end of the
    1.70 +     *             stream is reached.
    1.71 +     * @exception  IOException  if an I/O error occurs.
    1.72 +     */
    1.73 +    public int read() throws IOException {
    1.74 +        if (spaces > 0) {
    1.75 +            // We have cached space characters, return one
    1.76 +            spaces--;
    1.77 +            return ' ';
    1.78 +        }
    1.79 +
    1.80 +        int c = in.read();
    1.81 +
    1.82 +        if (c == ' ') {
    1.83 +            // Got space, keep reading till we get a non-space char
    1.84 +            while ((c = in.read()) == ' ')
    1.85 +                spaces++;
    1.86 +
    1.87 +            if (c == '\r' || c == '\n' || c == -1)
    1.88 +                // If the non-space char is CR/LF/EOF, the spaces we got
    1.89 +                // so far is junk introduced during transport. Junk 'em.
    1.90 +                spaces = 0;
    1.91 +            else {
    1.92 +                // The non-space char is NOT CR/LF, the spaces are valid.
    1.93 +                ((PushbackInputStream)in).unread(c);
    1.94 +                c = ' ';
    1.95 +            }
    1.96 +            return c; // return either <SPACE> or <CR/LF>
    1.97 +        }
    1.98 +        else if (c == '=') {
    1.99 +            // QP Encoded atom. Decode the next two bytes
   1.100 +            int a = in.read();
   1.101 +
   1.102 +            if (a == '\n') {
   1.103 +                /* Hmm ... not really confirming QP encoding, but lets
   1.104 +                 * allow this as a LF terminated encoded line .. and
   1.105 +                 * consider this a soft linebreak and recurse to fetch
   1.106 +                 * the next char.
   1.107 +                 */
   1.108 +                return read();
   1.109 +            } else if (a == '\r') {
   1.110 +                // Expecting LF. This forms a soft linebreak to be ignored.
   1.111 +                int b = in.read();
   1.112 +                if (b != '\n')
   1.113 +                    /* Not really confirming QP encoding, but
   1.114 +                     * lets allow this as well.
   1.115 +                     */
   1.116 +                    ((PushbackInputStream)in).unread(b);
   1.117 +                return read();
   1.118 +            } else if (a == -1) {
   1.119 +                // Not valid QP encoding, but we be nice and tolerant here !
   1.120 +                return -1;
   1.121 +            } else {
   1.122 +                ba[0] = (byte)a;
   1.123 +                ba[1] = (byte)in.read();
   1.124 +                try {
   1.125 +                    return ASCIIUtility.parseInt(ba, 0, 2, 16);
   1.126 +                } catch (NumberFormatException nex) {
   1.127 +                    /*
   1.128 +                    System.err.println(
   1.129 +                        "Illegal characters in QP encoded stream: " +
   1.130 +                        ASCIIUtility.toString(ba, 0, 2)
   1.131 +                    );
   1.132 +                    */
   1.133 +
   1.134 +                    ((PushbackInputStream)in).unread(ba);
   1.135 +                    return c;
   1.136 +                }
   1.137 +            }
   1.138 +        }
   1.139 +        return c;
   1.140 +    }
   1.141 +
   1.142 +    /**
   1.143 +     * Reads up to <code>len</code> decoded bytes of data from this input stream
   1.144 +     * into an array of bytes. This method blocks until some input is
   1.145 +     * available.
   1.146 +     * <p>
   1.147 +     *
   1.148 +     * @param      buf   the buffer into which the data is read.
   1.149 +     * @param      off   the start offset of the data.
   1.150 +     * @param      len   the maximum number of bytes read.
   1.151 +     * @return     the total number of bytes read into the buffer, or
   1.152 +     *             <code>-1</code> if there is no more data because the end of
   1.153 +     *             the stream has been reached.
   1.154 +     * @exception  IOException  if an I/O error occurs.
   1.155 +     */
   1.156 +    public int read(byte[] buf, int off, int len) throws IOException {
   1.157 +        int i, c;
   1.158 +        for (i = 0; i < len; i++) {
   1.159 +            if ((c = read()) == -1) {
   1.160 +                if (i == 0) // At end of stream, so we should
   1.161 +                    i = -1; // return -1 , NOT 0.
   1.162 +                break;
   1.163 +            }
   1.164 +            buf[off+i] = (byte)c;
   1.165 +        }
   1.166 +        return i;
   1.167 +    }
   1.168 +
   1.169 +    /**
   1.170 +     * Tests if this input stream supports marks. Currently this class
   1.171 +     * does not support marks
   1.172 +     */
   1.173 +    public boolean markSupported() {
   1.174 +        return false;
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     * Returns the number of bytes that can be read from this input
   1.179 +     * stream without blocking. The QP algorithm does not permit
   1.180 +     * a priori knowledge of the number of bytes after decoding, so
   1.181 +     * this method just invokes the <code>available</code> method
   1.182 +     * of the original input stream.
   1.183 +     */
   1.184 +    public int available() throws IOException {
   1.185 +        // This is bogus ! We don't really know how much
   1.186 +        // bytes are available *after* decoding
   1.187 +        return in.available();
   1.188 +    }
   1.189 +
   1.190 +    /**** begin TEST program
   1.191 +    public static void main(String argv[]) throws Exception {
   1.192 +        FileInputStream infile = new FileInputStream(argv[0]);
   1.193 +        QPDecoderStream decoder = new QPDecoderStream(infile);
   1.194 +        int c;
   1.195 +
   1.196 +        while ((c = decoder.read()) != -1)
   1.197 +            System.out.print((char)c);
   1.198 +        System.out.println();
   1.199 +    }
   1.200 +    *** end TEST program ****/
   1.201 +}

mercurial