src/java.base/share/classes/java/util/zip/DeflaterInputStream.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/java.base/share/classes/java/util/zip/DeflaterInputStream.java@3317bb8137f4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18156
diff changeset
     2
 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.zip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FilterInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Implements an input stream filter for compressing data in the "deflate"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * compression format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @since       1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author      David R Tribble (david@tribble.com)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see DeflaterOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see InflaterOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see InflaterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class DeflaterInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /** Compressor for this stream. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected final Deflater def;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** Input buffer for reading compressed data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected final byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /** Temporary read buffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private byte[] rbuf = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** Default compressor is used. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private boolean usesDefaultDeflater = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** End of the underlying input stream has been reached. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean reachEOF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Check to make sure that this stream has not been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates a new input stream with a default compressor and buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param in input stream to read the uncompressed data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @throws NullPointerException if {@code in} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public DeflaterInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this(in, new Deflater());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        usesDefaultDeflater = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Creates a new input stream with the specified compressor and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * default buffer size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param in input stream to read the uncompressed data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param defl compressor ("deflater") for this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @throws NullPointerException if {@code in} or {@code defl} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public DeflaterInputStream(InputStream in, Deflater defl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this(in, defl, 512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Creates a new input stream with the specified compressor and buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param in input stream to read the uncompressed data to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param defl compressor ("deflater") for this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param bufLen compression buffer size
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   100
     * @throws IllegalArgumentException if {@code bufLen <= 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws NullPointerException if {@code in} or {@code defl} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public DeflaterInputStream(InputStream in, Deflater defl, int bufLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // Sanity checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            throw new NullPointerException("Null input");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (defl == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new NullPointerException("Null deflater");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (bufLen < 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new IllegalArgumentException("Buffer size < 1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // Initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        def = defl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        buf = new byte[bufLen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Closes this input stream and its underlying input stream, discarding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * any pending uncompressed data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                // Clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if (usesDefaultDeflater) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    def.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Reads a single byte of compressed data from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * This method will block until some input can be read and compressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return a single byte of compressed data, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * uncompressed input stream is reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @throws IOException if an I/O error occurs or if this stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * already closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        // Read a single byte of compressed data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int len = read(rbuf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (len <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return (rbuf[0] & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Reads compressed data into a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * This method will block until some input can be read and compressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param b buffer into which the data is read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param off starting offset of the data within {@code b}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param len maximum number of compressed bytes to read into {@code b}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return the actual number of bytes read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * uncompressed input stream is reached
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   166
     * @throws IndexOutOfBoundsException  if {@code len > b.length - off}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @throws IOException if an I/O error occurs or if this input stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * already closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // Sanity checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new NullPointerException("Null buffer for read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else if (off < 0 || len < 0 || len > b.length - off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        // Read and compress (deflate) input data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        while (len > 0 && !def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // Read data from the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (def.needsInput()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                n = in.read(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    // End of the input stream reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    def.finish();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                } else if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    def.setInput(buf, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // Compress the input data, filling the read buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            n = def.deflate(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            cnt += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            off += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            len -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (cnt == 0 && def.finished()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            reachEOF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            cnt = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Skips over and discards data from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * This method may block until the specified number of bytes are read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * skipped. <em>Note:</em> While {@code n} is given as a {@code long},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * the maximum number of bytes which can be skipped is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * {@code Integer.MAX_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param n number of bytes to be skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return the actual number of bytes skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @throws IOException if an I/O error occurs or if this stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * already closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new IllegalArgumentException("negative skip length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // Skip bytes by repeatedly decompressing small blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (rbuf.length < 512)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            rbuf = new byte[512];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int total = (int)Math.min(n, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        long cnt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        while (total > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            // Read a small block of uncompressed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            int len = read(rbuf, 0, (total <= rbuf.length ? total : rbuf.length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            cnt += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            total -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return cnt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Returns 0 after EOF has been reached, otherwise always return 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Programs should not count on this method to return the actual number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * of bytes that could be read without blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return zero after the end of the underlying input stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * reached, otherwise always returns 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @throws IOException if an I/O error occurs or if this stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * already closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (reachEOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Always returns {@code false} because this input stream does not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * the {@link #mark mark()} and {@link #reset reset()} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @return false, always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * <i>This operation is not supported</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param limit maximum bytes that can be read before invalidating the position marker
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public void mark(int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        // Operation not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <i>This operation is not supported</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @throws IOException always thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}