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