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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/util/UUDecoderStream.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,257 @@
     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 +/*
    1.30 + * @(#)UUDecoderStream.java   1.8 02/07/08
    1.31 + */
    1.32 +
    1.33 +
    1.34 +
    1.35 +package com.sun.xml.internal.messaging.saaj.packaging.mime.util;
    1.36 +
    1.37 +import java.io.*;
    1.38 +
    1.39 +/**
    1.40 + * This class implements a UUDecoder. It is implemented as
    1.41 + * a FilterInputStream, so one can just wrap this class around
    1.42 + * any input stream and read bytes from this filter. The decoding
    1.43 + * is done as the bytes are read out.
    1.44 + *
    1.45 + * @author John Mani
    1.46 + * @author Bill Shannon
    1.47 + */
    1.48 +
    1.49 +public class UUDecoderStream extends FilterInputStream {
    1.50 +    private String name;
    1.51 +    private int mode;
    1.52 +
    1.53 +    private byte[] buffer;      // cache of decoded bytes
    1.54 +    private int bufsize = 0;    // size of the cache
    1.55 +    private int index = 0;      // index into the cache
    1.56 +    private boolean gotPrefix = false;
    1.57 +    private boolean gotEnd = false;
    1.58 +    private LineInputStream lin;
    1.59 +
    1.60 +    /**
    1.61 +     * Create a UUdecoder that decodes the specified input stream
    1.62 +     * @param in        the input stream
    1.63 +     */
    1.64 +    public UUDecoderStream(InputStream in) {
    1.65 +        super(in);
    1.66 +        lin = new LineInputStream(in);
    1.67 +        buffer = new byte[45]; // max decoded chars in a line = 45
    1.68 +    }
    1.69 +
    1.70 +    /**
    1.71 +     * Read the next decoded byte from this input stream. The byte
    1.72 +     * is returned as an <code>int</code> in the range <code>0</code>
    1.73 +     * to <code>255</code>. If no byte is available because the end of
    1.74 +     * the stream has been reached, the value <code>-1</code> is returned.
    1.75 +     * This method blocks until input data is available, the end of the
    1.76 +     * stream is detected, or an exception is thrown.
    1.77 +     *
    1.78 +     * @return     next byte of data, or <code>-1</code> if the end of
    1.79 +     *             stream is reached.
    1.80 +     * @exception  IOException  if an I/O error occurs.
    1.81 +     * @see        java.io.FilterInputStream#in
    1.82 +     */
    1.83 +
    1.84 +    public int read() throws IOException {
    1.85 +        if (index >= bufsize) {
    1.86 +            readPrefix();
    1.87 +            if (!decode())
    1.88 +                return -1;
    1.89 +            index = 0; // reset index into buffer
    1.90 +        }
    1.91 +        return buffer[index++] & 0xff; // return lower byte
    1.92 +    }
    1.93 +
    1.94 +    public int read(byte[] buf, int off, int len) throws IOException {
    1.95 +        int i, c;
    1.96 +        for (i = 0; i < len; i++) {
    1.97 +            if ((c = read()) == -1) {
    1.98 +                if (i == 0) // At end of stream, so we should
    1.99 +                    i = -1; // return -1, NOT 0.
   1.100 +                break;
   1.101 +            }
   1.102 +            buf[off+i] = (byte)c;
   1.103 +        }
   1.104 +        return i;
   1.105 +    }
   1.106 +
   1.107 +    public boolean markSupported() {
   1.108 +        return false;
   1.109 +    }
   1.110 +
   1.111 +    public int available() throws IOException {
   1.112 +         // This is only an estimate, since in.available()
   1.113 +         // might include CRLFs too ..
   1.114 +         return ((in.available() * 3)/4 + (bufsize-index));
   1.115 +    }
   1.116 +
   1.117 +    /**
   1.118 +     * Get the "name" field from the prefix. This is meant to
   1.119 +     * be the pathname of the decoded file
   1.120 +     *
   1.121 +     * @return     name of decoded file
   1.122 +     * @exception  IOException  if an I/O error occurs.
   1.123 +     */
   1.124 +    public String getName() throws IOException {
   1.125 +        readPrefix();
   1.126 +        return name;
   1.127 +    }
   1.128 +
   1.129 +    /**
   1.130 +     * Get the "mode" field from the prefix. This is the permission
   1.131 +     * mode of the source file.
   1.132 +     *
   1.133 +     * @return     permission mode of source file
   1.134 +     * @exception  IOException  if an I/O error occurs.
   1.135 +     */
   1.136 +    public int getMode() throws IOException {
   1.137 +        readPrefix();
   1.138 +        return mode;
   1.139 +    }
   1.140 +
   1.141 +    /**
   1.142 +     * UUencoded streams start off with the line:
   1.143 +     *  "begin <mode> <filename>"
   1.144 +     * Search for this prefix and gobble it up.
   1.145 +     */
   1.146 +    private void readPrefix() throws IOException {
   1.147 +        if (gotPrefix) // got the prefix
   1.148 +            return;
   1.149 +
   1.150 +        String s;
   1.151 +        for (;;) {
   1.152 +            // read till we get the prefix: "begin MODE FILENAME"
   1.153 +            s = lin.readLine(); // NOTE: readLine consumes CRLF pairs too
   1.154 +            if (s == null)
   1.155 +                throw new IOException("UUDecoder error: No Begin");
   1.156 +            if (s.regionMatches(true, 0, "begin", 0, 5)) {
   1.157 +                try {
   1.158 +                    mode = Integer.parseInt(s.substring(6,9));
   1.159 +                } catch (NumberFormatException ex) {
   1.160 +                    throw new IOException("UUDecoder error: " + ex.toString());
   1.161 +                }
   1.162 +                name = s.substring(10);
   1.163 +                gotPrefix = true;
   1.164 +                return;
   1.165 +            }
   1.166 +        }
   1.167 +    }
   1.168 +
   1.169 +    private boolean decode() throws IOException {
   1.170 +
   1.171 +        if (gotEnd)
   1.172 +            return false;
   1.173 +        bufsize = 0;
   1.174 +        String line;
   1.175 +        do {
   1.176 +            line = lin.readLine();
   1.177 +
   1.178 +            /*
   1.179 +             * Improperly encoded data sometimes omits the zero length
   1.180 +             * line that starts with a space character, we detect the
   1.181 +             * following "end" line here.
   1.182 +             */
   1.183 +            if (line == null)
   1.184 +                throw new IOException("Missing End");
   1.185 +            if (line.regionMatches(true, 0, "end", 0, 3)) {
   1.186 +                gotEnd = true;
   1.187 +                return false;
   1.188 +            }
   1.189 +        } while (line.length() == 0);
   1.190 +        int count = line.charAt(0);
   1.191 +        if (count < ' ')
   1.192 +            throw new IOException("Buffer format error");
   1.193 +
   1.194 +        /*
   1.195 +         * The first character in a line is the number of original (not
   1.196 +         *  the encoded atoms) characters in the line. Note that all the
   1.197 +         *  code below has to handle the <SPACE> character that indicates
   1.198 +         *  end of encoded stream.
   1.199 +         */
   1.200 +        count = (count - ' ') & 0x3f;
   1.201 +
   1.202 +        if (count == 0) {
   1.203 +            line = lin.readLine();
   1.204 +            if (line == null || !line.regionMatches(true, 0, "end", 0, 3))
   1.205 +                throw new IOException("Missing End");
   1.206 +            gotEnd = true;
   1.207 +            return false;
   1.208 +        }
   1.209 +
   1.210 +        int need = ((count * 8)+5)/6;
   1.211 +//System.out.println("count " + count + ", need " + need + ", len " + line.length());
   1.212 +        if (line.length() < need + 1)
   1.213 +            throw new IOException("Short buffer error");
   1.214 +
   1.215 +        int i = 1;
   1.216 +        byte a, b;
   1.217 +        /*
   1.218 +         * A correct uuencoder always encodes 3 characters at a time, even
   1.219 +         * if there aren't 3 characters left.  But since some people out
   1.220 +         * there have broken uuencoders we handle the case where they
   1.221 +         * don't include these "unnecessary" characters.
   1.222 +         */
   1.223 +        while (bufsize < count) {
   1.224 +            // continue decoding until we get 'count' decoded chars
   1.225 +            a = (byte)((line.charAt(i++) - ' ') & 0x3f);
   1.226 +            b = (byte)((line.charAt(i++) - ' ') & 0x3f);
   1.227 +            buffer[bufsize++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3));
   1.228 +
   1.229 +            if (bufsize < count) {
   1.230 +                a = b;
   1.231 +                b = (byte)((line.charAt(i++) - ' ') & 0x3f);
   1.232 +                buffer[bufsize++] =
   1.233 +                                (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf));
   1.234 +            }
   1.235 +
   1.236 +            if (bufsize < count) {
   1.237 +                a = b;
   1.238 +                b = (byte)((line.charAt(i++) - ' ') & 0x3f);
   1.239 +                buffer[bufsize++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f));
   1.240 +            }
   1.241 +        }
   1.242 +        return true;
   1.243 +    }
   1.244 +
   1.245 +    /*** begin TEST program *****
   1.246 +    public static void main(String argv[]) throws Exception {
   1.247 +        FileInputStream infile = new FileInputStream(argv[0]);
   1.248 +        UUDecoderStream decoder = new UUDecoderStream(infile);
   1.249 +        int c;
   1.250 +
   1.251 +        try {
   1.252 +            while ((c = decoder.read()) != -1)
   1.253 +                System.out.write(c);
   1.254 +            System.out.flush();
   1.255 +        } catch (Exception e) {
   1.256 +            e.printStackTrace();
   1.257 +        }
   1.258 +    }
   1.259 +    **** end TEST program ****/
   1.260 +}

mercurial