1 /* Deflater.java - Compress a data stream
2 Copyright (C) 1999, 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.util.zip;
41 * This is the Deflater class. The deflater class compresses input
42 * with the deflate algorithm described in RFC 1951. It has several
43 * compression levels and three different strategies described below.
45 * This class is <i>not</i> thread safe. This is inherent in the API, due
46 * to the split of deflate and setInput.
48 * @author Jochen Hoenicke
54 * The best and slowest compression level. This tries to find very
55 * long and distant string repetitions.
57 public static final int BEST_COMPRESSION = 9;
59 * The worst but fastest compression level.
61 public static final int BEST_SPEED = 1;
63 * The default compression level.
65 public static final int DEFAULT_COMPRESSION = -1;
67 * This level won't compress at all but output uncompressed blocks.
69 public static final int NO_COMPRESSION = 0;
72 * The default strategy.
74 public static final int DEFAULT_STRATEGY = 0;
76 * This strategy will only allow longer string repetitions. It is
77 * useful for random data with a small character set.
79 public static final int FILTERED = 1;
82 * This strategy will not look for string repetitions at all. It
83 * only encodes with Huffman trees (which means, that more common
84 * characters get a smaller encoding.
86 public static final int HUFFMAN_ONLY = 2;
89 * The compression method. This is the only method supported so far.
90 * There is no need to use this constant at all.
92 public static final int DEFLATED = 8;
95 * The Deflater can do the following state transitions:
97 * (1) -> INIT_STATE ----> INIT_FINISHING_STATE ---.
100 * (3)| SETDICT_STATE ---> SETDICT_FINISHING_STATE |(3)
101 * \ | (3) | ,-------'
104 * (1) -> BUSY_STATE ----> FINISHING_STATE
108 * \_____________________________________/
113 * (1) If we should produce a header we start in INIT_STATE, otherwise
114 * we start in BUSY_STATE.
115 * (2) A dictionary may be set only when we are in INIT_STATE, then
116 * we change the state as indicated.
117 * (3) Whether a dictionary is set or not, on the first call of deflate
118 * we change to BUSY_STATE.
119 * (4) -- intentionally left blank -- :)
120 * (5) FINISHING_STATE is entered, when flush() is called to indicate that
121 * there is no more INPUT. There are also states indicating, that
122 * the header wasn't written yet.
123 * (6) FINISHED_STATE is entered, when everything has been flushed to the
124 * internal pending output buffer.
125 * (7) At any time (7)
129 private static final int IS_SETDICT = 0x01;
130 private static final int IS_FLUSHING = 0x04;
131 private static final int IS_FINISHING = 0x08;
133 private static final int INIT_STATE = 0x00;
134 private static final int SETDICT_STATE = 0x01;
135 private static final int INIT_FINISHING_STATE = 0x08;
136 private static final int SETDICT_FINISHING_STATE = 0x09;
137 private static final int BUSY_STATE = 0x10;
138 private static final int FLUSHING_STATE = 0x14;
139 private static final int FINISHING_STATE = 0x1c;
140 private static final int FINISHED_STATE = 0x1e;
141 private static final int CLOSED_STATE = 0x7f;
143 /** Compression level. */
146 /** should we include a header. */
147 private boolean noHeader;
149 /** The current state. */
152 /** The total bytes of output written. */
153 private long totalOut;
155 /** The pending output. */
156 private DeflaterPending pending;
158 /** The deflater engine. */
159 private DeflaterEngine engine;
162 * Creates a new deflater with default compression level.
166 this(DEFAULT_COMPRESSION, false);
170 * Creates a new deflater with given compression level.
171 * @param lvl the compression level, a value between NO_COMPRESSION
172 * and BEST_COMPRESSION, or DEFAULT_COMPRESSION.
173 * @exception IllegalArgumentException if lvl is out of range.
175 public Deflater(int lvl)
181 * Creates a new deflater with given compression level.
182 * @param lvl the compression level, a value between NO_COMPRESSION
183 * and BEST_COMPRESSION.
184 * @param nowrap true, iff we should suppress the deflate header at the
185 * beginning and the adler checksum at the end of the output. This is
186 * useful for the GZIP format.
187 * @exception IllegalArgumentException if lvl is out of range.
189 public Deflater(int lvl, boolean nowrap)
191 if (lvl == DEFAULT_COMPRESSION)
193 else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION)
194 throw new IllegalArgumentException();
196 pending = new DeflaterPending();
197 engine = new DeflaterEngine(pending);
198 this.noHeader = nowrap;
199 setStrategy(DEFAULT_STRATEGY);
205 * Resets the deflater. The deflater acts afterwards as if it was
206 * just created with the same compression level and strategy as it
211 state = (noHeader ? BUSY_STATE : INIT_STATE);
218 * Frees all objects allocated by the compressor. There's no
219 * reason to call this, since you can just rely on garbage
220 * collection. Exists only for compatibility against Sun's JDK,
221 * where the compressor allocates native memory.
222 * If you call any method (even reset) afterwards the behaviour is
229 state = CLOSED_STATE;
233 * Gets the current adler checksum of the data that was processed so
236 public int getAdler()
238 return engine.getAdler();
242 * Gets the number of input bytes processed so far.
244 public int getTotalIn()
246 return (int) engine.getTotalIn();
250 * Gets the number of input bytes processed so far.
253 public long getBytesRead()
255 return engine.getTotalIn();
259 * Gets the number of output bytes so far.
261 public int getTotalOut()
263 return (int) totalOut;
267 * Gets the number of output bytes so far.
270 public long getBytesWritten()
276 * Finalizes this object.
278 protected void finalize()
280 /* Exists solely for compatibility. We don't have any native state. */
284 * Flushes the current input block. Further calls to deflate() will
285 * produce enough output to inflate everything in the current input
286 * block. This is not part of Sun's JDK so I have made it package
287 * private. It is used by DeflaterOutputStream to implement
291 state |= IS_FLUSHING;
295 * Finishes the deflater with the current input block. It is an error
296 * to give more input after this method was called. This method must
297 * be called to force all bytes to be flushed.
299 public void finish() {
300 state |= IS_FLUSHING | IS_FINISHING;
304 * Returns true iff the stream was finished and no more output bytes
307 public boolean finished()
309 return state == FINISHED_STATE && pending.isFlushed();
313 * Returns true, if the input buffer is empty.
314 * You should then call setInput(). <br>
316 * <em>NOTE</em>: This method can also return true when the stream
319 public boolean needsInput()
321 return engine.needsInput();
325 * Sets the data which should be compressed next. This should be only
326 * called when needsInput indicates that more input is needed.
327 * If you call setInput when needsInput() returns false, the
328 * previous input that is still pending will be thrown away.
329 * The given byte array should not be changed, before needsInput() returns
331 * This call is equivalent to <code>setInput(input, 0, input.length)</code>.
332 * @param input the buffer containing the input data.
333 * @exception IllegalStateException if the buffer was finished() or ended().
335 public void setInput(byte[] input)
337 setInput(input, 0, input.length);
341 * Sets the data which should be compressed next. This should be
342 * only called when needsInput indicates that more input is needed.
343 * The given byte array should not be changed, before needsInput() returns
345 * @param input the buffer containing the input data.
346 * @param off the start of the data.
347 * @param len the length of the data.
348 * @exception IllegalStateException if the buffer was finished() or ended()
349 * or if previous input is still pending.
351 public void setInput(byte[] input, int off, int len)
353 if ((state & IS_FINISHING) != 0)
354 throw new IllegalStateException("finish()/end() already called");
355 engine.setInput(input, off, len);
359 * Sets the compression level. There is no guarantee of the exact
360 * position of the change, but if you call this when needsInput is
361 * true the change of compression level will occur somewhere near
362 * before the end of the so far given input.
363 * @param lvl the new compression level.
365 public void setLevel(int lvl)
367 if (lvl == DEFAULT_COMPRESSION)
369 else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION)
370 throw new IllegalArgumentException();
376 engine.setLevel(lvl);
381 * Sets the compression strategy. Strategy is one of
382 * DEFAULT_STRATEGY, HUFFMAN_ONLY and FILTERED. For the exact
383 * position where the strategy is changed, the same as for
384 * setLevel() applies.
385 * @param stgy the new compression strategy.
387 public void setStrategy(int stgy)
389 if (stgy != DEFAULT_STRATEGY && stgy != FILTERED
390 && stgy != HUFFMAN_ONLY)
391 throw new IllegalArgumentException();
392 engine.setStrategy(stgy);
396 * Deflates the current input block to the given array. It returns
397 * the number of bytes compressed, or 0 if either
398 * needsInput() or finished() returns true or length is zero.
399 * @param output the buffer where to write the compressed data.
401 public int deflate(byte[] output)
403 return deflate(output, 0, output.length);
407 * Deflates the current input block to the given array. It returns
408 * the number of bytes compressed, or 0 if either
409 * needsInput() or finished() returns true or length is zero.
410 * @param output the buffer where to write the compressed data.
411 * @param offset the offset into the output array.
412 * @param length the maximum number of bytes that may be written.
413 * @exception IllegalStateException if end() was called.
414 * @exception IndexOutOfBoundsException if offset and/or length
415 * don't match the array length.
417 public int deflate(byte[] output, int offset, int length)
419 int origLength = length;
421 if (state == CLOSED_STATE)
422 throw new IllegalStateException("Deflater closed");
424 if (state < BUSY_STATE)
427 int header = (DEFLATED +
428 ((DeflaterConstants.MAX_WBITS - 8) << 4)) << 8;
429 int level_flags = (level - 1) >> 1;
430 if (level_flags < 0 || level_flags > 3)
432 header |= level_flags << 6;
433 if ((state & IS_SETDICT) != 0)
434 /* Dictionary was set */
435 header |= DeflaterConstants.PRESET_DICT;
436 header += 31 - (header % 31);
438 pending.writeShortMSB(header);
439 if ((state & IS_SETDICT) != 0)
441 int chksum = engine.getAdler();
443 pending.writeShortMSB(chksum >> 16);
444 pending.writeShortMSB(chksum & 0xffff);
447 state = BUSY_STATE | (state & (IS_FLUSHING | IS_FINISHING));
452 int count = pending.flush(output, offset, length);
456 if (length == 0 || state == FINISHED_STATE)
459 if (!engine.deflate((state & IS_FLUSHING) != 0,
460 (state & IS_FINISHING) != 0))
462 if (state == BUSY_STATE)
463 /* We need more input now */
464 return origLength - length;
465 else if (state == FLUSHING_STATE)
467 if (level != NO_COMPRESSION)
469 /* We have to supply some lookahead. 8 bit lookahead
470 * are needed by the zlib inflater, and we must fill
471 * the next byte, so that all bits are flushed.
473 int neededbits = 8 + ((-pending.getBitCount()) & 7);
474 while (neededbits > 0)
476 /* write a static tree block consisting solely of
479 pending.writeBits(2, 10);
485 else if (state == FINISHING_STATE)
487 pending.alignToByte();
488 /* We have completed the stream */
491 int adler = engine.getAdler();
492 pending.writeShortMSB(adler >> 16);
493 pending.writeShortMSB(adler & 0xffff);
495 state = FINISHED_STATE;
500 return origLength - length;
504 * Sets the dictionary which should be used in the deflate process.
505 * This call is equivalent to <code>setDictionary(dict, 0,
506 * dict.length)</code>.
507 * @param dict the dictionary.
508 * @exception IllegalStateException if setInput () or deflate ()
509 * were already called or another dictionary was already set.
511 public void setDictionary(byte[] dict)
513 setDictionary(dict, 0, dict.length);
517 * Sets the dictionary which should be used in the deflate process.
518 * The dictionary should be a byte array containing strings that are
519 * likely to occur in the data which should be compressed. The
520 * dictionary is not stored in the compressed output, only a
521 * checksum. To decompress the output you need to supply the same
523 * @param dict the dictionary.
524 * @param offset an offset into the dictionary.
525 * @param length the length of the dictionary.
526 * @exception IllegalStateException if setInput () or deflate () were
527 * already called or another dictionary was already set.
529 public void setDictionary(byte[] dict, int offset, int length)
531 if (state != INIT_STATE)
532 throw new IllegalStateException();
534 state = SETDICT_STATE;
535 engine.setDictionary(dict, offset, length);