src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/QPDecoderStream.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial