jaroslav@609: /* jaroslav@609: * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@609: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@609: * jaroslav@609: * This code is free software; you can redistribute it and/or modify it jaroslav@609: * under the terms of the GNU General Public License version 2 only, as jaroslav@609: * published by the Free Software Foundation. Oracle designates this jaroslav@609: * particular file as subject to the "Classpath" exception as provided jaroslav@609: * by Oracle in the LICENSE file that accompanied this code. jaroslav@609: * jaroslav@609: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@609: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@609: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@609: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@609: * accompanied this code). jaroslav@609: * jaroslav@609: * You should have received a copy of the GNU General Public License version jaroslav@609: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@609: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@609: * jaroslav@609: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@609: * or visit www.oracle.com if you need additional information or have any jaroslav@609: * questions. jaroslav@609: */ jaroslav@609: jaroslav@609: package java.util.zip; jaroslav@609: jaroslav@609: import java.io.FilterInputStream; jaroslav@609: import java.io.InputStream; jaroslav@609: import java.io.IOException; jaroslav@609: import java.io.EOFException; jaroslav@609: jaroslav@609: /** jaroslav@609: * This class implements a stream filter for uncompressing data in the jaroslav@609: * "deflate" compression format. It is also used as the basis for other jaroslav@609: * decompression filters, such as GZIPInputStream. jaroslav@609: * jaroslav@609: * @see Inflater jaroslav@609: * @author David Connelly jaroslav@609: */ jaroslav@609: public jaroslav@609: class InflaterInputStream extends FilterInputStream { jaroslav@609: /** jaroslav@609: * Decompressor for this stream. jaroslav@609: */ jaroslav@609: protected Inflater inf; jaroslav@609: jaroslav@609: /** jaroslav@609: * Input buffer for decompression. jaroslav@609: */ jaroslav@609: protected byte[] buf; jaroslav@609: jaroslav@609: /** jaroslav@609: * Length of input buffer. jaroslav@609: */ jaroslav@609: protected int len; jaroslav@609: jaroslav@609: private boolean closed = false; jaroslav@609: // this flag is set to true after EOF has reached jaroslav@609: private boolean reachEOF = false; jaroslav@609: jaroslav@609: /** jaroslav@609: * Check to make sure that this stream has not been closed jaroslav@609: */ jaroslav@609: private void ensureOpen() throws IOException { jaroslav@609: if (closed) { jaroslav@609: throw new IOException("Stream closed"); jaroslav@609: } jaroslav@609: } jaroslav@609: jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new input stream with the specified decompressor and jaroslav@609: * buffer size. jaroslav@609: * @param in the input stream jaroslav@609: * @param inf the decompressor ("inflater") jaroslav@609: * @param size the input buffer size jaroslav@609: * @exception IllegalArgumentException if size is <= 0 jaroslav@609: */ jaroslav@609: public InflaterInputStream(InputStream in, Inflater inf, int size) { jaroslav@609: super(in); jaroslav@609: if (in == null || inf == null) { jaroslav@609: throw new NullPointerException(); jaroslav@609: } else if (size <= 0) { jaroslav@609: throw new IllegalArgumentException("buffer size <= 0"); jaroslav@609: } jaroslav@609: this.inf = inf; jaroslav@609: buf = new byte[size]; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new input stream with the specified decompressor and a jaroslav@609: * default buffer size. jaroslav@609: * @param in the input stream jaroslav@609: * @param inf the decompressor ("inflater") jaroslav@609: */ jaroslav@609: public InflaterInputStream(InputStream in, Inflater inf) { jaroslav@609: this(in, inf, 512); jaroslav@609: } jaroslav@609: jaroslav@609: boolean usesDefaultInflater = false; jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new input stream with a default decompressor and buffer size. jaroslav@609: * @param in the input stream jaroslav@609: */ jaroslav@609: public InflaterInputStream(InputStream in) { jaroslav@609: this(in, new Inflater()); jaroslav@609: usesDefaultInflater = true; jaroslav@609: } jaroslav@609: jaroslav@609: private byte[] singleByteBuf = new byte[1]; jaroslav@609: jaroslav@609: /** jaroslav@609: * Reads a byte of uncompressed data. This method will block until jaroslav@609: * enough input is available for decompression. jaroslav@609: * @return the byte read, or -1 if end of compressed input is reached jaroslav@609: * @exception IOException if an I/O error has occurred jaroslav@609: */ jaroslav@609: public int read() throws IOException { jaroslav@609: ensureOpen(); jaroslav@609: return read(singleByteBuf, 0, 1) == -1 ? -1 : singleByteBuf[0] & 0xff; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Reads uncompressed data into an array of bytes. If len is not jaroslav@609: * zero, the method will block until some input can be decompressed; otherwise, jaroslav@609: * no bytes are read and 0 is returned. jaroslav@609: * @param b the buffer into which the data is read jaroslav@609: * @param off the start offset in the destination array b jaroslav@609: * @param len the maximum number of bytes read jaroslav@609: * @return the actual number of bytes read, or -1 if the end of the jaroslav@609: * compressed input is reached or a preset dictionary is needed jaroslav@609: * @exception NullPointerException If b is null. jaroslav@609: * @exception IndexOutOfBoundsException If off is negative, jaroslav@609: * len is negative, or len is greater than jaroslav@609: * b.length - off jaroslav@609: * @exception ZipException if a ZIP format error has occurred jaroslav@609: * @exception IOException if an I/O error has occurred jaroslav@609: */ jaroslav@609: public int read(byte[] b, int off, int len) throws IOException { jaroslav@609: ensureOpen(); jaroslav@609: if (b == null) { jaroslav@609: throw new NullPointerException(); jaroslav@609: } else if (off < 0 || len < 0 || len > b.length - off) { jaroslav@609: throw new IndexOutOfBoundsException(); jaroslav@609: } else if (len == 0) { jaroslav@609: return 0; jaroslav@609: } jaroslav@609: try { jaroslav@609: int n; jaroslav@609: while ((n = inf.inflate(b, off, len)) == 0) { jaroslav@609: if (inf.finished() || inf.needsDictionary()) { jaroslav@609: reachEOF = true; jaroslav@609: return -1; jaroslav@609: } jaroslav@609: if (inf.needsInput()) { jaroslav@609: fill(); jaroslav@609: } jaroslav@609: } jaroslav@609: return n; jaroslav@609: } catch (DataFormatException e) { jaroslav@609: String s = e.getMessage(); jaroslav@609: throw new ZipException(s != null ? s : "Invalid ZLIB data format"); jaroslav@609: } jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns 0 after EOF has been reached, otherwise always return 1. jaroslav@609: *

jaroslav@609: * Programs should not count on this method to return the actual number jaroslav@609: * of bytes that could be read without blocking. jaroslav@609: * jaroslav@609: * @return 1 before EOF and 0 after EOF. jaroslav@609: * @exception IOException if an I/O error occurs. jaroslav@609: * jaroslav@609: */ jaroslav@609: public int available() throws IOException { jaroslav@609: ensureOpen(); jaroslav@609: if (reachEOF) { jaroslav@609: return 0; jaroslav@609: } else { jaroslav@609: return 1; jaroslav@609: } jaroslav@609: } jaroslav@609: jaroslav@609: private byte[] b = new byte[512]; jaroslav@609: jaroslav@609: /** jaroslav@609: * Skips specified number of bytes of uncompressed data. jaroslav@609: * @param n the number of bytes to skip jaroslav@609: * @return the actual number of bytes skipped. jaroslav@609: * @exception IOException if an I/O error has occurred jaroslav@609: * @exception IllegalArgumentException if n < 0 jaroslav@609: */ jaroslav@609: public long skip(long n) throws IOException { jaroslav@609: if (n < 0) { jaroslav@609: throw new IllegalArgumentException("negative skip length"); jaroslav@609: } jaroslav@609: ensureOpen(); jaroslav@609: int max = (int)Math.min(n, Integer.MAX_VALUE); jaroslav@609: int total = 0; jaroslav@609: while (total < max) { jaroslav@609: int len = max - total; jaroslav@609: if (len > b.length) { jaroslav@609: len = b.length; jaroslav@609: } jaroslav@609: len = read(b, 0, len); jaroslav@609: if (len == -1) { jaroslav@609: reachEOF = true; jaroslav@609: break; jaroslav@609: } jaroslav@609: total += len; jaroslav@609: } jaroslav@609: return total; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Closes this input stream and releases any system resources associated jaroslav@609: * with the stream. jaroslav@609: * @exception IOException if an I/O error has occurred jaroslav@609: */ jaroslav@609: public void close() throws IOException { jaroslav@609: if (!closed) { jaroslav@609: if (usesDefaultInflater) jaroslav@609: inf.end(); jaroslav@609: in.close(); jaroslav@609: closed = true; jaroslav@609: } jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Fills input buffer with more data to decompress. jaroslav@609: * @exception IOException if an I/O error has occurred jaroslav@609: */ jaroslav@609: protected void fill() throws IOException { jaroslav@609: ensureOpen(); jaroslav@609: len = in.read(buf, 0, buf.length); jaroslav@609: if (len == -1) { jaroslav@609: throw new EOFException("Unexpected end of ZLIB input stream"); jaroslav@609: } jaroslav@609: inf.setInput(buf, 0, len); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Tests if this input stream supports the mark and jaroslav@609: * reset methods. The markSupported jaroslav@609: * method of InflaterInputStream returns jaroslav@609: * false. jaroslav@609: * jaroslav@609: * @return a boolean indicating if this stream type supports jaroslav@609: * the mark and reset methods. jaroslav@609: * @see java.io.InputStream#mark(int) jaroslav@609: * @see java.io.InputStream#reset() jaroslav@609: */ jaroslav@609: public boolean markSupported() { jaroslav@609: return false; jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Marks the current position in this input stream. jaroslav@609: * jaroslav@609: *

The mark method of InflaterInputStream jaroslav@609: * does nothing. jaroslav@609: * jaroslav@609: * @param readlimit the maximum limit of bytes that can be read before jaroslav@609: * the mark position becomes invalid. jaroslav@609: * @see java.io.InputStream#reset() jaroslav@609: */ jaroslav@609: public synchronized void mark(int readlimit) { jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Repositions this stream to the position at the time the jaroslav@609: * mark method was last called on this input stream. jaroslav@609: * jaroslav@609: *

The method reset for class jaroslav@609: * InflaterInputStream does nothing except throw an jaroslav@609: * IOException. jaroslav@609: * jaroslav@609: * @exception IOException if this method is invoked. jaroslav@609: * @see java.io.InputStream#mark(int) jaroslav@609: * @see java.io.IOException jaroslav@609: */ jaroslav@609: public synchronized void reset() throws IOException { jaroslav@609: throw new IOException("mark/reset not supported"); jaroslav@609: } jaroslav@609: }