Deflater.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* Deflater.java - Compress a data stream
  2. Copyright (C) 1999, 2000, 2001, 2004, 2006 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 gnu.gcj.RawData;
  33. /**
  34. * This is the Deflater class. The deflater class compresses input
  35. * with the deflate algorithm described in RFC 1951. It has several
  36. * compression levels and three different strategies described below.
  37. *
  38. * This class is <i>not</i> thread safe. This is inherent in the API, due
  39. * to the split of deflate and setInput.
  40. *
  41. * @author Jochen Hoenicke
  42. * @author Tom Tromey
  43. */
  44. public class Deflater
  45. {
  46. /**
  47. * The best and slowest compression level. This tries to find very
  48. * long and distant string repetitions.
  49. */
  50. public static final int BEST_COMPRESSION = 9;
  51. /**
  52. * The worst but fastest compression level.
  53. */
  54. public static final int BEST_SPEED = 1;
  55. /**
  56. * The default compression level.
  57. */
  58. public static final int DEFAULT_COMPRESSION = -1;
  59. /**
  60. * This level won't compress at all but output uncompressed blocks.
  61. */
  62. public static final int NO_COMPRESSION = 0;
  63. /**
  64. * The default strategy.
  65. */
  66. public static final int DEFAULT_STRATEGY = 0;
  67. /**
  68. * This strategy will only allow longer string repetitions. It is
  69. * useful for random data with a small character set.
  70. */
  71. public static final int FILTERED = 1;
  72. /**
  73. * This strategy will not look for string repetitions at all. It
  74. * only encodes with Huffman trees (which means, that more common
  75. * characters get a smaller encoding.
  76. */
  77. public static final int HUFFMAN_ONLY = 2;
  78. /**
  79. * The compression method. This is the only method supported so far.
  80. * There is no need to use this constant at all.
  81. */
  82. public static final int DEFLATED = 8;
  83. /** Compression level. */
  84. private int level;
  85. /** Compression strategy. */
  86. private int strategy;
  87. /** The zlib stream. */
  88. private RawData zstream;
  89. /** True if finished. */
  90. private boolean is_finished;
  91. /** `Flush' flag to pass to next call to deflate. */
  92. private int flush_flag;
  93. /**
  94. * Creates a new deflater with default compression level.
  95. */
  96. public Deflater()
  97. {
  98. this(DEFAULT_COMPRESSION, false);
  99. }
  100. /**
  101. * Creates a new deflater with given compression level.
  102. * @param lvl the compression level, a value between NO_COMPRESSION
  103. * and BEST_COMPRESSION, or DEFAULT_COMPRESSION.
  104. * @exception IllegalArgumentException if lvl is out of range.
  105. */
  106. public Deflater(int lvl)
  107. {
  108. this(lvl, false);
  109. }
  110. /**
  111. * Creates a new deflater with given compression level.
  112. * @param lvl the compression level, a value between NO_COMPRESSION
  113. * and BEST_COMPRESSION.
  114. * @param nowrap true, iff we should suppress the deflate header at the
  115. * beginning and the adler checksum at the end of the output. This is
  116. * useful for the GZIP format.
  117. * @exception IllegalArgumentException if lvl is out of range.
  118. */
  119. public Deflater(int lvl, boolean noHeader)
  120. {
  121. this.strategy = DEFAULT_STRATEGY;
  122. init(lvl, noHeader);
  123. setLevel(lvl);
  124. }
  125. private native void init(int level, boolean noHeader);
  126. private native void update();
  127. /**
  128. * Resets the deflater. The deflater acts afterwards as if it was
  129. * just created with the same compression level and strategy as it
  130. * had before.
  131. */
  132. public native void reset();
  133. /**
  134. * Frees all objects allocated by the compressor. There's no
  135. * reason to call this, since you can just rely on garbage
  136. * collection. Exists only for compatibility against Sun's JDK,
  137. * where the compressor allocates native memory.
  138. * If you call any method (even reset) afterwards the behaviour is
  139. * <i>undefined</i>.
  140. * @deprecated Just clear all references to deflater instead.
  141. */
  142. public native void end();
  143. /**
  144. * Gets the current adler checksum of the data that was processed so
  145. * far.
  146. */
  147. public native int getAdler();
  148. /**
  149. * Gets the number of input bytes processed so far.
  150. */
  151. @Deprecated
  152. public int getTotalIn()
  153. {
  154. return (int) getBytesRead();
  155. }
  156. /**
  157. * Gets the number of input bytes processed so far.
  158. * @since 1.5
  159. */
  160. public native long getBytesRead();
  161. /**
  162. * Gets the number of output bytes so far.
  163. */
  164. @Deprecated
  165. public int getTotalOut()
  166. {
  167. return (int) getBytesWritten();
  168. }
  169. /**
  170. * Gets the number of output bytes so far.
  171. * @since 1.5
  172. */
  173. public native long getBytesWritten();
  174. /**
  175. * Finalizes this object.
  176. */
  177. protected void finalize()
  178. {
  179. end();
  180. }
  181. /**
  182. * Finishes the deflater with the current input block. It is an error
  183. * to give more input after this method was called. This method must
  184. * be called to force all bytes to be flushed.
  185. */
  186. public native void finish();
  187. /**
  188. * Returns true iff the stream was finished and no more output bytes
  189. * are available.
  190. */
  191. public synchronized boolean finished()
  192. {
  193. return is_finished;
  194. }
  195. /**
  196. * Returns true, if the input buffer is empty.
  197. * You should then call setInput(). <br>
  198. *
  199. * <em>NOTE</em>: This method can also return true when the stream
  200. * was finished.
  201. */
  202. public native boolean needsInput();
  203. /**
  204. * Sets the data which should be compressed next. This should be only
  205. * called when needsInput indicates that more input is needed.
  206. * If you call setInput when needsInput() returns false, the
  207. * previous input that is still pending will be thrown away.
  208. * The given byte array should not be changed, before needsInput() returns
  209. * true again.
  210. * This call is equivalent to <code>setInput(input, 0, input.length)</code>.
  211. * @param input the buffer containing the input data.
  212. * @exception IllegalStateException if the buffer was finished() or ended().
  213. */
  214. public void setInput(byte[] input)
  215. {
  216. setInput(input, 0, input.length);
  217. }
  218. /**
  219. * Sets the data which should be compressed next. This should be
  220. * only called when needsInput indicates that more input is needed.
  221. * The given byte array should not be changed, before needsInput() returns
  222. * true again.
  223. * @param input the buffer containing the input data.
  224. * @param off the start of the data.
  225. * @param len the length of the data.
  226. * @exception IllegalStateException if the buffer was finished() or ended()
  227. * or if previous input is still pending.
  228. */
  229. public native void setInput(byte[] input, int off, int len);
  230. /**
  231. * Sets the compression level. There is no guarantee of the exact
  232. * position of the change, but if you call this when needsInput is
  233. * true the change of compression level will occur somewhere near
  234. * before the end of the so far given input.
  235. * @param lvl the new compression level.
  236. */
  237. public synchronized void setLevel(int lvl)
  238. {
  239. if (lvl != -1 && (lvl < 0 || lvl > 9))
  240. throw new IllegalArgumentException();
  241. level = (lvl == -1) ? 6 : lvl;
  242. update();
  243. }
  244. /**
  245. * Sets the compression strategy. Strategy is one of
  246. * DEFAULT_STRATEGY, HUFFMAN_ONLY and FILTERED. For the exact
  247. * position where the strategy is changed, the same as for
  248. * setLevel() applies.
  249. * @param stgy the new compression strategy.
  250. */
  251. public synchronized void setStrategy(int stgy)
  252. {
  253. if (stgy != DEFAULT_STRATEGY && stgy != FILTERED
  254. && stgy != HUFFMAN_ONLY)
  255. throw new IllegalArgumentException();
  256. strategy = stgy;
  257. update();
  258. }
  259. /**
  260. * Deflates the current input block to the given array. It returns
  261. * the number of bytes compressed, or 0 if either
  262. * needsInput() or finished() returns true or length is zero.
  263. * @param output the buffer where to write the compressed data.
  264. */
  265. public int deflate(byte[] output)
  266. {
  267. return deflate(output, 0, output.length);
  268. }
  269. /**
  270. * Deflates the current input block to the given array. It returns
  271. * the number of bytes compressed, or 0 if either
  272. * needsInput() or finished() returns true or length is zero.
  273. * @param output the buffer where to write the compressed data.
  274. * @param offset the offset into the output array.
  275. * @param length the maximum number of bytes that may be written.
  276. * @exception IllegalStateException if end() was called.
  277. * @exception IndexOutOfBoundsException if offset and/or length
  278. * don't match the array length.
  279. */
  280. public native int deflate(byte[] output, int off, int len);
  281. /**
  282. * Sets the dictionary which should be used in the deflate process.
  283. * This call is equivalent to <code>setDictionary(dict, 0,
  284. * dict.length)</code>.
  285. * @param dict the dictionary.
  286. * @exception IllegalStateException if setInput () or deflate ()
  287. * were already called or another dictionary was already set.
  288. */
  289. public void setDictionary(byte[] dict)
  290. {
  291. setDictionary(dict, 0, dict.length);
  292. }
  293. /**
  294. * Sets the dictionary which should be used in the deflate process.
  295. * The dictionary should be a byte array containing strings that are
  296. * likely to occur in the data which should be compressed. The
  297. * dictionary is not stored in the compressed output, only a
  298. * checksum. To decompress the output you need to supply the same
  299. * dictionary again.
  300. * @param dict the dictionary.
  301. * @param offset an offset into the dictionary.
  302. * @param length the length of the dictionary.
  303. * @exception IllegalStateException if setInput () or deflate () were
  304. * already called or another dictionary was already set.
  305. */
  306. public native void setDictionary(byte[] buf, int off, int len);
  307. // Classpath's compression library supports flushing, but we
  308. // don't. So this is a no-op here.
  309. void flush()
  310. {
  311. }
  312. }