emul/mini/src/main/java/java/util/zip/ZipInputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Feb 2013 12:58:12 +0100
branchemul
changeset 694 0d277415ed02
parent 611 9839e9a75bcf
child 701 bfb3f72249de
permissions -rw-r--r--
Rebasing the Inflater support on jzlib which, unlike GNU ClassPath, has correct implementation of Huffman code. Making the implementation more easily testable by turning Inflater and ZipInputStream into pure delegates. Current implementation is going to need proper long support.
     1 /*
     2  * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.util.zip;
    27 
    28 import java.io.InputStream;
    29 import java.io.IOException;
    30 
    31 /**
    32  * This class implements an input stream filter for reading files in the
    33  * ZIP file format. Includes support for both compressed and uncompressed
    34  * entries.
    35  *
    36  * @author      David Connelly
    37  */
    38 public
    39 class ZipInputStream extends InflaterInputStream implements ZipConstants {
    40     private final org.apidesign.bck2brwsr.emul.zip.ZipInputStream impl;
    41 
    42     /**
    43      * Creates a new ZIP input stream.
    44      *
    45      * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
    46      * decode the entry names.
    47      *
    48      * @param in the actual input stream
    49      */
    50     public ZipInputStream(InputStream in) {
    51         super(null);
    52         impl = new org.apidesign.bck2brwsr.emul.zip.ZipInputStream(in);
    53     }
    54 
    55     /**
    56      * Creates a new ZIP input stream.
    57      *
    58      * @param in the actual input stream
    59      *
    60      * @param charset
    61      *        The {@linkplain java.nio.charset.Charset charset} to be
    62      *        used to decode the ZIP entry name (ignored if the
    63      *        <a href="package-summary.html#lang_encoding"> language
    64      *        encoding bit</a> of the ZIP entry's general purpose bit
    65      *        flag is set).
    66      *
    67      * @since 1.7
    68      *
    69     public ZipInputStream(InputStream in, Charset charset) {
    70         super(new PushbackInputStream(in, 512), new Inflater(true), 512);
    71         usesDefaultInflater = true;
    72         if(in == null) {
    73             throw new NullPointerException("in is null");
    74         }
    75         if (charset == null)
    76             throw new NullPointerException("charset is null");
    77         this.zc = ZipCoder.get(charset);
    78     }
    79     */
    80 
    81     /**
    82      * Reads the next ZIP file entry and positions the stream at the
    83      * beginning of the entry data.
    84      * @return the next ZIP file entry, or null if there are no more entries
    85      * @exception ZipException if a ZIP file error has occurred
    86      * @exception IOException if an I/O error has occurred
    87      */
    88     public ZipEntry getNextEntry() throws IOException {
    89         return impl.getNextEntry();
    90     }
    91 
    92     /**
    93      * Closes the current ZIP entry and positions the stream for reading the
    94      * next entry.
    95      * @exception ZipException if a ZIP file error has occurred
    96      * @exception IOException if an I/O error has occurred
    97      */
    98     public void closeEntry() throws IOException {
    99         impl.closeEntry();
   100     }
   101 
   102     /**
   103      * Returns 0 after EOF has reached for the current entry data,
   104      * otherwise always return 1.
   105      * <p>
   106      * Programs should not count on this method to return the actual number
   107      * of bytes that could be read without blocking.
   108      *
   109      * @return     1 before EOF and 0 after EOF has reached for current entry.
   110      * @exception  IOException  if an I/O error occurs.
   111      *
   112      */
   113     public int available() throws IOException {
   114         return impl.available();
   115     }
   116 
   117     /**
   118      * Reads from the current ZIP entry into an array of bytes.
   119      * If <code>len</code> is not zero, the method
   120      * blocks until some input is available; otherwise, no
   121      * bytes are read and <code>0</code> is returned.
   122      * @param b the buffer into which the data is read
   123      * @param off the start offset in the destination array <code>b</code>
   124      * @param len the maximum number of bytes read
   125      * @return the actual number of bytes read, or -1 if the end of the
   126      *         entry is reached
   127      * @exception  NullPointerException if <code>b</code> is <code>null</code>.
   128      * @exception  IndexOutOfBoundsException if <code>off</code> is negative,
   129      * <code>len</code> is negative, or <code>len</code> is greater than
   130      * <code>b.length - off</code>
   131      * @exception ZipException if a ZIP file error has occurred
   132      * @exception IOException if an I/O error has occurred
   133      */
   134     public int read(byte[] b, int off, int len) throws IOException {
   135         return impl.read(b, off, len);
   136     }
   137 
   138     /**
   139      * Skips specified number of bytes in the current ZIP entry.
   140      * @param n the number of bytes to skip
   141      * @return the actual number of bytes skipped
   142      * @exception ZipException if a ZIP file error has occurred
   143      * @exception IOException if an I/O error has occurred
   144      * @exception IllegalArgumentException if n < 0
   145      */
   146     public long skip(long n) throws IOException {
   147         return impl.skip(n);
   148     }
   149 
   150     /**
   151      * Closes this input stream and releases any system resources associated
   152      * with the stream.
   153      * @exception IOException if an I/O error has occurred
   154      */
   155     public void close() throws IOException {
   156         impl.close();
   157     }
   158 
   159     /**
   160      * Creates a new <code>ZipEntry</code> object for the specified
   161      * entry name.
   162      *
   163      * @param name the ZIP file entry name
   164      * @return the ZipEntry just created
   165      */
   166     protected ZipEntry createZipEntry(String name) {
   167         return new ZipEntry(name);
   168     }
   169 
   170     @Override
   171     public int read() throws IOException {
   172         return impl.read();
   173     }
   174 
   175     @Override
   176     public boolean markSupported() {
   177         return impl.markSupported();
   178     }
   179 
   180     @Override
   181     public void mark(int readlimit) {
   182         impl.mark(readlimit);
   183     }
   184 
   185     @Override
   186     public void reset() throws IOException {
   187         impl.reset();
   188     }
   189 
   190     @Override
   191     public int read(byte[] b) throws IOException {
   192         return impl.read(b);
   193     }
   194 }