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