~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/java/util/zip/Deflater.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Deflater.java - Compress a data stream
 
2
   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2011
 
3
   Free Software Foundation, Inc.
 
4
 
 
5
This file is part of GNU Classpath.
 
6
 
 
7
GNU Classpath is free software; you can redistribute it and/or modify
 
8
it under the terms of the GNU General Public License as published by
 
9
the Free Software Foundation; either version 2, or (at your option)
 
10
any later version.
 
11
 
 
12
GNU Classpath is distributed in the hope that it will be useful, but
 
13
WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with GNU Classpath; see the file COPYING.  If not, write to the
 
19
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
20
02110-1301 USA.
 
21
 
 
22
Linking this library statically or dynamically with other modules is
 
23
making a combined work based on this library.  Thus, the terms and
 
24
conditions of the GNU General Public License cover the whole
 
25
combination.
 
26
 
 
27
As a special exception, the copyright holders of this library give you
 
28
permission to link this library with independent modules to produce an
 
29
executable, regardless of the license terms of these independent
 
30
modules, and to copy and distribute the resulting executable under
 
31
terms of your choice, provided that you also meet, for each linked
 
32
independent module, the terms and conditions of the license of that
 
33
module.  An independent module is a module which is not derived from
 
34
or based on this library.  If you modify this library, you may extend
 
35
this exception to your version of the library, but you are not
 
36
obligated to do so.  If you do not wish to do so, delete this
 
37
exception statement from your version. */
 
38
 
 
39
package java.util.zip;
 
40
 
 
41
/**
 
42
 * This is the Deflater class.  The deflater class compresses input
 
43
 * with the deflate algorithm described in RFC 1951.  It has several
 
44
 * compression levels and three different strategies described below.
 
45
 * 
 
46
 * This class is <i>not</i> thread safe.  This is inherent in the API, due
 
47
 * to the split of deflate and setInput.
 
48
 * 
 
49
 * @author Jochen Hoenicke
 
50
 * @author Tom Tromey
 
51
 */
 
52
public class Deflater
 
