1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.util.zip; 28 29 import java.io.FilterInputStream; 30 import java.io.InputStream; 31 import java.io.IOException; 32 import java.io.EOFException; 33 34 /** 35 * This class implements a stream filter for uncompressing data in the 36 * "deflate" compression format. It is also used as the basis for other 37 * decompression filters, such as GZIPInputStream. 38 * 39 * @see Inflater 40 * @author David Connelly 41 */ 42 public 43 class InflaterInputStream extends FilterInputStream { 44 /** 45 * Decompressor for this stream. 46 */ 47 protected Inflater inf; 48 49 /** 50 * Input buffer for decompression. 51 */ 52 protected byte[] buf; 53 54 /** 55 * Length of input buffer. 56 */ 57 protected int len; 58 59 // Android-changed: closed is now protected. 60 protected boolean closed = false; 61 62 // this flag is set to true after EOF has reached 63 private boolean reachEOF = false; 64 65 /** 66 * Check to make sure that this stream has not been closed 67 */ ensureOpen()68 private void ensureOpen() throws IOException { 69 if (closed) { 70 throw new IOException("Stream closed"); 71 } 72 } 73 74 75 /** 76 * Creates a new input stream with the specified decompressor and 77 * buffer size. 78 * @param in the input stream 79 * @param inf the decompressor ("inflater") 80 * @param size the input buffer size 81 * @exception IllegalArgumentException if {@code size <= 0} 82 */ InflaterInputStream(InputStream in, Inflater inf, int size)83 public InflaterInputStream(InputStream in, Inflater inf, int size) { 84 super(in); 85 if (in == null || inf == null) { 86 throw new NullPointerException(); 87 } else if (size <= 0) { 88 throw new IllegalArgumentException("buffer size <= 0"); 89 } 90 this.inf = inf; 91 buf = new byte[size]; 92 } 93 94 /** 95 * Creates a new input stream with the specified decompressor and a 96 * default buffer size. 97 * @param in the input stream 98 * @param inf the decompressor ("inflater") 99 */ InflaterInputStream(InputStream in, Inflater inf)100 public InflaterInputStream(InputStream in, Inflater inf) { 101 this(in, inf, 512); 102 } 103 104 // Android-changed: Unconditionally close external inflaters (b/26462400) 105 // boolean usesDefaultInflater = false; 106 107 /** 108 * Creates a new input stream with a default decompressor and buffer size. 109 * @param in the input stream 110 */ InflaterInputStream(InputStream in)111 public InflaterInputStream(InputStream in) { 112 this(in, new Inflater()); 113 // Android-changed: Unconditionally close external inflaters (b/26462400) 114 // usesDefaultInflater = true; 115 } 116 117 private byte[] singleByteBuf = new byte[1]; 118 119 /** 120 * Reads a byte of uncompressed data. This method will block until 121 * enough input is available for decompression. 122 * @return the byte read, or -1 if end of compressed input is reached 123 * @exception IOException if an I/O error has occurred 124 */ read()125 public int read() throws IOException { 126 ensureOpen(); 127 return read(singleByteBuf, 0, 1) == -1 ? -1 : Byte.toUnsignedInt(singleByteBuf[0]); 128 } 129 130 /** 131 * Reads uncompressed data into an array of bytes. If <code>len</code> is not 132 * zero, the method will block until some input can be decompressed; otherwise, 133 * no bytes are read and <code>0</code> is returned. 134 * @param b the buffer into which the data is read 135 * @param off the start offset in the destination array <code>b</code> 136 * @param len the maximum number of bytes read 137 * @return the actual number of bytes read, or -1 if the end of the 138 * compressed input is reached or a preset dictionary is needed 139 * @exception NullPointerException If <code>b</code> is <code>null</code>. 140 * @exception IndexOutOfBoundsException If <code>off</code> is negative, 141 * <code>len</code> is negative, or <code>len</code> is greater than 142 * <code>b.length - off</code> 143 * @exception ZipException if a ZIP format error has occurred 144 * @exception IOException if an I/O error has occurred 145 */ read(byte[] b, int off, int len)146 public int read(byte[] b, int off, int len) throws IOException { 147 ensureOpen(); 148 if (b == null) { 149 throw new NullPointerException(); 150 } else if (off < 0 || len < 0 || len > b.length - off) { 151 throw new IndexOutOfBoundsException(); 152 } else if (len == 0) { 153 return 0; 154 } 155 try { 156 int n; 157 while ((n = inf.inflate(b, off, len)) == 0) { 158 if (inf.finished() || inf.needsDictionary()) { 159 reachEOF = true; 160 return -1; 161 } 162 if (inf.needsInput()) { 163 fill(); 164 } 165 } 166 167 // Android-changed: Eagerly set reachEOF. 168 if (inf.finished()) { 169 reachEOF = true; 170 } 171 172 return n; 173 } catch (DataFormatException e) { 174 String s = e.getMessage(); 175 throw new ZipException(s != null ? s : "Invalid ZLIB data format"); 176 } 177 } 178 179 /** 180 * Returns 0 after EOF has been reached, otherwise always return 1. 181 * <p> 182 * Programs should not count on this method to return the actual number 183 * of bytes that could be read without blocking. 184 * 185 * @return 1 before EOF and 0 after EOF. 186 * @exception IOException if an I/O error occurs. 187 * 188 */ available()189 public int available() throws IOException { 190 ensureOpen(); 191 if (reachEOF) { 192 return 0; 193 } else { 194 return 1; 195 } 196 } 197 198 private byte[] b = new byte[512]; 199 200 /** 201 * Skips specified number of bytes of uncompressed data. 202 * @param n the number of bytes to skip 203 * @return the actual number of bytes skipped. 204 * @exception IOException if an I/O error has occurred 205 * @exception IllegalArgumentException if {@code n < 0} 206 */ skip(long n)207 public long skip(long n) throws IOException { 208 if (n < 0) { 209 throw new IllegalArgumentException("negative skip length"); 210 } 211 ensureOpen(); 212 int max = (int)Math.min(n, Integer.MAX_VALUE); 213 int total = 0; 214 while (total < max) { 215 int len = max - total; 216 if (len > b.length) { 217 len = b.length; 218 } 219 len = read(b, 0, len); 220 if (len == -1) { 221 reachEOF = true; 222 break; 223 } 224 total += len; 225 } 226 return total; 227 } 228 229 /** 230 * Closes this input stream and releases any system resources associated 231 * with the stream. 232 * @exception IOException if an I/O error has occurred 233 */ close()234 public void close() throws IOException { 235 if (!closed) { 236 // Android-changed: Unconditionally close external inflaters (b/26462400) 237 //if (usesDefaultInflater) 238 inf.end(); 239 in.close(); 240 closed = true; 241 } 242 } 243 244 /** 245 * Fills input buffer with more data to decompress. 246 * @exception IOException if an I/O error has occurred 247 */ fill()248 protected void fill() throws IOException { 249 ensureOpen(); 250 len = in.read(buf, 0, buf.length); 251 if (len == -1) { 252 throw new EOFException("Unexpected end of ZLIB input stream"); 253 } 254 inf.setInput(buf, 0, len); 255 } 256 257 /** 258 * Tests if this input stream supports the <code>mark</code> and 259 * <code>reset</code> methods. The <code>markSupported</code> 260 * method of <code>InflaterInputStream</code> returns 261 * <code>false</code>. 262 * 263 * @return a <code>boolean</code> indicating if this stream type supports 264 * the <code>mark</code> and <code>reset</code> methods. 265 * @see java.io.InputStream#mark(int) 266 * @see java.io.InputStream#reset() 267 */ markSupported()268 public boolean markSupported() { 269 return false; 270 } 271 272 /** 273 * Marks the current position in this input stream. 274 * 275 * <p> The <code>mark</code> method of <code>InflaterInputStream</code> 276 * does nothing. 277 * 278 * @param readlimit the maximum limit of bytes that can be read before 279 * the mark position becomes invalid. 280 * @see java.io.InputStream#reset() 281 */ mark(int readlimit)282 public synchronized void mark(int readlimit) { 283 } 284 285 /** 286 * Repositions this stream to the position at the time the 287 * <code>mark</code> method was last called on this input stream. 288 * 289 * <p> The method <code>reset</code> for class 290 * <code>InflaterInputStream</code> does nothing except throw an 291 * <code>IOException</code>. 292 * 293 * @exception IOException if this method is invoked. 294 * @see java.io.InputStream#mark(int) 295 * @see java.io.IOException 296 */ reset()297 public synchronized void reset() throws IOException { 298 throw new IOException("mark/reset not supported"); 299 } 300 } 301