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 * @since 1.1 42 */ 43 public 44 class InflaterInputStream extends FilterInputStream { 45 /** 46 * Decompressor for this stream. 47 */ 48 protected Inflater inf; 49 50 /** 51 * Input buffer for decompression. 52 */ 53 protected byte[] buf; 54 55 /** 56 * Length of input buffer. 57 */ 58 protected int len; 59 60 // Android-changed: Make closed accessible to subclasses. 61 // This was made protected because it needed to be accessed by 62 // StrictJarFile.ZipInflaterInputStream. Unfortunately, it was not marked as @hide and so it 63 // inadvertently became part of the public API. It will be marked as @removed to remove it from 64 // the public API in a future release of Android. See http://b/111592689 for more information. 65 // private boolean closed = false; 66 /** 67 * Indicates whether the {@link #close()} method has been called, internal use only. 68 * 69 * @deprecated This field will be removed from a future version of Android and should not be 70 * used. Subclasses that access this field need to be modified to keep track of their own 71 * closed state by overriding close(). 72 */ 73 @Deprecated 74 protected boolean closed = false; 75 76 // this flag is set to true after EOF has reached 77 private boolean reachEOF = false; 78 79 /** 80 * Check to make sure that this stream has not been closed 81 */ ensureOpen()82 private void ensureOpen() throws IOException { 83 if (closed) { 84 throw new IOException("Stream closed"); 85 } 86 } 87 88 89 /** 90 * Creates a new input stream with the specified decompressor and 91 * buffer size. 92 * @param in the input stream 93 * @param inf the decompressor ("inflater") 94 * @param size the input buffer size 95 * @exception IllegalArgumentException if {@code size <= 0} 96 */ InflaterInputStream(InputStream in, Inflater inf, int size)97 public InflaterInputStream(InputStream in, Inflater inf, int size) { 98 super(in); 99 if (in == null || inf == null) { 100 throw new NullPointerException(); 101 } else if (size <= 0) { 102 throw new IllegalArgumentException("buffer size <= 0"); 103 } 104 this.inf = inf; 105 buf = new byte[size]; 106 } 107 108 /** 109 * Creates a new input stream with the specified decompressor and a 110 * default buffer size. 111 * @param in the input stream 112 * @param inf the decompressor ("inflater") 113 */ InflaterInputStream(InputStream in, Inflater inf)114 public InflaterInputStream(InputStream in, Inflater inf) { 115 this(in, inf, 512); 116 } 117 118 // Android-changed: Unconditionally close external inflaters (b/26462400) 119 // See http://b/111630946 for more details. 120 // boolean usesDefaultInflater = false; 121 122 /** 123 * Creates a new input stream with a default decompressor and buffer size. 124 * @param in the input stream 125 */ InflaterInputStream(InputStream in)126 public InflaterInputStream(InputStream in) { 127 this(in, new Inflater()); 128 // Android-changed: Unconditionally close external inflaters (b/26462400) 129 // usesDefaultInflater = true; 130 } 131 132 private byte[] singleByteBuf = new byte[1]; 133 134 /** 135 * Reads a byte of uncompressed data. This method will block until 136 * enough input is available for decompression. 137 * @return the byte read, or -1 if end of compressed input is reached 138 * @exception IOException if an I/O error has occurred 139 */ read()140 public int read() throws IOException { 141 ensureOpen(); 142 return read(singleByteBuf, 0, 1) == -1 ? -1 : Byte.toUnsignedInt(singleByteBuf[0]); 143 } 144 145 /** 146 * Reads uncompressed data into an array of bytes. If <code>len</code> is not 147 * zero, the method will block until some input can be decompressed; otherwise, 148 * no bytes are read and <code>0</code> is returned. 149 * @param b the buffer into which the data is read 150 * @param off the start offset in the destination array <code>b</code> 151 * @param len the maximum number of bytes read 152 * @return the actual number of bytes read, or -1 if the end of the 153 * compressed input is reached or a preset dictionary is needed 154 * @exception NullPointerException If <code>b</code> is <code>null</code>. 155 * @exception IndexOutOfBoundsException If <code>off</code> is negative, 156 * <code>len</code> is negative, or <code>len</code> is greater than 157 * <code>b.length - off</code> 158 * @exception ZipException if a ZIP format error has occurred 159 * @exception IOException if an I/O error has occurred 160 */ read(byte[] b, int off, int len)161 public int read(byte[] b, int off, int len) throws IOException { 162 ensureOpen(); 163 if (b == null) { 164 throw new NullPointerException(); 165 } else if (off < 0 || len < 0 || len > b.length - off) { 166 throw new IndexOutOfBoundsException(); 167 } else if (len == 0) { 168 return 0; 169 } 170 try { 171 int n; 172 while ((n = inf.inflate(b, off, len)) == 0) { 173 if (inf.finished() || inf.needsDictionary()) { 174 reachEOF = true; 175 return -1; 176 } 177 if (inf.needsInput()) { 178 fill(); 179 } 180 } 181 return n; 182 } catch (DataFormatException e) { 183 String s = e.getMessage(); 184 throw new ZipException(s != null ? s : "Invalid ZLIB data format"); 185 } 186 } 187 188 /** 189 * Returns 0 after EOF has been reached, otherwise always return 1. 190 * <p> 191 * Programs should not count on this method to return the actual number 192 * of bytes that could be read without blocking. 193 * 194 * @return 1 before EOF and 0 after EOF. 195 * @exception IOException if an I/O error occurs. 196 * 197 */ available()198 public int available() throws IOException { 199 ensureOpen(); 200 if (reachEOF) { 201 return 0; 202 } else if (inf.finished()) { 203 // the end of the compressed data stream has been reached 204 reachEOF = true; 205 return 0; 206 } else { 207 return 1; 208 } 209 } 210 211 private byte[] b = new byte[512]; 212 213 /** 214 * Skips specified number of bytes of uncompressed data. 215 * @param n the number of bytes to skip 216 * @return the actual number of bytes skipped. 217 * @exception IOException if an I/O error has occurred 218 * @exception IllegalArgumentException if {@code n < 0} 219 */ skip(long n)220 public long skip(long n) throws IOException { 221 if (n < 0) { 222 throw new IllegalArgumentException("negative skip length"); 223 } 224 ensureOpen(); 225 int max = (int)Math.min(n, Integer.MAX_VALUE); 226 int total = 0; 227 while (total < max) { 228 int len = max - total; 229 if (len > b.length) { 230 len = b.length; 231 } 232 len = read(b, 0, len); 233 if (len == -1) { 234 reachEOF = true; 235 break; 236 } 237 total += len; 238 } 239 return total; 240 } 241 242 /** 243 * Closes this input stream and releases any system resources associated 244 * with the stream. 245 * @exception IOException if an I/O error has occurred 246 */ close()247 public void close() throws IOException { 248 if (!closed) { 249 // Android-changed: Unconditionally close external inflaters (b/26462400) 250 //if (usesDefaultInflater) 251 inf.end(); 252 in.close(); 253 closed = true; 254 } 255 } 256 257 /** 258 * Fills input buffer with more data to decompress. 259 * @exception IOException if an I/O error has occurred 260 */ fill()261 protected void fill() throws IOException { 262 ensureOpen(); 263 len = in.read(buf, 0, buf.length); 264 if (len == -1) { 265 throw new EOFException("Unexpected end of ZLIB input stream"); 266 } 267 inf.setInput(buf, 0, len); 268 } 269 270 /** 271 * Tests if this input stream supports the <code>mark</code> and 272 * <code>reset</code> methods. The <code>markSupported</code> 273 * method of <code>InflaterInputStream</code> returns 274 * <code>false</code>. 275 * 276 * @return a <code>boolean</code> indicating if this stream type supports 277 * the <code>mark</code> and <code>reset</code> methods. 278 * @see java.io.InputStream#mark(int) 279 * @see java.io.InputStream#reset() 280 */ markSupported()281 public boolean markSupported() { 282 return false; 283 } 284 285 /** 286 * Marks the current position in this input stream. 287 * 288 * <p> The <code>mark</code> method of <code>InflaterInputStream</code> 289 * does nothing. 290 * 291 * @param readlimit the maximum limit of bytes that can be read before 292 * the mark position becomes invalid. 293 * @see java.io.InputStream#reset() 294 */ mark(int readlimit)295 public synchronized void mark(int readlimit) { 296 } 297 298 /** 299 * Repositions this stream to the position at the time the 300 * <code>mark</code> method was last called on this input stream. 301 * 302 * <p> The method <code>reset</code> for class 303 * <code>InflaterInputStream</code> does nothing except throw an 304 * <code>IOException</code>. 305 * 306 * @exception IOException if this method is invoked. 307 * @see java.io.InputStream#mark(int) 308 * @see java.io.IOException 309 */ reset()310 public synchronized void reset() throws IOException { 311 throw new IOException("mark/reset not supported"); 312 } 313 } 314