1. /*
  2. * @(#)InflaterInputStream.java 1.24 01/11/29
  3. *
  4. * Copyright 2002 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.24, 11/29/01
  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. /**
  74. * Creates a new input stream with a default decompressor and buffer size.
  75. */
  76. public InflaterInputStream(InputStream in) {
  77. this(in, new Inflater());
  78. }
  79. /**
  80. * Reads a byte of uncompressed data. This method will block until
  81. * enough input is available for decompression.
  82. * @return the byte read, or -1 if end of compressed input is reached
  83. * @exception IOException if an I/O error has occurred
  84. */
  85. public int read() throws IOException {
  86. ensureOpen();
  87. byte[] b = new byte[1];
  88. return read(b, 0, 1) == -1 ? -1 : b[0] & 0xff;
  89. }
  90. /**
  91. * Reads uncompressed data into an array of bytes. This method will
  92. * block until some input can be decompressed.
  93. * @param b the buffer into which the data is read
  94. * @param off the start offset of the data
  95. * @param len the maximum number of bytes read
  96. * @return the actual number of bytes read, or -1 if the end of the
  97. * compressed input is reached or a preset dictionary is needed
  98. * @exception ZipException if a ZIP format error has occurred
  99. * @exception IOException if an I/O error has occurred
  100. */
  101. public int read(byte[] b, int off, int len) throws IOException {
  102. ensureOpen();
  103. if (len <= 0) {
  104. return 0;
  105. }
  106. try {
  107. int n;
  108. while ((n = inf.inflate(b, off, len)) == 0) {
  109. if (inf.finished() || inf.needsDictionary()) {
  110. reachEOF = true;
  111. return -1;
  112. }
  113. if (inf.needsInput()) {
  114. fill();
  115. }
  116. }
  117. return n;
  118. } catch (DataFormatException e) {
  119. String s = e.getMessage();
  120. throw new ZipException(s != null ? s : "Invalid ZLIB data format");
  121. }
  122. }
  123. /**
  124. * Returns 0 after EOF has reached, otherwise always return 1.
  125. * <p>
  126. * Programs should not count on this method to return the actual number
  127. * of bytes that could be read without blocking.
  128. *
  129. * @return 1 before EOF and 0 after EOF.
  130. * @exception IOException if an I/O error occurs.
  131. *
  132. */
  133. public int available() throws IOException {
  134. ensureOpen();
  135. if (reachEOF) {
  136. return 0;
  137. } else {
  138. return 1;
  139. }
  140. }
  141. /**
  142. * Skips specified number of bytes of uncompressed data.
  143. * @param n the number of bytes to skip
  144. * @return the actual number of bytes skipped.
  145. * @exception IOException if an I/O error has occurred
  146. * @exception IllegalArgumentException if n < 0
  147. */
  148. public long skip(long n) throws IOException {
  149. if (n < 0) {
  150. throw new IllegalArgumentException("negative skip length");
  151. }
  152. ensureOpen();
  153. int max = (int)Math.min(n, Integer.MAX_VALUE);
  154. int total = 0;
  155. byte[] b = new byte[512];
  156. while (total < max) {
  157. int len = max - total;
  158. if (len > b.length) {
  159. len = b.length;
  160. }
  161. len = read(b, 0, len);
  162. if (len == -1) {
  163. reachEOF = true;
  164. break;
  165. }
  166. total += len;
  167. }
  168. return total;
  169. }
  170. /**
  171. * Closes the input stream.
  172. * @exception IOException if an I/O error has occurred
  173. */
  174. public void close() throws IOException {
  175. in.close();
  176. closed = true;
  177. }
  178. /**
  179. * Fills input buffer with more data to decompress.
  180. * @exception IOException if an I/O error has occurred
  181. */
  182. protected void fill() throws IOException {
  183. ensureOpen();
  184. len = in.read(buf, 0, buf.length);
  185. if (len == -1) {
  186. throw new EOFException("Unexpected end of ZLIB input stream");
  187. }
  188. inf.setInput(buf, 0, len);
  189. }
  190. }