53
{
 
54
  /**
 
55
   * The best and slowest compression level.  This tries to find very
 
56
   * long and distant string repetitions.  
 
57
   */
 
58
  public static final int BEST_COMPRESSION = 9;
 
59
  /**
 
60
   * The worst but fastest compression level.  
 
61
   */
 
62
  public static final int BEST_SPEED = 1;
 
63
  /**
 
64
   * The default compression level.
 
65
   */
 
66
  public static final int DEFAULT_COMPRESSION = -1;
 
67
  /**
 
68
   * This level won't compress at all but output uncompressed blocks.
 
69
   */
 
70
  public static final int NO_COMPRESSION = 0;
 
71
 
 
72
  /**
 
73
   * Flush operation.
 
74
   */
 
75
  public static final int NO_FLUSH = 0;
 
76
  public static final int SYNC_FLUSH = 2;
 
77
  public static final int FULL_FLUSH = 3;
 
78
 
 
79
  /**
 
80
   * The default strategy.
 
81
   */
 
82
  public static final int DEFAULT_STRATEGY = 0;
 
83
  /**
 
84
   * This strategy will only allow longer string repetitions.  It is
 
85
   * useful for random data with a small character set.
 
86
   */
 
87
  public static final int FILTERED = 1;
 
88
 
 
89
  /** 
 
90
   * This strategy will not look for string repetitions at all.  It
 
91
   * only encodes with Huffman trees (which means, that more common
 
92
   * characters get a smaller encoding.  
 
93
   */
 
94
  public static final int HUFFMAN_ONLY = 2;
 
95
 
 
96
  /**
 
97
   * The compression method.  This is the only method supported so far.
 
98
   * There is no need to use this constant at all.
 
99
   */
 
100
  public static final int DEFLATED = 8;
 
101
 
 
102
  /*
 
103
   * The Deflater can do the following state transitions:
 
104
   *
 
105
   * (1) -> INIT_STATE   ----> INIT_FINISHING_STATE ---.
 
106
   *        /  | (2)      (5)                         |
 
107
   *       /   v          (5)                         |
 
108
   *   (3)| SETDICT_STATE ---> SETDICT_FINISHING_STATE |(3)
 
109
   *       \   | (3)                 |        ,-------'
 
110
   *        |  |                     | (3)   /
 
111
   *        v  v          (5)        v      v
 
112
   * (1) -> BUSY_STATE   ----> FINISHING_STATE
 
113
   *                                | (6)
 
114
   *                                v
 
115
   *                           FINISHED_STATE
 
116
   *    \_____________________________________/
 
117
   *          | (7)
 
118
   *          v
 
119
   *        CLOSED_STATE
 
120
   *
 
121
   * (1) If we should produce a header we start in INIT_STATE, otherwise
 
122
   *     we start in BUSY_STATE.
 
123
   * (2) A dictionary may be set only when we are in INIT_STATE, then
 
124
   *     we change the state as indicated.
 
125
   * (3) Whether a dictionary is set or not, on the first call of deflate
 
126
   *     we change to BUSY_STATE.
 
127
   * (4) -- intentionally left blank -- :)
 
128
   * (5) FINISHING_STATE is entered, when flush() is called to indicate that
 
129
   *     there is no more INPUT.  There are also states indicating, that
 
130
   *     the header wasn't written yet.
 
131
   * (6) FINISHED_STATE is entered, when everything has been flushed to the
 
132
   *     internal pending output buffer.
 
133
   * (7) At any time (7)
 
134
   * 
 
135
   */
 
136
 
 
137
  private static final int IS_SETDICT              = 0x01;
 
138
  private static final int IS_FINISHING            = 0x08;
 
139
  
 
140
  private static final int INIT_STATE              = 0x00;
 
141
  private static final int SETDICT_STATE           = 0x01;
 
142
  private static final int INIT_FINISHING_STATE    = 0x08;
 
143
  private static final int SETDICT_FINISHING_STATE = 0x09;
 
144
  private static final int BUSY_STATE              = 0x10;
 
145
  private static final int FINISHING_STATE         = 0x18;
 
146
  private static final int FINISHED_STATE          = 0x1e;
 
147
  private static final int CLOSED_STATE            = 0x7f;
 
148
 
 
149
  /** Compression level. */
 
150
  private int level;
 
151
 
 
152
  /** should we include a header. */
 
153
  private boolean noHeader;
 
154
 
 
155
  /** The current state. */
 
156
  private int state;
 
157
 
 
158
  /** The flush level the previous deflate call used. */
 
159
  private int lastFlush;
 
160
 
 
161
  /** The total bytes of output written. */
 
162
  private long totalOut;
 
163
 
 
164
  /** The pending output. */
 
165
  private DeflaterPending pending;
 
166
 
 
167
  /** The deflater engine. */
 
168
  private DeflaterEngine engine;
 
169
 
 
170
  /**
 
171
   * Creates a new deflater with default compression level.
 
172
   */
 
173
  public Deflater()
 
174
  {
 
175
    this(DEFAULT_COMPRESSION, false);
 
176
  }
 
177
 
 
178
  /**
 
179
   * Creates a new deflater with given compression level.
 
180
   * @param lvl the compression level, a value between NO_COMPRESSION
 
181
   * and BEST_COMPRESSION, or DEFAULT_COMPRESSION.  
 
182
   * @exception IllegalArgumentException if lvl is out of range.
 
183
   */
 
184
  public Deflater(int lvl)
 
185
  {
 
186
    this(lvl, false);
 
187
  }
 
188
 
 
189
  /**
 
190
   * Creates a new deflater with given compression level.
 
191
   * @param lvl the compression level, a value between NO_COMPRESSION
 
192
   * and BEST_COMPRESSION.  
 
193
   * @param nowrap true, iff we should suppress the deflate header at the
 
194
   * beginning and the adler checksum at the end of the output.  This is
 
195
   * useful for the GZIP format.
 
196
   * @exception IllegalArgumentException if lvl is out of range.
 
197
   */
 
198
  public Deflater(int lvl, boolean nowrap)
 
199
  {
 
200
    if (lvl == DEFAULT_COMPRESSION)
 
201
      lvl = 6;
 
202
    else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION)
 
203
      throw new IllegalArgumentException();
 
204
 
 
205
    pending = new DeflaterPending();
 
206
    engine = new DeflaterEngine(pending);
 
207
    this.noHeader = nowrap;
 
208
    setStrategy(DEFAULT_STRATEGY);
 
209
    setLevel(lvl);
 
210
    reset();
 
211
  }
 
