1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/QPDecoderStream.java Tue Apr 09 14:51:13 2013 +0100 1.3 @@ -0,0 +1,207 @@ 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 +/* FROM mail.jar */ 1.30 +package com.sun.xml.internal.org.jvnet.mimepull; 1.31 + 1.32 +import java.io.*; 1.33 + 1.34 +/** 1.35 + * This class implements a QP Decoder. It is implemented as 1.36 + * a FilterInputStream, so one can just wrap this class around 1.37 + * any input stream and read bytes from this filter. The decoding 1.38 + * is done as the bytes are read out. 1.39 + * 1.40 + * @author John Mani 1.41 + */ 1.42 + 1.43 +final class QPDecoderStream extends FilterInputStream { 1.44 + private byte[] ba = new byte[2]; 1.45 + private int spaces = 0; 1.46 + 1.47 + /** 1.48 + * Create a Quoted Printable decoder that decodes the specified 1.49 + * input stream. 1.50 + * @param in the input stream 1.51 + */ 1.52 + public QPDecoderStream(InputStream in) { 1.53 + super(new PushbackInputStream(in, 2)); // pushback of size=2 1.54 + } 1.55 + 1.56 + /** 1.57 + * Read the next decoded byte from this input stream. The byte 1.58 + * is returned as an <code>int</code> in the range <code>0</code> 1.59 + * to <code>255</code>. If no byte is available because the end of 1.60 + * the stream has been reached, the value <code>-1</code> is returned. 1.61 + * This method blocks until input data is available, the end of the 1.62 + * stream is detected, or an exception is thrown. 1.63 + * 1.64 + * @return the next byte of data, or <code>-1</code> if the end of the 1.65 + * stream is reached. 1.66 + * @exception IOException if an I/O error occurs. 1.67 + */ 1.68 + @Override 1.69 + public int read() throws IOException { 1.70 + if (spaces > 0) { 1.71 + // We have cached space characters, return one 1.72 + spaces--; 1.73 + return ' '; 1.74 + } 1.75 + 1.76 + int c = in.read(); 1.77 + 1.78 + if (c == ' ') { 1.79 + // Got space, keep reading till we get a non-space char 1.80 + while ((c = in.read()) == ' ') { 1.81 + spaces++; 1.82 + } 1.83 + 1.84 + if (c == '\r' || c == '\n' || c == -1) { 1.85 + spaces = 0; 1.86 + } else { 1.87 + // The non-space char is NOT CR/LF, the spaces are valid. 1.88 + ((PushbackInputStream)in).unread(c); 1.89 + c = ' '; 1.90 + } 1.91 + return c; // return either <SPACE> or <CR/LF> 1.92 + } 1.93 + else if (c == '=') { 1.94 + // QP Encoded atom. Decode the next two bytes 1.95 + int a = in.read(); 1.96 + 1.97 + if (a == '\n') { 1.98 + /* Hmm ... not really confirming QP encoding, but lets 1.99 + * allow this as a LF terminated encoded line .. and 1.100 + * consider this a soft linebreak and recurse to fetch 1.101 + * the next char. 1.102 + */ 1.103 + return read(); 1.104 + } else if (a == '\r') { 1.105 + // Expecting LF. This forms a soft linebreak to be ignored. 1.106 + int b = in.read(); 1.107 + if (b != '\n') { 1.108 + ((PushbackInputStream)in).unread(b); 1.109 + } 1.110 + return read(); 1.111 + } else if (a == -1) { 1.112 + // Not valid QP encoding, but we be nice and tolerant here ! 1.113 + return -1; 1.114 + } else { 1.115 + ba[0] = (byte)a; 1.116 + ba[1] = (byte)in.read(); 1.117 + try { 1.118 + return ASCIIUtility.parseInt(ba, 0, 2, 16); 1.119 + } catch (NumberFormatException nex) { 1.120 + /* 1.121 + System.err.println( 1.122 + "Illegal characters in QP encoded stream: " + 1.123 + ASCIIUtility.toString(ba, 0, 2) 1.124 + ); 1.125 + */ 1.126 + 1.127 + ((PushbackInputStream)in).unread(ba); 1.128 + return c; 1.129 + } 1.130 + } 1.131 + } 1.132 + return c; 1.133 + } 1.134 + 1.135 + /** 1.136 + * Reads up to <code>len</code> decoded bytes of data from this input stream 1.137 + * into an array of bytes. This method blocks until some input is 1.138 + * available. 1.139 + * <p> 1.140 + * 1.141 + * @param buf the buffer into which the data is read. 1.142 + * @param off the start offset of the data. 1.143 + * @param len the maximum number of bytes read. 1.144 + * @return the total number of bytes read into the buffer, or 1.145 + * <code>-1</code> if there is no more data because the end of 1.146 + * the stream has been reached. 1.147 + * @exception IOException if an I/O error occurs. 1.148 + */ 1.149 + @Override 1.150 + public int read(byte[] buf, int off, int len) throws IOException { 1.151 + int i, c; 1.152 + for (i = 0; i < len; i++) { 1.153 + if ((c = read()) == -1) { 1.154 + if (i == 0) { 1.155 + i = -1; // return -1 , NOT 0. 1.156 + } 1.157 + break; 1.158 + } 1.159 + buf[off+i] = (byte)c; 1.160 + } 1.161 + return i; 1.162 + } 1.163 + 1.164 + /** 1.165 + * Skips over and discards n bytes of data from this stream. 1.166 + */ 1.167 + @Override 1.168 + public long skip(long n) throws IOException { 1.169 + long skipped = 0; 1.170 + while (n-- > 0 && read() >= 0) { 1.171 + skipped++; 1.172 + } 1.173 + return skipped; 1.174 + } 1.175 + 1.176 + /** 1.177 + * Tests if this input stream supports marks. Currently this class 1.178 + * does not support marks 1.179 + */ 1.180 + @Override 1.181 + public boolean markSupported() { 1.182 + return false; 1.183 + } 1.184 + 1.185 + /** 1.186 + * Returns the number of bytes that can be read from this input 1.187 + * stream without blocking. The QP algorithm does not permit 1.188 + * a priori knowledge of the number of bytes after decoding, so 1.189 + * this method just invokes the <code>available</code> method 1.190 + * of the original input stream. 1.191 + */ 1.192 + @Override 1.193 + public int available() throws IOException { 1.194 + // This is bogus ! We don't really know how much 1.195 + // bytes are available *after* decoding 1.196 + return in.available(); 1.197 + } 1.198 + 1.199 + /**** begin TEST program 1.200 + public static void main(String argv[]) throws Exception { 1.201 + FileInputStream infile = new FileInputStream(argv[0]); 1.202 + QPDecoderStream decoder = new QPDecoderStream(infile); 1.203 + int c; 1.204 + 1.205 + while ((c = decoder.read()) != -1) 1.206 + System.out.print((char)c); 1.207 + System.out.println(); 1.208 + } 1.209 + *** end TEST program ****/ 1.210 +}