1. /*
  2. * @(#)InflaterInputStream.java 1.32 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.zip;
  8. import java.io.FilterInputStream;
  9. import java.io.InputStream;
  10. import java.io.IOException;
  11. import java.io.EOFException;
  12. /**
  13. * This class implements a stream filter for uncompressing data in the
  14. * "deflate" compression format. It is also used as the basis for other
  15. * decompression filters, such as GZIPInputStream.
  16. *
  17. * @see Inflater
  18. * @version 1.32, 01/23/03
  19. * @author David Connelly
  20. */
  21. public
  22. class InflaterInputStream extends FilterInputStream {
  23. /**
  24. * Decompressor for this stream.
  25. */
  26. protected Inflater inf;
  27. /**
  28. * Input buffer for decompression.
  29. */
  30. protected byte[] buf;
  31. /**
  32. * Length of input buffer.
  33. */
  34. protected int len;
  35. private boolean closed = false;
  36. // this flag is set to true after EOF has reached
  37. private boolean reachEOF = false;
  38. /**
  39. * Check to make sure that this stream has not been closed
  40. */
  41. private void ensureOpen() throws IOException {
  42. if (closed) {
  43. throw new IOException("Stream closed");
  44. }
  45. }
  46. /**
  47. * Creates a new input stream with the specified decompressor and
  48. * buffer size.
  49. * @param in the input stream
  50. * @param inf the decompressor ("inflater")
  51. * @param size the input buffer size
  52. * @exception IllegalArgumentException if size is <= 0
  53. */
  54. public InflaterInputStream(InputStream in, Inflater inf, int size) {
  55. super(in);
  56. if (in == null || inf == null) {
  57. throw new NullPointerException();
  58. } else if (size <= 0) {
  59. throw new IllegalArgumentException("buffer size <= 0");
  60. }
  61. this.inf = inf;
  62. buf = new byte[size];
  63. }
  64. /**
  65. * Creates a new input stream with the specified decompressor and a
  66. * default buffer size.
  67. * @param in the input stream
  68. * @param inf the decompressor ("inflater")
  69. */
  70. public InflaterInputStream(InputStream in, Inflater inf) {
  71. this(in, inf, 512);
  72. }
  73. boolean usesDefaultInflater = false;
  74. /**
  75. * Creates a new input stream with a default decompressor and buffer size.
  76. * @param in the input stream
  77. */
  78. public InflaterInputStream(InputStream in) {
  79. this(in, new Inflater());
  80. usesDefaultInflater = true;
  81. }
  82. private byte[] singleByteBuf = new byte[1];
  83. /**
  84. * Reads a byte of uncompressed data. This method will block until
  85. * enough input is available for decompression.
  86. * @return the byte read, or -1 if end of compressed input is reached
  87. * @exception IOException if an I/O error has occurred
  88. */
  89. public int read() throws IOException {
  90. ensureOpen();
  91. return read(singleByteBuf, 0, 1) == -1 ? -1 : singleByteBuf[0] & 0xff;
  92. }
  93. /**
  94. * Reads uncompressed data into an array of bytes. This method will
  95. * block until some input can be decompressed.
  96. * @param b the buffer into which the data is read
  97. * @param off the start offset of the data
  98. * @param len the maximum number of bytes read
  99. * @return the actual number of bytes read, or -1 if the end of the
  100. * compressed input is reached or a preset dictionary is needed
  101. * @exception ZipException if a ZIP format error has occurred
  102. * @exception IOException if an I/O error has occurred
  103. */
  104. public int read(byte[] b, int off, int len) throws IOException {
  105. ensureOpen();
  106. if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
  107. throw new IndexOutOfBoundsException();
  108. } else if (len == 0) {
  109. return 0;
  110. }
  111. try {
  112. int n;
  113. while ((n = inf.inflate(b, off, len)) == 0) {
  114. if (inf.finished() || inf.needsDictionary()) {
  115. reachEOF = true;
  116. return -1;
  117. }
  118. if (inf.needsInput()) {
  119. fill();
  120. }
  121. }
  122. return n;
  123. } catch (DataFormatException e) {
  124. String s = e.getMessage();
  125. throw new ZipException(s != null ? s : "Invalid ZLIB data format");
  126. }
  127. }
  128. /**
  129. * Returns 0 after EOF has reached, otherwise always return 1.
  130. * <p>
  131. * Programs should not count on this method to return the actual number
  132. * of bytes that could be read without blocking.
  133. *
  134. * @return 1 before EOF and 0 after EOF.
  135. * @exception IOException if an I/O error occurs.
  136. *
  137. */
  138. public int available() throws IOException {
  139. ensureOpen();
  140. if (reachEOF) {
  141. return 0;
  142. } else {
  143. return 1;
  144. }
  145. }
  146. private byte[] b = new byte[512];
  147. /**
  148. * Skips specified number of bytes of uncompressed data.
  149. * @param n the number of bytes to skip
  150. * @return the actual number of bytes skipped.
  151. * @exception IOException if an I/O error has occurred
  152. * @exception IllegalArgumentException if n < 0
  153. */
  154. public long skip(long n) throws IOException {
  155. if (n < 0) {
  156. throw new IllegalArgumentException("negative skip length");
  157. }
  158. ensureOpen();
  159. int max = (int)Math.min(n, Integer.MAX_VALUE);
  160. int total = 0;
  161. while (total < max) {
  162. int len = max - total;
  163. if (len > b.length) {
  164. len = b.length;
  165. }
  166. len = read(b, 0, len);
  167. if (len == -1) {
  168. reachEOF = true;
  169. break;
  170. }
  171. total += len;
  172. }
  173. return total;
  174. }
  175. /**
  176. * Closes the input stream.
  177. * @exception IOException if an I/O error has occurred
  178. */
  179. public void close() throws IOException {
  180. if (!closed) {
  181. if (usesDefaultInflater)
  182. inf.end();
  183. in.close();
  184. closed = true;
  185. }
  186. }
  187. /**
  188. * Fills input buffer with more data to decompress.
  189. * @exception IOException if an I/O error has occurred
  190. */
  191. protected void fill() throws IOException {
  192. ensureOpen();
  193. len = in.read(buf, 0, buf.length);
  194. if (len == -1) {
  195. throw new EOFException("Unexpected end of ZLIB input stream");
  196. }
  197. inf.setInput(buf, 0, len);
  198. }
  199. }