InflaterInputStream.java revision a8ed084745590c5e4a0e8559b5821809d60fe242
1/* 2 * Copyright (c) 1996, 2006, 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 26package java.util.zip; 27 28import java.io.FilterInputStream; 29import java.io.InputStream; 30import java.io.IOException; 31import java.io.EOFException; 32 33/** 34 * This class implements a stream filter for uncompressing data in the 35 * "deflate" compression format. It is also used as the basis for other 36 * decompression filters, such as GZIPInputStream. 37 * 38 * @see Inflater 39 * @author David Connelly 40 */ 41public 42class InflaterInputStream extends FilterInputStream { 43 /** 44 * Decompressor for this stream. 45 */ 46 protected Inflater inf; 47 48 /** 49 * Input buffer for decompression. 50 */ 51 protected byte[] buf; 52 53 /** 54 * Length of input buffer. 55 */ 56 protected int len; 57 58 // Android-changed: closed is now protected. 59 protected boolean closed = false; 60 61 // this flag is set to true after EOF has reached 62 private boolean reachEOF = false; 63 64 /** 65 * Check to make sure that this stream has not been closed 66 */ 67 private void ensureOpen() throws IOException { 68 if (closed) { 69 throw new IOException("Stream closed"); 70 } 71 } 72 73 74 /** 75 * Creates a new input stream with the specified decompressor and 76 * buffer size. 77 * @param in the input stream 78 * @param inf the decompressor ("inflater") 79 * @param size the input buffer size 80 * @exception IllegalArgumentException if size is <= 0 81 */ 82 public InflaterInputStream(InputStream in, Inflater inf, int size) { 83 super(in); 84 if (in == null || inf == null) { 85 throw new NullPointerException(); 86 } else if (size <= 0) { 87 throw new IllegalArgumentException("buffer size <= 0"); 88 } 89 this.inf = inf; 90 buf = new byte[size]; 91 } 92 93 /** 94 * Creates a new input stream with the specified decompressor and a 95 * default buffer size. 96 * @param in the input stream 97 * @param inf the decompressor ("inflater") 98 */ 99 public InflaterInputStream(InputStream in, Inflater inf) { 100 this(in, inf, 512); 101 } 102 103 boolean usesDefaultInflater = false; 104 105 /** 106 * Creates a new input stream with a default decompressor and buffer size. 107 * @param in the input stream 108 */ 109 public InflaterInputStream(InputStream in) { 110 this(in, new Inflater()); 111 usesDefaultInflater = true; 112 } 113 114 private byte[] singleByteBuf = new byte[1]; 115 116 /** 117 * Reads a byte of uncompressed data. This method will block until 118 * enough input is available for decompression. 119 * @return the byte read, or -1 if end of compressed input is reached 120 * @exception IOException if an I/O error has occurred 121 */ 122 public int read() throws IOException { 123 ensureOpen(); 124 return read(singleByteBuf, 0, 1) == -1 ? -1 : singleByteBuf[0] & 0xff; 125 } 126 127 /** 128 * Reads uncompressed data into an array of bytes. If <code>len</code> is not 129 * zero, the method will block until some input can be decompressed; otherwise, 130 * no bytes are read and <code>0</code> is returned. 131 * @param b the buffer into which the data is read 132 * @param off the start offset in the destination array <code>b</code> 133 * @param len the maximum number of bytes read 134 * @return the actual number of bytes read, or -1 if the end of the 135 * compressed input is reached or a preset dictionary is needed 136 * @exception NullPointerException If <code>b</code> is <code>null</code>. 137 * @exception IndexOutOfBoundsException If <code>off</code> is negative, 138 * <code>len</code> is negative, or <code>len</code> is greater than 139 * <code>b.length - off</code> 140 * @exception ZipException if a ZIP format error has occurred 141 * @exception IOException if an I/O error has occurred 142 */ 143 public int read(byte[] b, int off, int len) throws IOException { 144 ensureOpen(); 145 if (b == null) { 146 throw new NullPointerException(); 147 } else if (off < 0 || len < 0 || len > b.length - off) { 148 throw new IndexOutOfBoundsException(); 149 } else if (len == 0) { 150 return 0; 151 } 152 try { 153 int n; 154 while ((n = inf.inflate(b, off, len)) == 0) { 155 if (inf.finished() || inf.needsDictionary()) { 156 reachEOF = true; 157 return -1; 158 } 159 if (inf.needsInput()) { 160 fill(); 161 } 162 } 163 return n; 164 } catch (DataFormatException e) { 165 String s = e.getMessage(); 166 throw new ZipException(s != null ? s : "Invalid ZLIB data format"); 167 } 168 } 169 170 /** 171 * Returns 0 after EOF has been reached, otherwise always return 1. 172 * <p> 173 * Programs should not count on this method to return the actual number 174 * of bytes that could be read without blocking. 175 * 176 * @return 1 before EOF and 0 after EOF. 177 * @exception IOException if an I/O error occurs. 178 * 179 */ 180 public int available() throws IOException { 181 ensureOpen(); 182 if (reachEOF) { 183 return 0; 184 } else { 185 return 1; 186 } 187 } 188 189 private byte[] b = new byte[512]; 190 191 /** 192 * Skips specified number of bytes of uncompressed data. 193 * @param n the number of bytes to skip 194 * @return the actual number of bytes skipped. 195 * @exception IOException if an I/O error has occurred 196 * @exception IllegalArgumentException if n < 0 197 */ 198 public long skip(long n) throws IOException { 199 if (n < 0) { 200 throw new IllegalArgumentException("negative skip length"); 201 } 202 ensureOpen(); 203 int max = (int)Math.min(n, Integer.MAX_VALUE); 204 int total = 0; 205 while (total < max) { 206 int len = max - total; 207 if (len > b.length) { 208 len = b.length; 209 } 210 len = read(b, 0, len); 211 if (len == -1) { 212 reachEOF = true; 213 break; 214 } 215 total += len; 216 } 217 return total; 218 } 219 220 /** 221 * Closes this input stream and releases any system resources associated 222 * with the stream. 223 * @exception IOException if an I/O error has occurred 224 */ 225 public void close() throws IOException { 226 if (!closed) { 227 if (usesDefaultInflater) 228 inf.end(); 229 in.close(); 230 closed = true; 231 } 232 } 233 234 /** 235 * Fills input buffer with more data to decompress. 236 * @exception IOException if an I/O error has occurred 237 */ 238 protected void fill() throws IOException { 239 ensureOpen(); 240 len = in.read(buf, 0, buf.length); 241 if (len == -1) { 242 throw new EOFException("Unexpected end of ZLIB input stream"); 243 } 244 inf.setInput(buf, 0, len); 245 } 246 247 /** 248 * Tests if this input stream supports the <code>mark</code> and 249 * <code>reset</code> methods. The <code>markSupported</code> 250 * method of <code>InflaterInputStream</code> returns 251 * <code>false</code>. 252 * 253 * @return a <code>boolean</code> indicating if this stream type supports 254 * the <code>mark</code> and <code>reset</code> methods. 255 * @see java.io.InputStream#mark(int) 256 * @see java.io.InputStream#reset() 257 */ 258 public boolean markSupported() { 259 return false; 260 } 261 262 /** 263 * Marks the current position in this input stream. 264 * 265 * <p> The <code>mark</code> method of <code>InflaterInputStream</code> 266 * does nothing. 267 * 268 * @param readlimit the maximum limit of bytes that can be read before 269 * the mark position becomes invalid. 270 * @see java.io.InputStream#reset() 271 */ 272 public synchronized void mark(int readlimit) { 273 } 274 275 /** 276 * Repositions this stream to the position at the time the 277 * <code>mark</code> method was last called on this input stream. 278 * 279 * <p> The method <code>reset</code> for class 280 * <code>InflaterInputStream</code> does nothing except throw an 281 * <code>IOException</code>. 282 * 283 * @exception IOException if this method is invoked. 284 * @see java.io.InputStream#mark(int) 285 * @see java.io.IOException 286 */ 287 public synchronized void reset() throws IOException { 288 throw new IOException("mark/reset not supported"); 289 } 290} 291