InflaterInputStream.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* InflaterInputStream.java - Input stream filter for decompressing
  2. Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2006
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util.zip;
  33. import java.io.FilterInputStream;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. /**
  37. * This filter stream is used to decompress data compressed in the "deflate"
  38. * format. The "deflate" format is described in RFC 1951.
  39. *
  40. * This stream may form the basis for other decompression filters, such
  41. * as the <code>GZIPInputStream</code>.
  42. *
  43. * @author John Leuner
  44. * @author Tom Tromey
  45. * @since 1.1
  46. */
  47. public class InflaterInputStream extends FilterInputStream
  48. {
  49. /**
  50. * Decompressor for this filter
  51. */
  52. protected Inflater inf;
  53. /**
  54. * Byte array used as a buffer
  55. */
  56. protected byte[] buf;
  57. /**
  58. * Size of buffer
  59. */
  60. protected int len;
  61. // We just use this if we are decoding one byte at a time with the
  62. // read() call.
  63. private byte[] onebytebuffer = new byte[1];
  64. /**
  65. * Create an InflaterInputStream with the default decompresseor
  66. * and a default buffer size.
  67. *
  68. * @param in the InputStream to read bytes from
  69. */
  70. public InflaterInputStream(InputStream in)
  71. {
  72. this(in, new Inflater(), 4096);
  73. }
  74. /**
  75. * Create an InflaterInputStream with the specified decompresseor
  76. * and a default buffer size.
  77. *
  78. * @param in the InputStream to read bytes from
  79. * @param inf the decompressor used to decompress data read from in
  80. */
  81. public InflaterInputStream(InputStream in, Inflater inf)
  82. {
  83. this(in, inf, 4096);
  84. }
  85. /**
  86. * Create an InflaterInputStream with the specified decompresseor
  87. * and a specified buffer size.
  88. *
  89. * @param in the InputStream to read bytes from
  90. * @param inf the decompressor used to decompress data read from in
  91. * @param size size of the buffer to use
  92. */
  93. public InflaterInputStream(InputStream in, Inflater inf, int size)
  94. {
  95. super(in);
  96. if (in == null)
  97. throw new NullPointerException("in may not be null");
  98. if (inf == null)
  99. throw new NullPointerException("inf may not be null");
  100. if (size < 0)
  101. throw new IllegalArgumentException("size may not be negative");
  102. this.inf = inf;
  103. this.buf = new byte [size];
  104. }
  105. /**
  106. * Returns 0 once the end of the stream (EOF) has been reached.
  107. * Otherwise returns 1.
  108. */
  109. public int available() throws IOException
  110. {
  111. // According to the JDK 1.2 docs, this should only ever return 0
  112. // or 1 and should not be relied upon by Java programs.
  113. if (inf == null)
  114. throw new IOException("stream closed");
  115. return inf.finished() ? 0 : 1;
  116. }
  117. /**
  118. * Closes the input stream
  119. */
  120. public synchronized void close() throws IOException
  121. {
  122. if (in != null)
  123. in.close();
  124. in = null;
  125. }
  126. /**
  127. * Fills the buffer with more data to decompress.
  128. */
  129. protected void fill() throws IOException
  130. {
  131. if (in == null)
  132. throw new ZipException ("InflaterInputStream is closed");
  133. len = in.read(buf, 0, buf.length);
  134. if (len < 0)
  135. throw new ZipException("Deflated stream ends early.");
  136. inf.setInput(buf, 0, len);
  137. }
  138. /**
  139. * Reads one byte of decompressed data.
  140. *
  141. * The byte is in the lower 8 bits of the int.
  142. */
  143. public int read() throws IOException
  144. {
  145. int nread = read(onebytebuffer, 0, 1);
  146. if (nread > 0)
  147. return onebytebuffer[0] & 0xff;
  148. return -1;
  149. }
  150. /**
  151. * Decompresses data into the byte array
  152. *
  153. * @param b the array to read and decompress data into
  154. * @param off the offset indicating where the data should be placed
  155. * @param len the number of bytes to decompress
  156. */
  157. public int read(byte[] b, int off, int len) throws IOException
  158. {
  159. if (inf == null)
  160. throw new IOException("stream closed");
  161. if (len == 0)
  162. return 0;
  163. if (inf.finished())
  164. return -1;
  165. int count = 0;
  166. while (count == 0)
  167. {
  168. if (inf.needsInput())
  169. fill();
  170. try
  171. {
  172. count = inf.inflate(b, off, len);
  173. if (count == 0)
  174. {
  175. if (this.len == -1)
  176. {
  177. // Couldn't get any more data to feed to the Inflater
  178. return -1;
  179. }
  180. if (inf.needsDictionary())
  181. throw new ZipException("Inflater needs Dictionary");
  182. }
  183. }
  184. catch (DataFormatException dfe)
  185. {
  186. throw new ZipException(dfe.getMessage());
  187. }
  188. }
  189. return count;
  190. }
  191. /**
  192. * Skip specified number of bytes of uncompressed data
  193. *
  194. * @param n number of bytes to skip
  195. */
  196. public long skip(long n) throws IOException
  197. {
  198. if (inf == null)
  199. throw new IOException("stream closed");
  200. if (n < 0)
  201. throw new IllegalArgumentException();
  202. if (n == 0)
  203. return 0;
  204. int buflen = (int) Math.min(n, 2048);
  205. byte[] tmpbuf = new byte[buflen];
  206. long skipped = 0L;
  207. while (n > 0L)
  208. {
  209. int numread = read(tmpbuf, 0, buflen);
  210. if (numread <= 0)
  211. break;
  212. n -= numread;
  213. skipped += numread;
  214. buflen = (int) Math.min(n, 2048);
  215. }
  216. return skipped;
  217. }
  218. public boolean markSupported()
  219. {
  220. return false;
  221. }
  222. public void mark(int readLimit)
  223. {
  224. }
  225. public void reset() throws IOException
  226. {
  227. throw new IOException("reset not supported");
  228. }
  229. }