rt/emul/mini/src/main/java/java/util/zip/InflaterInputStream.java
changeset 772 d382dacfd73f
parent 609 48ef38e9677e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/mini/src/main/java/java/util/zip/InflaterInputStream.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,288 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2006, 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 +package java.util.zip;
    1.30 +
    1.31 +import java.io.FilterInputStream;
    1.32 +import java.io.InputStream;
    1.33 +import java.io.IOException;
    1.34 +import java.io.EOFException;
    1.35 +
    1.36 +/**
    1.37 + * This class implements a stream filter for uncompressing data in the
    1.38 + * "deflate" compression format. It is also used as the basis for other
    1.39 + * decompression filters, such as GZIPInputStream.
    1.40 + *
    1.41 + * @see         Inflater
    1.42 + * @author      David Connelly
    1.43 + */
    1.44 +public
    1.45 +class InflaterInputStream extends FilterInputStream {
    1.46 +    /**
    1.47 +     * Decompressor for this stream.
    1.48 +     */
    1.49 +    protected Inflater inf;
    1.50 +
    1.51 +    /**
    1.52 +     * Input buffer for decompression.
    1.53 +     */
    1.54 +    protected byte[] buf;
    1.55 +
    1.56 +    /**
    1.57 +     * Length of input buffer.
    1.58 +     */
    1.59 +    protected int len;
    1.60 +
    1.61 +    private boolean closed = false;
    1.62 +    // this flag is set to true after EOF has reached
    1.63 +    private boolean reachEOF = false;
    1.64 +
    1.65 +    /**
    1.66 +     * Check to make sure that this stream has not been closed
    1.67 +     */
    1.68 +    private void ensureOpen() throws IOException {
    1.69 +        if (closed) {
    1.70 +            throw new IOException("Stream closed");
    1.71 +        }
    1.72 +    }
    1.73 +
    1.74 +
    1.75 +    /**
    1.76 +     * Creates a new input stream with the specified decompressor and
    1.77 +     * buffer size.
    1.78 +     * @param in the input stream
    1.79 +     * @param inf the decompressor ("inflater")
    1.80 +     * @param size the input buffer size
    1.81 +     * @exception IllegalArgumentException if size is <= 0
    1.82 +     */
    1.83 +    public InflaterInputStream(InputStream in, Inflater inf, int size) {
    1.84 +        super(in);
    1.85 +        if (in == null || inf == null) {
    1.86 +            throw new NullPointerException();
    1.87 +        } else if (size <= 0) {
    1.88 +            throw new IllegalArgumentException("buffer size <= 0");
    1.89 +        }
    1.90 +        this.inf = inf;
    1.91 +        buf = new byte[size];
    1.92 +    }
    1.93 +
    1.94 +    /**
    1.95 +     * Creates a new input stream with the specified decompressor and a
    1.96 +     * default buffer size.
    1.97 +     * @param in the input stream
    1.98 +     * @param inf the decompressor ("inflater")
    1.99 +     */
   1.100 +    public InflaterInputStream(InputStream in, Inflater inf) {
   1.101 +        this(in, inf, 512);
   1.102 +    }
   1.103 +
   1.104 +    boolean usesDefaultInflater = false;
   1.105 +
   1.106 +    /**
   1.107 +     * Creates a new input stream with a default decompressor and buffer size.
   1.108 +     * @param in the input stream
   1.109 +     */
   1.110 +    public InflaterInputStream(InputStream in) {
   1.111 +        this(in, new Inflater());
   1.112 +        usesDefaultInflater = true;
   1.113 +    }
   1.114 +
   1.115 +    private byte[] singleByteBuf = new byte[1];
   1.116 +
   1.117 +    /**
   1.118 +     * Reads a byte of uncompressed data. This method will block until
   1.119 +     * enough input is available for decompression.
   1.120 +     * @return the byte read, or -1 if end of compressed input is reached
   1.121 +     * @exception IOException if an I/O error has occurred
   1.122 +     */
   1.123 +    public int read() throws IOException {
   1.124 +        ensureOpen();
   1.125 +        return read(singleByteBuf, 0, 1) == -1 ? -1 : singleByteBuf[0] & 0xff;
   1.126 +    }
   1.127 +
   1.128 +    /**
   1.129 +     * Reads uncompressed data into an array of bytes. If <code>len</code> is not
   1.130 +     * zero, the method will block until some input can be decompressed; otherwise,
   1.131 +     * no bytes are read and <code>0</code> is returned.
   1.132 +     * @param b the buffer into which the data is read
   1.133 +     * @param off the start offset in the destination array <code>b</code>
   1.134 +     * @param len the maximum number of bytes read
   1.135 +     * @return the actual number of bytes read, or -1 if the end of the
   1.136 +     *         compressed input is reached or a preset dictionary is needed
   1.137 +     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
   1.138 +     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
   1.139 +     * <code>len</code> is negative, or <code>len</code> is greater than
   1.140 +     * <code>b.length - off</code>
   1.141 +     * @exception ZipException if a ZIP format error has occurred
   1.142 +     * @exception IOException if an I/O error has occurred
   1.143 +     */
   1.144 +    public int read(byte[] b, int off, int len) throws IOException {
   1.145 +        ensureOpen();
   1.146 +        if (b == null) {
   1.147 +            throw new NullPointerException();
   1.148 +        } else if (off < 0 || len < 0 || len > b.length - off) {
   1.149 +            throw new IndexOutOfBoundsException();
   1.150 +        } else if (len == 0) {
   1.151 +            return 0;
   1.152 +        }
   1.153 +        try {
   1.154 +            int n;
   1.155 +            while ((n = inf.inflate(b, off, len)) == 0) {
   1.156 +                if (inf.finished() || inf.needsDictionary()) {
   1.157 +                    reachEOF = true;
   1.158 +                    return -1;
   1.159 +                }
   1.160 +                if (inf.needsInput()) {
   1.161 +                    fill();
   1.162 +                }
   1.163 +            }
   1.164 +            return n;
   1.165 +        } catch (DataFormatException e) {
   1.166 +            String s = e.getMessage();
   1.167 +            throw new ZipException(s != null ? s : "Invalid ZLIB data format");
   1.168 +        }
   1.169 +    }
   1.170 +
   1.171 +    /**
   1.172 +     * Returns 0 after EOF has been reached, otherwise always return 1.
   1.173 +     * <p>
   1.174 +     * Programs should not count on this method to return the actual number
   1.175 +     * of bytes that could be read without blocking.
   1.176 +     *
   1.177 +     * @return     1 before EOF and 0 after EOF.
   1.178 +     * @exception  IOException  if an I/O error occurs.
   1.179 +     *
   1.180 +     */
   1.181 +    public int available() throws IOException {
   1.182 +        ensureOpen();
   1.183 +        if (reachEOF) {
   1.184 +            return 0;
   1.185 +        } else {
   1.186 +            return 1;
   1.187 +        }
   1.188 +    }
   1.189 +
   1.190 +    private byte[] b = new byte[512];
   1.191 +
   1.192 +    /**
   1.193 +     * Skips specified number of bytes of uncompressed data.
   1.194 +     * @param n the number of bytes to skip
   1.195 +     * @return the actual number of bytes skipped.
   1.196 +     * @exception IOException if an I/O error has occurred
   1.197 +     * @exception IllegalArgumentException if n < 0
   1.198 +     */
   1.199 +    public long skip(long n) throws IOException {
   1.200 +        if (n < 0) {
   1.201 +            throw new IllegalArgumentException("negative skip length");
   1.202 +        }
   1.203 +        ensureOpen();
   1.204 +        int max = (int)Math.min(n, Integer.MAX_VALUE);
   1.205 +        int total = 0;
   1.206 +        while (total < max) {
   1.207 +            int len = max - total;
   1.208 +            if (len > b.length) {
   1.209 +                len = b.length;
   1.210 +            }
   1.211 +            len = read(b, 0, len);
   1.212 +            if (len == -1) {
   1.213 +                reachEOF = true;
   1.214 +                break;
   1.215 +            }
   1.216 +            total += len;
   1.217 +        }
   1.218 +        return total;
   1.219 +    }
   1.220 +
   1.221 +    /**
   1.222 +     * Closes this input stream and releases any system resources associated
   1.223 +     * with the stream.
   1.224 +     * @exception IOException if an I/O error has occurred
   1.225 +     */
   1.226 +    public void close() throws IOException {
   1.227 +        if (!closed) {
   1.228 +            if (usesDefaultInflater)
   1.229 +                inf.end();
   1.230 +            in.close();
   1.231 +            closed = true;
   1.232 +        }
   1.233 +    }
   1.234 +
   1.235 +    /**
   1.236 +     * Fills input buffer with more data to decompress.
   1.237 +     * @exception IOException if an I/O error has occurred
   1.238 +     */
   1.239 +    protected void fill() throws IOException {
   1.240 +        ensureOpen();
   1.241 +        len = in.read(buf, 0, buf.length);
   1.242 +        if (len == -1) {
   1.243 +            throw new EOFException("Unexpected end of ZLIB input stream");
   1.244 +        }
   1.245 +        inf.setInput(buf, 0, len);
   1.246 +    }
   1.247 +
   1.248 +    /**
   1.249 +     * Tests if this input stream supports the <code>mark</code> and
   1.250 +     * <code>reset</code> methods. The <code>markSupported</code>
   1.251 +     * method of <code>InflaterInputStream</code> returns
   1.252 +     * <code>false</code>.
   1.253 +     *
   1.254 +     * @return  a <code>boolean</code> indicating if this stream type supports
   1.255 +     *          the <code>mark</code> and <code>reset</code> methods.
   1.256 +     * @see     java.io.InputStream#mark(int)
   1.257 +     * @see     java.io.InputStream#reset()
   1.258 +     */
   1.259 +    public boolean markSupported() {
   1.260 +        return false;
   1.261 +    }
   1.262 +
   1.263 +    /**
   1.264 +     * Marks the current position in this input stream.
   1.265 +     *
   1.266 +     * <p> The <code>mark</code> method of <code>InflaterInputStream</code>
   1.267 +     * does nothing.
   1.268 +     *
   1.269 +     * @param   readlimit   the maximum limit of bytes that can be read before
   1.270 +     *                      the mark position becomes invalid.
   1.271 +     * @see     java.io.InputStream#reset()
   1.272 +     */
   1.273 +    public synchronized void mark(int readlimit) {
   1.274 +    }
   1.275 +
   1.276 +    /**
   1.277 +     * Repositions this stream to the position at the time the
   1.278 +     * <code>mark</code> method was last called on this input stream.
   1.279 +     *
   1.280 +     * <p> The method <code>reset</code> for class
   1.281 +     * <code>InflaterInputStream</code> does nothing except throw an
   1.282 +     * <code>IOException</code>.
   1.283 +     *
   1.284 +     * @exception  IOException  if this method is invoked.
   1.285 +     * @see     java.io.InputStream#mark(int)
   1.286 +     * @see     java.io.IOException
   1.287 +     */
   1.288 +    public synchronized void reset() throws IOException {
   1.289 +        throw new IOException("mark/reset not supported");
   1.290 +    }
   1.291 +}