DeflaterOutputStream.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* DeflaterOutputStream.java - Output filter for compressing.
  2. Copyright (C) 1999, 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util.zip;
  32. import java.io.FilterOutputStream;
  33. import java.io.IOException;
  34. import java.io.OutputStream;
  35. /* Written using on-line Java Platform 1.2 API Specification
  36. * and JCL book.
  37. * Believed complete and correct.
  38. */
  39. /**
  40. * This is a special FilterOutputStream deflating the bytes that are
  41. * written through it. It uses the Deflater for deflating.
  42. *
  43. * A special thing to be noted is that flush() doesn't flush
  44. * everything in Sun's JDK, but it does so in jazzlib. This is because
  45. * Sun's Deflater doesn't have a way to flush() everything, without
  46. * finishing the stream.
  47. *
  48. * @author Tom Tromey, Jochen Hoenicke
  49. * @date Jan 11, 2001
  50. */
  51. public class DeflaterOutputStream extends FilterOutputStream
  52. {
  53. /**
  54. * This buffer is used temporarily to retrieve the bytes from the
  55. * deflater and write them to the underlying output stream.
  56. */
  57. protected byte[] buf;
  58. /**
  59. * The deflater which is used to deflate the stream.
  60. */
  61. protected Deflater def;
  62. /**
  63. * Deflates everything in the def's input buffers. This will call
  64. * <code>def.deflate()</code> until all bytes from the input buffers
  65. * are processed.
  66. */
  67. protected void deflate() throws IOException
  68. {
  69. while (! def.needsInput())
  70. {
  71. int len = def.deflate(buf, 0, buf.length);
  72. // System.err.println("DOS deflated " + len + " out of " + buf.length);
  73. if (len <= 0)
  74. break;
  75. out.write(buf, 0, len);
  76. }
  77. if (! def.needsInput())
  78. throw new InternalError("Can't deflate all input?");
  79. }
  80. /**
  81. * Creates a new DeflaterOutputStream with a default Deflater and
  82. * default buffer size.
  83. * @param out the output stream where deflated output should be written.
  84. */
  85. public DeflaterOutputStream(OutputStream out)
  86. {
  87. this(out, new Deflater(), 4096);
  88. }
  89. /**
  90. * Creates a new DeflaterOutputStream with the given Deflater and
  91. * default buffer size.
  92. * @param out the output stream where deflated output should be written.
  93. * @param defl the underlying deflater.
  94. */
  95. public DeflaterOutputStream(OutputStream out, Deflater defl)
  96. {
  97. this(out, defl, 4096);
  98. }
  99. /**
  100. * Creates a new DeflaterOutputStream with the given Deflater and
  101. * buffer size.
  102. * @param out the output stream where deflated output should be written.
  103. * @param defl the underlying deflater.
  104. * @param bufsize the buffer size.
  105. * @exception IllegalArgumentException if bufsize isn't positive.
  106. */
  107. public DeflaterOutputStream(OutputStream out, Deflater defl, int bufsize)
  108. {
  109. super(out);
  110. if (bufsize <= 0)
  111. throw new IllegalArgumentException("bufsize <= 0");
  112. buf = new byte[bufsize];
  113. def = defl;
  114. }
  115. /**
  116. * Flushes the stream by calling flush() on the deflater and then
  117. * on the underlying stream. This ensures that all bytes are
  118. * flushed. This function doesn't work in Sun's JDK, but only in
  119. * jazzlib.
  120. */
  121. public void flush() throws IOException
  122. {
  123. def.flush();
  124. deflate();
  125. out.flush();
  126. }
  127. /**
  128. * Finishes the stream by calling finish() on the deflater. This
  129. * was the only way to ensure that all bytes are flushed in Sun's
  130. * JDK.
  131. */
  132. public void finish() throws IOException
  133. {
  134. def.finish();
  135. while (! def.finished())
  136. {
  137. int len = def.deflate(buf, 0, buf.length);
  138. if (len <= 0)
  139. break;
  140. out.write(buf, 0, len);
  141. }
  142. if (! def.finished())
  143. throw new InternalError("Can't deflate all input?");
  144. out.flush();
  145. }
  146. /**
  147. * Calls finish() and closes the stream.
  148. */
  149. public void close() throws IOException
  150. {
  151. finish();
  152. out.close();
  153. }
  154. /**
  155. * Writes a single byte to the compressed output stream.
  156. * @param bval the byte value.
  157. */
  158. public void write(int bval) throws IOException
  159. {
  160. byte[] b = new byte[1];
  161. b[0] = (byte) bval;
  162. write(b, 0, 1);
  163. }
  164. /**
  165. * Writes a len bytes from an array to the compressed stream.
  166. * @param buf the byte array.
  167. * @param off the offset into the byte array where to start.
  168. * @param len the number of bytes to write.
  169. */
  170. public void write(byte[] buf, int off, int len) throws IOException
  171. {
  172. def.setInput(buf, off, len);
  173. deflate();
  174. }
  175. }