212
 
 
213
  /** 
 
214
   * Resets the deflater.  The deflater acts afterwards as if it was
 
215
   * just created with the same compression level and strategy as it
 
216
   * had before.  
 
217
   */
 
218
  public void reset() 
 
219
  {
 
220
    state = (noHeader ? BUSY_STATE : INIT_STATE);
 
221
    totalOut = 0;
 
222
    pending.reset();
 
223
    engine.reset();
 
224
  }
 
225
  
 
226
  /**
 
227
   * Frees all objects allocated by the compressor.  There's no
 
228
   * reason to call this, since you can just rely on garbage
 
229
   * collection.  Exists only for compatibility against Sun's JDK,
 
230
   * where the compressor allocates native memory.
 
231
   * If you call any method (even reset) afterwards the behaviour is
 
232
   * <i>undefined</i>.  
 
233
   */
 
234
  public void end()
 
235
  {
 
236
    engine = null;
 
237
    pending = null;
 
238
    state = CLOSED_STATE;
 
239
  }
 
240
 
 
241
  /** 
 
242
   * Gets the current adler checksum of the data that was processed so
 
243
   * far.
 
244
   */
 
245
  public int getAdler()
 
246
  {
 
247
    return engine.getAdler();
 
248
  }
 
249
 
 
250
  /** 
 
251
   * Gets the number of input bytes processed so far.
 
252
   */
 
253
  public int getTotalIn()
 
254
  {
 
255
    return (int) engine.getTotalIn();
 
256
  }
 
257
 
 
258
  /** 
 
259
   * Gets the number of input bytes processed so far.
 
260
   * @since 1.5
 
261
   */
 
262
  public long getBytesRead()
 
263
  {
 
264
    return engine.getTotalIn();
 
265
  }
 
266
 
 
267
  /** 
 
268
   * Gets the number of output bytes so far.
 
269
   */
 
270
  public int getTotalOut()
 
271
  {
 
272
    return (int) totalOut;
 
273
  }
 
274
 
 
275
  /** 
 
276
   * Gets the number of output bytes so far.
 
277
   * @since 1.5
 
278
   */
 
279
  public long getBytesWritten()
 
280
  {
 
281
    return totalOut;
 
282
  }
 
283
 
 
284
  /** 
 
285
   * Finalizes this object.
 
286
   */
 
287
  protected void finalize()
 
288
  {
 
289
    /* Exists solely for compatibility.  We don't have any native state. */
 
290
  }
 
291
 
 
292
  /** 
 
293
   * Finishes the deflater with the current input block.  It is an error
 
294
   * to give more input after this method was called.  This method must
 
295
   * be called to force all bytes to be flushed.
 
296
   */
 
297
  public void finish() {
 
298
    state |= IS_FINISHING;
 
299
  }
 
300
 
 
301
  /** 
 
302
   * Returns true iff the stream was finished and no more output bytes
 
303
   * are available.
 
304
   */
 
305
  public boolean finished()
 
306
  {
 
307
    return state == FINISHED_STATE && pending.isFlushed();
 
308
  }
 
309
 
 
310
  /**
 
311
   * Returns true, if the input buffer is empty.
 
312
   * You should then call setInput(). <br>
 
313
   *
 
314
   * <em>NOTE</em>: This method can also return true when the stream
 
315
   * was finished.  
 
316
   */
 
317
  public boolean needsInput()
 
318
  {
 
319
    return engine.needsInput();
 
320
  }
 
321
 
 
322
  /**
 
323
   * Sets the data which should be compressed next.  This should be only
 
324
   * called when needsInput indicates that more input is needed.
 
325
   * If you call setInput when needsInput() returns false, the
 
326
   * previous input that is still pending will be thrown away.
 
327
   * The given byte array should not be changed, before needsInput() returns
 
328
   * true again.
 
329
   * This call is equivalent to <code>setInput(input, 0, input.length)</code>.
 
330
   * @param input the buffer containing the input data.
 
331
   * @exception IllegalStateException if the buffer was finished() or ended().
 
332
   */
 
333
  public void setInput(byte[] input)
 
