jaroslav@609: /* jaroslav@609: * Copyright (c) 1996, 2009, 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.InputStream; jaroslav@609: import java.io.IOException; jaroslav@609: jaroslav@609: /** jaroslav@609: * This class implements an input stream filter for reading files in the jaroslav@609: * ZIP file format. Includes support for both compressed and uncompressed jaroslav@609: * entries. jaroslav@609: * jaroslav@609: * @author David Connelly jaroslav@609: */ jaroslav@609: public jaroslav@609: class ZipInputStream extends InflaterInputStream implements ZipConstants { jaroslav@694: private final org.apidesign.bck2brwsr.emul.zip.ZipInputStream impl; jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new ZIP input stream. jaroslav@609: * jaroslav@609: *

The UTF-8 {@link java.nio.charset.Charset charset} is used to jaroslav@609: * decode the entry names. jaroslav@609: * jaroslav@609: * @param in the actual input stream jaroslav@609: */ jaroslav@609: public ZipInputStream(InputStream in) { jaroslav@701: super(in); jaroslav@694: impl = new org.apidesign.bck2brwsr.emul.zip.ZipInputStream(in); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new ZIP input stream. jaroslav@609: * jaroslav@609: * @param in the actual input stream jaroslav@609: * jaroslav@609: * @param charset jaroslav@609: * The {@linkplain java.nio.charset.Charset charset} to be jaroslav@609: * used to decode the ZIP entry name (ignored if the jaroslav@609: * language jaroslav@609: * encoding bit of the ZIP entry's general purpose bit jaroslav@609: * flag is set). jaroslav@609: * jaroslav@609: * @since 1.7 jaroslav@611: * jaroslav@609: public ZipInputStream(InputStream in, Charset charset) { jaroslav@609: super(new PushbackInputStream(in, 512), new Inflater(true), 512); jaroslav@609: usesDefaultInflater = true; jaroslav@609: if(in == null) { jaroslav@609: throw new NullPointerException("in is null"); jaroslav@609: } jaroslav@609: if (charset == null) jaroslav@609: throw new NullPointerException("charset is null"); jaroslav@609: this.zc = ZipCoder.get(charset); jaroslav@609: } jaroslav@611: */ jaroslav@609: jaroslav@609: /** jaroslav@609: * Reads the next ZIP file entry and positions the stream at the jaroslav@609: * beginning of the entry data. jaroslav@609: * @return the next ZIP file entry, or null if there are no more entries jaroslav@609: * @exception ZipException if a ZIP file error has occurred jaroslav@609: * @exception IOException if an I/O error has occurred jaroslav@609: */ jaroslav@609: public ZipEntry getNextEntry() throws IOException { jaroslav@694: return impl.getNextEntry(); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Closes the current ZIP entry and positions the stream for reading the jaroslav@609: * next entry. jaroslav@609: * @exception ZipException if a ZIP file error has occurred jaroslav@609: * @exception IOException if an I/O error has occurred jaroslav@609: */ jaroslav@609: public void closeEntry() throws IOException { jaroslav@694: impl.closeEntry(); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Returns 0 after EOF has reached for the current entry data, jaroslav@609: * 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 has reached for current entry. jaroslav@609: * @exception IOException if an I/O error occurs. jaroslav@609: * jaroslav@609: */ jaroslav@609: public int available() throws IOException { jaroslav@694: return impl.available(); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Reads from the current ZIP entry into an array of bytes. jaroslav@609: * If len is not zero, the method jaroslav@609: * blocks until some input is available; otherwise, no jaroslav@609: * 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: * entry is reached 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 file 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@694: return impl.read(b, off, len); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Skips specified number of bytes in the current ZIP entry. jaroslav@609: * @param n the number of bytes to skip jaroslav@609: * @return the actual number of bytes skipped jaroslav@609: * @exception ZipException if a ZIP file error has occurred 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@694: return impl.skip(n); 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@694: impl.close(); jaroslav@609: } jaroslav@609: jaroslav@609: /** jaroslav@609: * Creates a new ZipEntry object for the specified jaroslav@609: * entry name. jaroslav@609: * jaroslav@609: * @param name the ZIP file entry name jaroslav@609: * @return the ZipEntry just created jaroslav@609: */ jaroslav@609: protected ZipEntry createZipEntry(String name) { jaroslav@609: return new ZipEntry(name); jaroslav@609: } jaroslav@609: jaroslav@694: @Override jaroslav@694: public int read() throws IOException { jaroslav@694: return impl.read(); jaroslav@609: } jaroslav@609: jaroslav@694: @Override jaroslav@694: public boolean markSupported() { jaroslav@694: return impl.markSupported(); jaroslav@609: } jaroslav@609: jaroslav@694: @Override jaroslav@694: public void mark(int readlimit) { jaroslav@694: impl.mark(readlimit); jaroslav@609: } jaroslav@609: jaroslav@694: @Override jaroslav@694: public void reset() throws IOException { jaroslav@694: impl.reset(); jaroslav@609: } jaroslav@609: jaroslav@694: @Override jaroslav@694: public int read(byte[] b) throws IOException { jaroslav@694: return impl.read(b); jaroslav@611: } jaroslav@609: }