Source Home >> Java Source 1.6.0 >> java.util.zip.InflaterInputStream V 0.09
  • 001/*
  • 002 * @(#)InflaterInputStream.java 1.40 06/04/07
  • 003 *
  • 004 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.util.zip;
  • 009
  • 010import java.io.FilterInputStream;
  • 011import java.io.InputStream;
  • 012import java.io.IOException;
  • 013import java.io.EOFException;
  • 014
  • 015/**
  • 016 * This class implements a stream filter for uncompressing data in the
  • 017 * "deflate" compression format. It is also used as the basis for other
  • 018 * decompression filters, such as GZIPInputStream.
  • 019 *
  • 020 * @see Inflater
  • 021 * @version 1.40, 04/07/06
  • 022 * @author David Connelly
  • 023 */
  • 024public
  • 025class InflaterInputStream extends FilterInputStream {
  • 026 /**
  • 027 * Decompressor for this stream.
  • 028 */
  • 029 protected Inflater inf;
  • 030
  • 031 /**
  • 032 * Input buffer for decompression.
  • 033 */
  • 034 protected byte[] buf;
  • 035
  • 036 /**
  • 037 * Length of input buffer.
  • 038 */
  • 039 protected int len;
  • 040
  • 041 private boolean closed = false;
  • 042 // this flag is set to true after EOF has reached
  • 043 private boolean reachEOF = false;
  • 044
  • 045 /**
  • 046 * Check to make sure that this stream has not been closed
  • 047 */
  • 048 private void ensureOpen() throws IOException {
  • 049 if (closed) {
  • 050 throw new IOException("Stream closed");
  • 051 }
  • 052 }
  • 053
  • 054
  • 055 /**
  • 056 * Creates a new input stream with the specified decompressor and
  • 057 * buffer size.
  • 058 * @param in the input stream
  • 059 * @param inf the decompressor ("inflater")
  • 060 * @param size the input buffer size
  • 061 * @exception IllegalArgumentException if size is <= 0
  • 062 */
  • 063 public InflaterInputStream(InputStream in, Inflater inf, int size) {
  • 064 super(in);
  • 065 if (in == null || inf == null) {
  • 066 throw new NullPointerException();
  • 067 } else if (size <= 0) {
  • 068 throw new IllegalArgumentException("buffer size <= 0");
  • 069 }
  • 070 this.inf = inf;
  • 071 buf = new byte[size];
  • 072 }
  • 073
  • 074 /**
  • 075 * Creates a new input stream with the specified decompressor and a
  • 076 * default buffer size.
  • 077 * @param in the input stream
  • 078 * @param inf the decompressor ("inflater")
  • 079 */
  • 080 public InflaterInputStream(InputStream in, Inflater inf) {
  • 081 this(in, inf, 512);
  • 082 }
  • 083
  • 084 boolean usesDefaultInflater = false;
  • 085
  • 086 /**
  • 087 * Creates a new input stream with a default decompressor and buffer size.
  • 088 * @param in the input stream
  • 089 */
  • 090 public InflaterInputStream(InputStream in) {
  • 091 this(in, new Inflater());
  • 092 usesDefaultInflater = true;
  • 093 }
  • 094
  • 095 private byte[] singleByteBuf = new byte[1];
  • 096
  • 097 /**
  • 098 * Reads a byte of uncompressed data. This method will block until
  • 099 * enough input is available for decompression.
  • 100 * @return the byte read, or -1 if end of compressed input is reached
  • 101 * @exception IOException if an I/O error has occurred
  • 102 */
  • 103 public int read() throws IOException {
  • 104 ensureOpen();
  • 105 return read(singleByteBuf, 0, 1) == -1 ? -1 : singleByteBuf[0] & 0xff;
  • 106 }
  • 107
  • 108 /**
  • 109 * Reads uncompressed data into an array of bytes. If <code>len</code> is not
  • 110 * zero, the method will block until some input can be decompressed; otherwise,
  • 111 * no bytes are read and <code>0</code> is returned.
  • 112 * @param b the buffer into which the data is read
  • 113 * @param off the start offset in the destination array <code>b</code>
  • 114 * @param len the maximum number of bytes read
  • 115 * @return the actual number of bytes read, or -1 if the end of the
  • 116 * compressed input is reached or a preset dictionary is needed
  • 117 * @exception NullPointerException If <code>b</code> is <code>null</code>.
  • 118 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
  • 119 * <code>len</code> is negative, or <code>len</code> is greater than
  • 120 * <code>b.length - off</code>
  • 121 * @exception ZipException if a ZIP format error has occurred
  • 122 * @exception IOException if an I/O error has occurred
  • 123 */
  • 124 public int read(byte[] b, int off, int len) throws IOException {
  • 125 ensureOpen();
  • 126 if (b == null) {
  • 127 throw new NullPointerException();
  • 128 } else if (off < 0 || len < 0 || len > b.length - off) {
  • 129 throw new IndexOutOfBoundsException();
  • 130 } else if (len == 0) {
  • 131 return 0;
  • 132 }
  • 133 try {
  • 134 int n;
  • 135 while ((n = inf.inflate(b, off, len)) == 0) {
  • 136 if (inf.finished() || inf.needsDictionary()) {
  • 137 reachEOF = true;
  • 138 return -1;
  • 139 }
  • 140 if (inf.needsInput()) {
  • 141 fill();
  • 142 }
  • 143 }
  • 144 return n;
  • 145 } catch (DataFormatException e) {
  • 146 String s = e.getMessage();
  • 147 throw new ZipException(s != null ? s : "Invalid ZLIB data format");
  • 148 }
  • 149 }
  • 150
  • 151 /**
  • 152 * Returns 0 after EOF has been reached, otherwise always return 1.
  • 153 * <p>
  • 154 * Programs should not count on this method to return the actual number
  • 155 * of bytes that could be read without blocking.
  • 156 *
  • 157 * @return 1 before EOF and 0 after EOF.
  • 158 * @exception IOException if an I/O error occurs.
  • 159 *
  • 160 */
  • 161 public int available() throws IOException {
  • 162 ensureOpen();
  • 163 if (reachEOF) {
  • 164 return 0;
  • 165 } else {
  • 166 return 1;
  • 167 }
  • 168 }
  • 169
  • 170 private byte[] b = new byte[512];
  • 171
  • 172 /**
  • 173 * Skips specified number of bytes of uncompressed data.
  • 174 * @param n the number of bytes to skip
  • 175 * @return the actual number of bytes skipped.
  • 176 * @exception IOException if an I/O error has occurred
  • 177 * @exception IllegalArgumentException if n < 0
  • 178 */
  • 179 public long skip(long n) throws IOException {
  • 180 if (n < 0) {
  • 181 throw new IllegalArgumentException("negative skip length");
  • 182 }
  • 183 ensureOpen();
  • 184 int max = (int)Math.min(n, Integer.MAX_VALUE);
  • 185 int total = 0;
  • 186 while (total < max) {
  • 187 int len = max - total;
  • 188 if (len > b.length) {
  • 189 len = b.length;
  • 190 }
  • 191 len = read(b, 0, len);
  • 192 if (len == -1) {
  • 193 reachEOF = true;
  • 194 break;
  • 195 }
  • 196 total += len;
  • 197 }
  • 198 return total;
  • 199 }
  • 200
  • 201 /**
  • 202 * Closes this input stream and releases any system resources associated
  • 203 * with the stream.
  • 204 * @exception IOException if an I/O error has occurred
  • 205 */
  • 206 public void close() throws IOException {
  • 207 if (!closed) {
  • 208 if (usesDefaultInflater)
  • 209 inf.end();
  • 210 in.close();
  • 211 closed = true;
  • 212 }
  • 213 }
  • 214
  • 215 /**
  • 216 * Fills input buffer with more data to decompress.
  • 217 * @exception IOException if an I/O error has occurred
  • 218 */
  • 219 protected void fill() throws IOException {
  • 220 ensureOpen();
  • 221 len = in.read(buf, 0, buf.length);
  • 222 if (len == -1) {
  • 223 throw new EOFException("Unexpected end of ZLIB input stream");
  • 224 }
  • 225 inf.setInput(buf, 0, len);
  • 226 }
  • 227
  • 228 /**
  • 229 * Tests if this input stream supports the <code>mark</code> and
  • 230 * <code>reset</code> methods. The <code>markSupported</code>
  • 231 * method of <code>InflaterInputStream</code> returns
  • 232 * <code>false</code>.
  • 233 *
  • 234 * @return a <code>boolean</code> indicating if this stream type supports
  • 235 * the <code>mark</code> and <code>reset</code> methods.
  • 236 * @see java.io.InputStream#mark(int)
  • 237 * @see java.io.InputStream#reset()
  • 238 */
  • 239 public boolean markSupported() {
  • 240 return false;
  • 241 }
  • 242
  • 243 /**
  • 244 * Marks the current position in this input stream.
  • 245 *
  • 246 * <p> The <code>mark</code> method of <code>InflaterInputStream</code>
  • 247 * does nothing.
  • 248 *
  • 249 * @param readlimit the maximum limit of bytes that can be read before
  • 250 * the mark position becomes invalid.
  • 251 * @see java.io.InputStream#reset()
  • 252 */
  • 253 public synchronized void mark(int readlimit) {
  • 254 }
  • 255
  • 256 /**
  • 257 * Repositions this stream to the position at the time the
  • 258 * <code>mark</code> method was last called on this input stream.
  • 259 *
  • 260 * <p> The method <code>reset</code> for class
  • 261 * <code>InflaterInputStream</code> does nothing except throw an
  • 262 * <code>IOException</code>.
  • 263 *
  • 264 * @exception IOException if this method is invoked.
  • 265 * @see java.io.InputStream#mark(int)
  • 266 * @see java.io.IOException
  • 267 */
  • 268 public synchronized void reset() throws IOException {
  • 269 throw new IOException("mark/reset not supported");
  • 270 }
  • 271}

文件:InflaterInputStream.java
包名:java.util.zip
类名:InflaterInputStream
继承:FilterInputStream
接口: