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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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 * @(#)UUDecoderStream.java 1.8 02/07/08
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 UUDecoder. 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 * @author Bill Shannon
aoqi@0 44 */
aoqi@0 45
aoqi@0 46 public class UUDecoderStream extends FilterInputStream {
aoqi@0 47 private String name;
aoqi@0 48 private int mode;
aoqi@0 49
aoqi@0 50 private byte[] buffer; // cache of decoded bytes
aoqi@0 51 private int bufsize = 0; // size of the cache
aoqi@0 52 private int index = 0; // index into the cache
aoqi@0 53 private boolean gotPrefix = false;
aoqi@0 54 private boolean gotEnd = false;
aoqi@0 55 private LineInputStream lin;
aoqi@0 56
aoqi@0 57 /**
aoqi@0 58 * Create a UUdecoder that decodes the specified input stream
aoqi@0 59 * @param in the input stream
aoqi@0 60 */
aoqi@0 61 public UUDecoderStream(InputStream in) {
aoqi@0 62 super(in);
aoqi@0 63 lin = new LineInputStream(in);
aoqi@0 64 buffer = new byte[45]; // max decoded chars in a line = 45
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 /**
aoqi@0 68 * Read the next decoded byte from this input stream. The byte
aoqi@0 69 * is returned as an <code>int</code> in the range <code>0</code>
aoqi@0 70 * to <code>255</code>. If no byte is available because the end of
aoqi@0 71 * the stream has been reached, the value <code>-1</code> is returned.
aoqi@0 72 * This method blocks until input data is available, the end of the
aoqi@0 73 * stream is detected, or an exception is thrown.
aoqi@0 74 *
aoqi@0 75 * @return next byte of data, or <code>-1</code> if the end of
aoqi@0 76 * stream is reached.
aoqi@0 77 * @exception IOException if an I/O error occurs.
aoqi@0 78 * @see java.io.FilterInputStream#in
aoqi@0 79 */
aoqi@0 80
aoqi@0 81 public int read() throws IOException {
aoqi@0 82 if (index >= bufsize) {
aoqi@0 83 readPrefix();
aoqi@0 84 if (!decode())
aoqi@0 85 return -1;
aoqi@0 86 index = 0; // reset index into buffer
aoqi@0 87 }
aoqi@0 88 return buffer[index++] & 0xff; // return lower byte
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 public int read(byte[] buf, int off, int len) throws IOException {
aoqi@0 92 int i, c;
aoqi@0 93 for (i = 0; i < len; i++) {
aoqi@0 94 if ((c = read()) == -1) {
aoqi@0 95 if (i == 0) // At end of stream, so we should
aoqi@0 96 i = -1; // return -1, NOT 0.
aoqi@0 97 break;
aoqi@0 98 }
aoqi@0 99 buf[off+i] = (byte)c;
aoqi@0 100 }
aoqi@0 101 return i;
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 public boolean markSupported() {
aoqi@0 105 return false;
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 public int available() throws IOException {
aoqi@0 109 // This is only an estimate, since in.available()
aoqi@0 110 // might include CRLFs too ..
aoqi@0 111 return ((in.available() * 3)/4 + (bufsize-index));
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 /**
aoqi@0 115 * Get the "name" field from the prefix. This is meant to
aoqi@0 116 * be the pathname of the decoded file
aoqi@0 117 *
aoqi@0 118 * @return name of decoded file
aoqi@0 119 * @exception IOException if an I/O error occurs.
aoqi@0 120 */
aoqi@0 121 public String getName() throws IOException {
aoqi@0 122 readPrefix();
aoqi@0 123 return name;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 /**
aoqi@0 127 * Get the "mode" field from the prefix. This is the permission
aoqi@0 128 * mode of the source file.
aoqi@0 129 *
aoqi@0 130 * @return permission mode of source file
aoqi@0 131 * @exception IOException if an I/O error occurs.
aoqi@0 132 */
aoqi@0 133 public int getMode() throws IOException {
aoqi@0 134 readPrefix();
aoqi@0 135 return mode;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 /**
aoqi@0 139 * UUencoded streams start off with the line:
aoqi@0 140 * "begin <mode> <filename>"
aoqi@0 141 * Search for this prefix and gobble it up.
aoqi@0 142 */
aoqi@0 143 private void readPrefix() throws IOException {
aoqi@0 144 if (gotPrefix) // got the prefix
aoqi@0 145 return;
aoqi@0 146
aoqi@0 147 String s;
aoqi@0 148 for (;;) {
aoqi@0 149 // read till we get the prefix: "begin MODE FILENAME"
aoqi@0 150 s = lin.readLine(); // NOTE: readLine consumes CRLF pairs too
aoqi@0 151 if (s == null)
aoqi@0 152 throw new IOException("UUDecoder error: No Begin");
aoqi@0 153 if (s.regionMatches(true, 0, "begin", 0, 5)) {
aoqi@0 154 try {
aoqi@0 155 mode = Integer.parseInt(s.substring(6,9));
aoqi@0 156 } catch (NumberFormatException ex) {
aoqi@0 157 throw new IOException("UUDecoder error: " + ex.toString());
aoqi@0 158 }
aoqi@0 159 name = s.substring(10);
aoqi@0 160 gotPrefix = true;
aoqi@0 161 return;
aoqi@0 162 }
aoqi@0 163 }
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 private boolean decode() throws IOException {
aoqi@0 167
aoqi@0 168 if (gotEnd)
aoqi@0 169 return false;
aoqi@0 170 bufsize = 0;
aoqi@0 171 String line;
aoqi@0 172 do {
aoqi@0 173 line = lin.readLine();
aoqi@0 174
aoqi@0 175 /*
aoqi@0 176 * Improperly encoded data sometimes omits the zero length
aoqi@0 177 * line that starts with a space character, we detect the
aoqi@0 178 * following "end" line here.
aoqi@0 179 */
aoqi@0 180 if (line == null)
aoqi@0 181 throw new IOException("Missing End");
aoqi@0 182 if (line.regionMatches(true, 0, "end", 0, 3)) {
aoqi@0 183 gotEnd = true;
aoqi@0 184 return false;
aoqi@0 185 }
aoqi@0 186 } while (line.length() == 0);
aoqi@0 187 int count = line.charAt(0);
aoqi@0 188 if (count < ' ')
aoqi@0 189 throw new IOException("Buffer format error");
aoqi@0 190
aoqi@0 191 /*
aoqi@0 192 * The first character in a line is the number of original (not
aoqi@0 193 * the encoded atoms) characters in the line. Note that all the
aoqi@0 194 * code below has to handle the <SPACE> character that indicates
aoqi@0 195 * end of encoded stream.
aoqi@0 196 */
aoqi@0 197 count = (count - ' ') & 0x3f;
aoqi@0 198
aoqi@0 199 if (count == 0) {
aoqi@0 200 line = lin.readLine();
aoqi@0 201 if (line == null || !line.regionMatches(true, 0, "end", 0, 3))
aoqi@0 202 throw new IOException("Missing End");
aoqi@0 203 gotEnd = true;
aoqi@0 204 return false;
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 int need = ((count * 8)+5)/6;
aoqi@0 208 //System.out.println("count " + count + ", need " + need + ", len " + line.length());
aoqi@0 209 if (line.length() < need + 1)
aoqi@0 210 throw new IOException("Short buffer error");
aoqi@0 211
aoqi@0 212 int i = 1;
aoqi@0 213 byte a, b;
aoqi@0 214 /*
aoqi@0 215 * A correct uuencoder always encodes 3 characters at a time, even
aoqi@0 216 * if there aren't 3 characters left. But since some people out
aoqi@0 217 * there have broken uuencoders we handle the case where they
aoqi@0 218 * don't include these "unnecessary" characters.
aoqi@0 219 */
aoqi@0 220 while (bufsize < count) {
aoqi@0 221 // continue decoding until we get 'count' decoded chars
aoqi@0 222 a = (byte)((line.charAt(i++) - ' ') & 0x3f);
aoqi@0 223 b = (byte)((line.charAt(i++) - ' ') & 0x3f);
aoqi@0 224 buffer[bufsize++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3));
aoqi@0 225
aoqi@0 226 if (bufsize < count) {
aoqi@0 227 a = b;
aoqi@0 228 b = (byte)((line.charAt(i++) - ' ') & 0x3f);
aoqi@0 229 buffer[bufsize++] =
aoqi@0 230 (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf));
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 if (bufsize < count) {
aoqi@0 234 a = b;
aoqi@0 235 b = (byte)((line.charAt(i++) - ' ') & 0x3f);
aoqi@0 236 buffer[bufsize++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f));
aoqi@0 237 }
aoqi@0 238 }
aoqi@0 239 return true;
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 /*** begin TEST program *****
aoqi@0 243 public static void main(String argv[]) throws Exception {
aoqi@0 244 FileInputStream infile = new FileInputStream(argv[0]);
aoqi@0 245 UUDecoderStream decoder = new UUDecoderStream(infile);
aoqi@0 246 int c;
aoqi@0 247
aoqi@0 248 try {
aoqi@0 249 while ((c = decoder.read()) != -1)
aoqi@0 250 System.out.write(c);
aoqi@0 251 System.out.flush();
aoqi@0 252 } catch (Exception e) {
aoqi@0 253 e.printStackTrace();
aoqi@0 254 }
aoqi@0 255 }
aoqi@0 256 **** end TEST program ****/
aoqi@0 257 }

mercurial