334
  {
 
335
    setInput(input, 0, input.length);
 
336
  }
 
337
 
 
338
  /**
 
339
   * Sets the data which should be compressed next.  This should be
 
340
   * only called when needsInput indicates that more input is needed.
 
341
   * The given byte array should not be changed, before needsInput() returns
 
342
   * true again.
 
343
   * @param input the buffer containing the input data.
 
344
   * @param off the start of the data.
 
345
   * @param len the length of the data.  
 
346
   * @exception IllegalStateException if the buffer was finished() or ended()
 
347
   * or if previous input is still pending.
 
348
   */
 
349
  public void setInput(byte[] input, int off, int len)
 
350
  {
 
351
    if (input == null)
 
352
        throw new NullPointerException();
 
353
    if (off < 0 || len < 0 || off > input.length - len)
 
354
        throw new ArrayIndexOutOfBoundsException();
 
355
    engine.setInput(input, off, len);
 
356
  }
 
357
 
 
358
  /** 
 
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.
 
364
   */
 
365
  public void setLevel(int lvl)
 
366
  {
 
367
    if (lvl == DEFAULT_COMPRESSION)
 
368
      lvl = 6;
 
369
    else if (lvl < NO_COMPRESSION || lvl > BEST_COMPRESSION)
 
370
      throw new IllegalArgumentException();
 
371
 
 
372
 
 
373
    if (level != lvl)
 
374
      {
 
375
        level = lvl;
 
376
        engine.setLevel(lvl);
 
377
      }
 
378
  }
 
379
 
 
380
  /** 
 
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.
 
386
   */
 
387
  public void setStrategy(int stgy)
 
388
  {
 
389
    if (stgy != DEFAULT_STRATEGY && stgy != FILTERED
 
390
        && stgy != HUFFMAN_ONLY)
 
391
      throw new IllegalArgumentException();
 
392
    engine.setStrategy(stgy);
 
393
  }
 
394
 
 
395
  /**
 
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.
 
400
   */
 
401
  public int deflate(byte[] output)
 
402
  {
 
403
    return deflate(output, 0, output.length, NO_FLUSH);
 
404
  }
 
405
 
 
406
  /**
 
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.  
 
416
   */
 
417
  public int deflate(byte[] output, int offset, int length)
 
418
  {
 
419
    return deflate(output, offset, length, NO_FLUSH);
 
420
  }
 
421
 
 
422
  /**
 
423
   * Deflates the current input block to the given array.  It returns 
 
424
   * the number of bytes compressed, or 0 if either 
 
425
   * needsInput() or finished() returns true or length is zero.
 
426
   * @param output the buffer where to write the compressed data.
 
427
   * @param offset the offset into the output array.
 
428
   * @param length the maximum number of bytes that may be written.
 
429
   * @param flush the compression flush mode
 
430
   * @exception IllegalStateException if end() was called.
 
431
   * @exception IndexOutOfBoundsException if offset and/or length
 
432
   * don't match the array length.  
 
433
   */
 
434
  public int deflate(byte[] output, int offset, int length, int flush)
 
