Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
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 | /* |
aoqi@0 | 27 | * @(#)QPDecoderStream.java 1.9 02/04/02 |
aoqi@0 | 28 | */ |
aoqi@0 | 29 | |
aoqi@0 | 30 | |
aoqi@0 | 31 | |
aoqi@0 | 32 | package com.sun.xml.internal.messaging.saaj.packaging.mime.util; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import java.io.*; |
aoqi@0 | 35 | |
aoqi@0 | 36 | /** |
aoqi@0 | 37 | * This class implements a QP Decoder. It is implemented as |
aoqi@0 | 38 | * a FilterInputStream, so one can just wrap this class around |
aoqi@0 | 39 | * any input stream and read bytes from this filter. The decoding |
aoqi@0 | 40 | * is done as the bytes are read out. |
aoqi@0 | 41 | * |
aoqi@0 | 42 | * @author John Mani |
aoqi@0 | 43 | */ |
aoqi@0 | 44 | |
aoqi@0 | 45 | public class QPDecoderStream extends FilterInputStream { |
aoqi@0 | 46 | protected byte[] ba = new byte[2]; |
aoqi@0 | 47 | protected int spaces = 0; |
aoqi@0 | 48 | |
aoqi@0 | 49 | /** |
aoqi@0 | 50 | * Create a Quoted Printable decoder that decodes the specified |
aoqi@0 | 51 | * input stream. |
aoqi@0 | 52 | * @param in the input stream |
aoqi@0 | 53 | */ |
aoqi@0 | 54 | public QPDecoderStream(InputStream in) { |
aoqi@0 | 55 | super(new PushbackInputStream(in, 2)); // pushback of size=2 |
aoqi@0 | 56 | } |
aoqi@0 | 57 | |
aoqi@0 | 58 | /** |
aoqi@0 | 59 | * Read the next decoded byte from this input stream. The byte |
aoqi@0 | 60 | * is returned as an <code>int</code> in the range <code>0</code> |
aoqi@0 | 61 | * to <code>255</code>. If no byte is available because the end of |
aoqi@0 | 62 | * the stream has been reached, the value <code>-1</code> is returned. |
aoqi@0 | 63 | * This method blocks until input data is available, the end of the |
aoqi@0 | 64 | * stream is detected, or an exception is thrown. |
aoqi@0 | 65 | * |
aoqi@0 | 66 | * @return the next byte of data, or <code>-1</code> if the end of the |
aoqi@0 | 67 | * stream is reached. |
aoqi@0 | 68 | * @exception IOException if an I/O error occurs. |
aoqi@0 | 69 | */ |
aoqi@0 | 70 | public int read() throws IOException { |
aoqi@0 | 71 | if (spaces > 0) { |
aoqi@0 | 72 | // We have cached space characters, return one |
aoqi@0 | 73 | spaces--; |
aoqi@0 | 74 | return ' '; |
aoqi@0 | 75 | } |
aoqi@0 | 76 | |
aoqi@0 | 77 | int c = in.read(); |
aoqi@0 | 78 | |
aoqi@0 | 79 | if (c == ' ') { |
aoqi@0 | 80 | // Got space, keep reading till we get a non-space char |
aoqi@0 | 81 | while ((c = in.read()) == ' ') |
aoqi@0 | 82 | spaces++; |
aoqi@0 | 83 | |
aoqi@0 | 84 | if (c == '\r' || c == '\n' || c == -1) |
aoqi@0 | 85 | // If the non-space char is CR/LF/EOF, the spaces we got |
aoqi@0 | 86 | // so far is junk introduced during transport. Junk 'em. |
aoqi@0 | 87 | spaces = 0; |
aoqi@0 | 88 | else { |
aoqi@0 | 89 | // The non-space char is NOT CR/LF, the spaces are valid. |
aoqi@0 | 90 | ((PushbackInputStream)in).unread(c); |
aoqi@0 | 91 | c = ' '; |
aoqi@0 | 92 | } |
aoqi@0 | 93 | return c; // return either <SPACE> or <CR/LF> |
aoqi@0 | 94 | } |
aoqi@0 | 95 | else if (c == '=') { |
aoqi@0 | 96 | // QP Encoded atom. Decode the next two bytes |
aoqi@0 | 97 | int a = in.read(); |
aoqi@0 | 98 | |
aoqi@0 | 99 | if (a == '\n') { |
aoqi@0 | 100 | /* Hmm ... not really confirming QP encoding, but lets |
aoqi@0 | 101 | * allow this as a LF terminated encoded line .. and |
aoqi@0 | 102 | * consider this a soft linebreak and recurse to fetch |
aoqi@0 | 103 | * the next char. |
aoqi@0 | 104 | */ |
aoqi@0 | 105 | return read(); |
aoqi@0 | 106 | } else if (a == '\r') { |
aoqi@0 | 107 | // Expecting LF. This forms a soft linebreak to be ignored. |
aoqi@0 | 108 | int b = in.read(); |
aoqi@0 | 109 | if (b != '\n') |
aoqi@0 | 110 | /* Not really confirming QP encoding, but |
aoqi@0 | 111 | * lets allow this as well. |
aoqi@0 | 112 | */ |
aoqi@0 | 113 | ((PushbackInputStream)in).unread(b); |
aoqi@0 | 114 | return read(); |
aoqi@0 | 115 | } else if (a == -1) { |
aoqi@0 | 116 | // Not valid QP encoding, but we be nice and tolerant here ! |
aoqi@0 | 117 | return -1; |
aoqi@0 | 118 | } else { |
aoqi@0 | 119 | ba[0] = (byte)a; |
aoqi@0 | 120 | ba[1] = (byte)in.read(); |
aoqi@0 | 121 | try { |
aoqi@0 | 122 | return ASCIIUtility.parseInt(ba, 0, 2, 16); |
aoqi@0 | 123 | } catch (NumberFormatException nex) { |
aoqi@0 | 124 | /* |
aoqi@0 | 125 | System.err.println( |
aoqi@0 | 126 | "Illegal characters in QP encoded stream: " + |
aoqi@0 | 127 | ASCIIUtility.toString(ba, 0, 2) |
aoqi@0 | 128 | ); |
aoqi@0 | 129 | */ |
aoqi@0 | 130 | |
aoqi@0 | 131 | ((PushbackInputStream)in).unread(ba); |
aoqi@0 | 132 | return c; |
aoqi@0 | 133 | } |
aoqi@0 | 134 | } |
aoqi@0 | 135 | } |
aoqi@0 | 136 | return c; |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | /** |
aoqi@0 | 140 | * Reads up to <code>len</code> decoded bytes of data from this input stream |
aoqi@0 | 141 | * into an array of bytes. This method blocks until some input is |
aoqi@0 | 142 | * available. |
aoqi@0 | 143 | * <p> |
aoqi@0 | 144 | * |
aoqi@0 | 145 | * @param buf the buffer into which the data is read. |
aoqi@0 | 146 | * @param off the start offset of the data. |
aoqi@0 | 147 | * @param len the maximum number of bytes read. |
aoqi@0 | 148 | * @return the total number of bytes read into the buffer, or |
aoqi@0 | 149 | * <code>-1</code> if there is no more data because the end of |
aoqi@0 | 150 | * the stream has been reached. |
aoqi@0 | 151 | * @exception IOException if an I/O error occurs. |
aoqi@0 | 152 | */ |
aoqi@0 | 153 | public int read(byte[] buf, int off, int len) throws IOException { |
aoqi@0 | 154 | int i, c; |
aoqi@0 | 155 | for (i = 0; i < len; i++) { |
aoqi@0 | 156 | if ((c = read()) == -1) { |
aoqi@0 | 157 | if (i == 0) // At end of stream, so we should |
aoqi@0 | 158 | i = -1; // return -1 , NOT 0. |
aoqi@0 | 159 | break; |
aoqi@0 | 160 | } |
aoqi@0 | 161 | buf[off+i] = (byte)c; |
aoqi@0 | 162 | } |
aoqi@0 | 163 | return i; |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | /** |
aoqi@0 | 167 | * Tests if this input stream supports marks. Currently this class |
aoqi@0 | 168 | * does not support marks |
aoqi@0 | 169 | */ |
aoqi@0 | 170 | public boolean markSupported() { |
aoqi@0 | 171 | return false; |
aoqi@0 | 172 | } |
aoqi@0 | 173 | |
aoqi@0 | 174 | /** |
aoqi@0 | 175 | * Returns the number of bytes that can be read from this input |
aoqi@0 | 176 | * stream without blocking. The QP algorithm does not permit |
aoqi@0 | 177 | * a priori knowledge of the number of bytes after decoding, so |
aoqi@0 | 178 | * this method just invokes the <code>available</code> method |
aoqi@0 | 179 | * of the original input stream. |
aoqi@0 | 180 | */ |
aoqi@0 | 181 | public int available() throws IOException { |
aoqi@0 | 182 | // This is bogus ! We don't really know how much |
aoqi@0 | 183 | // bytes are available *after* decoding |
aoqi@0 | 184 | return in.available(); |
aoqi@0 | 185 | } |
aoqi@0 | 186 | |
aoqi@0 | 187 | /**** begin TEST program |
aoqi@0 | 188 | public static void main(String argv[]) throws Exception { |
aoqi@0 | 189 | FileInputStream infile = new FileInputStream(argv[0]); |
aoqi@0 | 190 | QPDecoderStream decoder = new QPDecoderStream(infile); |
aoqi@0 | 191 | int c; |
aoqi@0 | 192 | |
aoqi@0 | 193 | while ((c = decoder.read()) != -1) |
aoqi@0 | 194 | System.out.print((char)c); |
aoqi@0 | 195 | System.out.println(); |
aoqi@0 | 196 | } |
aoqi@0 | 197 | *** end TEST program ****/ |
aoqi@0 | 198 | } |