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