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