435
  {
 
436
    if (output.length - offset < length || offset < 0 || length < 0)
 
437
      throw new ArrayIndexOutOfBoundsException();
 
438
 
 
439
    if (flush < NO_FLUSH || flush > FULL_FLUSH)
 
440
      throw new IllegalArgumentException();
 
441
 
 
442
    int origLength = length;
 
443
 
 
444
    if (state == CLOSED_STATE)
 
445
      throw new IllegalStateException("Deflater closed");
 
446
 
 
447
    if (state < BUSY_STATE)
 
448
      {
 
449
        /* output header */
 
450
        int header = (DEFLATED + 
 
451
                      ((DeflaterConstants.MAX_WBITS - 8) << 4)) << 8;
 
452
        int level_flags = (level - 1) >> 1;
 
453
        if (level_flags < 0 || level_flags > 3) 
 
454
          level_flags = 3;
 
455
        header |= level_flags << 6;
 
456
        if ((state & IS_SETDICT) != 0)
 
457
          /* Dictionary was set */
 
458
          header |= DeflaterConstants.PRESET_DICT;
 
459
        header += 31 - (header % 31);
 
460
 
 
461
        pending.writeShortMSB(header);
 
462
        if ((state & IS_SETDICT) != 0)
 
463
          {
 
464
            int chksum = engine.getAdler();
 
465
            engine.resetAdler();
 
466
            pending.writeShortMSB(chksum >> 16);
 
467
            pending.writeShortMSB(chksum & 0xffff);
 
468
          }
 
469
 
 
470
        state = BUSY_STATE | (state & IS_FINISHING);
 
471
      }
 
472
 
 
473
    int oldFlush = lastFlush;
 
474
    lastFlush = flush;
 
475
 
 
476
    if (engine.needsInput() && flush <= oldFlush && (state & IS_FINISHING) == 0)
 
477
      {
 
478
        int count = pending.flush(output, offset, length);
 
479
        totalOut += count;
 
480
        return count;
 
481
      }
 
482
 
 
483
    boolean done = state == FINISHED_STATE;
 
484
 
 
485
    for (;;)
 
486
      {
 
487
        int count = pending.flush(output, offset, length);
 
488
        offset += count;
 
489
        totalOut += count;
 
490
        length -= count;
 
491
        if (length == 0 || done)
 
492
          return origLength - length;
 
493
 
 
494
        if (!engine.deflate((state & IS_FINISHING) != 0 || flush != NO_FLUSH,
 
495
                            (state & IS_FINISHING) != 0))
 
496
          {
 
497
            done = true;
 
498
            if (state == FINISHING_STATE)
 
499
              {
 
500
                pending.alignToByte();
 
501
                /* We have completed the stream */
 
502
                if (!noHeader)
 
503
                  {
 
504
                    int adler = engine.getAdler();
 
505
                    pending.writeShortMSB(adler >> 16);
 
506
                    pending.writeShortMSB(adler & 0xffff);
 
507
                  }
 
508
                state = FINISHED_STATE;
 
509
              }
 
510
            else if (flush != NO_FLUSH)
 
511
              {
 
512
                if (level != NO_COMPRESSION)
 
513
                  {
 
514
                    /* We have to supply some lookahead.  8 bit lookahead
 
515
                     * are needed by the zlib inflater, and we must fill 
 
516
                     * the next byte, so that all bits are flushed.
 
517
                     */
 
518
                    int neededbits = 8 + ((-pending.getBitCount()) & 7);
 
519
                    while (neededbits > 0)
 
520
                      {
 
521
                        /* write a static tree block consisting solely of
 
522
                         * an EOF:
 
523
                         */
 
524
                        pending.writeBits(2, 10);
 
525
                        neededbits -= 10;
 
526
                      }
 
527
                  }
 
528
                if (flush == FULL_FLUSH)
 
529
                  engine.clearHash();
 
530
              }
 
531
          }
 
532
      }
 
533
  }
 
534
 
 
535
  /**
 
536
   * Sets the dictionary which should be used in the deflate process.
 
537
   * This call is equivalent to <code>setDictionary(dict, 0,
 
538
   * dict.length)</code>.  
 
539
   * @param dict the dictionary.  
 
540
   * @exception IllegalStateException if setInput () or deflate ()
 
541
   * were already called or another dictionary was already set.  
 
542
   */
 
543
  public void setDictionary(byte[] dict)
 
544
  {
 
545
    setDictionary(dict, 0, dict.length);
 
546
  }
 
547
 
 
548
  /**
 
549
   * Sets the dictionary which should be used in the deflate process.
 
550
   * The dictionary should be a byte array containing strings that are
 
551
   * likely to occur in the data which should be compressed.  The
 
552
   * dictionary is not stored in the compressed output, only a
 
553
   * checksum.  To decompress the output you need to supply the same
 
554
   * dictionary again.
 
555
   * @param dict the dictionary.
 
556
   * @param offset an offset into the dictionary.
 
557
   * @param length the length of the dictionary.
 
558
   * @exception IllegalStateException if setInput () or deflate () were
 
559
   * already called or another dictionary was already set.
 
560
   */
 
561
  public void setDictionary(byte[] dict, int offset, int length)
 
562
  {
 
563
    if (state != INIT_STATE)
 
564
      throw new IllegalStateException();
 
565
 
 
566
    state = SETDICT_STATE;
 
567
    engine.setDictionary(dict, offset, length);
 
568
  }
 
569
}