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

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/java/util/zip/Inflater.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
/* Inflater.java - Decompress a data stream
 
2
   Copyright (C) 1999, 2000, 2001, 2003, 2005  Free Software Foundation, Inc.
 
3
 
 
4
This file is part of GNU Classpath.
 
5
 
 
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)
 
9
any later version.
 
10
 
 
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.
 
15
 
 
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
 
19
02110-1301 USA.
 
20
 
 
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
 
24
combination.
 
25
 
 
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. */
 
37
 
 
38
package java.util.zip;
 
39
 
 
40
/* Written using on-line Java Platform 1.2 API Specification
 
41
 * and JCL book.
 
42
 * Believed complete and correct.
 
43
 */
 
44
 
 
45
/**
 
46
 * Inflater is used to decompress data that has been compressed according 
 
47
 * to the "deflate" standard described in rfc1950.
 
48
 *
 
49
 * The usage is as following.  First you have to set some input with
 
50
 * <code>setInput()</code>, then inflate() it.  If inflate doesn't
 
51
 * inflate any bytes there may be three reasons:
 
52
 * <ul>
 
53
 * <li>needsInput() returns true because the input buffer is empty.
 
54
 * You have to provide more input with <code>setInput()</code>.  
 
55
 * NOTE: needsInput() also returns true when, the stream is finished.
 
56
 * </li>
 
57
 * <li>needsDictionary() returns true, you have to provide a preset 
 
58
 *     dictionary with <code>setDictionary()</code>.</li>
 
59
 * <li>finished() returns true, the inflater has finished.</li>
 
60
 * </ul>
 
61
 * Once the first output byte is produced, a dictionary will not be
 
62
 * needed at a later stage.
 
63
 *
 
64
 * @author John Leuner, Jochen Hoenicke
 
65
 * @author Tom Tromey
 
66
 * @date May 17, 1999
 
67
 * @since JDK 1.1
 
68
 */
 
69
public class Inflater
 
70
{
 
71
  /* Copy lengths for literal codes 257..285 */
 
72
  private static final int CPLENS[] = 
 
73
  { 
 
74
    3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
 
75
    35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258
 
76
  };
 
77
  
 
78
  /* Extra bits for literal codes 257..285 */  
 
79
  private static final int CPLEXT[] = 
 
80
  { 
 
81
    0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
 
82
    3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
 
83
  };
 
84
 
 
85
  /* Copy offsets for distance codes 0..29 */
 
86
  private static final int CPDIST[] = {
 
87
    1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
 
88
    257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
 
89
    8193, 12289, 16385, 24577
 
90
  };
 
91
  
 
92
  /* Extra bits for distance codes */
 
93
  private static final int CPDEXT[] = {
 
94
    0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
 
95
    7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 
 
96
    12, 12, 13, 13
 
97
  };
 
98
 
 
99
  /* This are the state in which the inflater can be.  */
 
100
  private static final int DECODE_HEADER           = 0;
 
101
  private static final int DECODE_DICT             = 1;
 
102
  private static final int DECODE_BLOCKS           = 2;
 
103
  private static final int DECODE_STORED_LEN1      = 3;
 
104
  private static final int DECODE_STORED_LEN2      = 4;
 
105
  private static final int DECODE_STORED           = 5;
 
106
  private static final int DECODE_DYN_HEADER       = 6;
 
107
  private static final int DECODE_HUFFMAN          = 7;
 
108
  private static final int DECODE_HUFFMAN_LENBITS  = 8;
 
109
  private static final int DECODE_HUFFMAN_DIST     = 9;
 
110
  private static final int DECODE_HUFFMAN_DISTBITS = 10;
 
111
  private static final int DECODE_CHKSUM           = 11;
 
112
  private static final int FINISHED                = 12;
 
113
 
 
114
  /** This variable contains the current state. */
 
115
  private int mode;
 
116
 
 
117
  /**
 
118
   * The adler checksum of the dictionary or of the decompressed
 
119
   * stream, as it is written in the header resp. footer of the
 
120
   * compressed stream.  <br>
 
121
   *
 
122
   * Only valid if mode is DECODE_DICT or DECODE_CHKSUM.
 
123
   */
 
124
  private int readAdler;
 
125
  /** 
 
126
   * The number of bits needed to complete the current state.  This
 
127
   * is valid, if mode is DECODE_DICT, DECODE_CHKSUM,
 
128
   * DECODE_HUFFMAN_LENBITS or DECODE_HUFFMAN_DISTBITS.  
 
129
   */
 
130
  private int neededBits;
 
131
  private int repLength, repDist;
 
132
  private int uncomprLen;
 
133
  /**
 
134
   * True, if the last block flag was set in the last block of the
 
135
   * inflated stream.  This means that the stream ends after the
 
136
   * current block.  
 
137
   */
 
138
  private boolean isLastBlock;
 
139
 
 
140
  /**
 
141
   * The total number of inflated bytes.
 
142
   */
 
143
  private long totalOut;
 
144
  /**
 
145
   * The total number of bytes set with setInput().  This is not the
 
146
   * value returned by getTotalIn(), since this also includes the 
 
147
   * unprocessed input.
 
148
   */
 
149
  private long totalIn;
 
150
  /**
 
151
   * This variable stores the nowrap flag that was given to the constructor.
 
152
   * True means, that the inflated stream doesn't contain a header nor the
 
153
   * checksum in the footer.
 
154
   */
 
155
  private boolean nowrap;
 
156
 
 
157
  private StreamManipulator input;
 
158
  private OutputWindow outputWindow;
 
159
  private InflaterDynHeader dynHeader;
 
160
  private InflaterHuffmanTree litlenTree, distTree;
 
161
  private Adler32 adler;
 
162
 
 
163
  /**
 
164
   * Creates a new inflater.
 
165
   */
 
166
  public Inflater ()
 
167
  {
 
168
    this (false);
 
169
  }
 
170
 
 
171
  /**
 
172
   * Creates a new inflater.
 
173
   * @param nowrap true if no header and checksum field appears in the
 
174
   * stream.  This is used for GZIPed input.  For compatibility with
 
175
   * Sun JDK you should provide one byte of input more than needed in
 
176
   * this case.
 
177
   */
 
178
  public Inflater (boolean nowrap)
 
179
  {
 
180
    this.nowrap = nowrap;
 
181
    this.adler = new Adler32();
 
182
    input = new StreamManipulator();
 
183
    outputWindow = new OutputWindow();
 
184
    mode = nowrap ? DECODE_BLOCKS : DECODE_HEADER;
 
185
  }
 
186
 
 
187
  /**
 
188
   * Finalizes this object.
 
189
   */
 
190
  protected void finalize ()
 
191
  {
 
192
    /* Exists only for compatibility */
 
193
  }
 
194
 
 
195
  /**
 
196
   * Frees all objects allocated by the inflater.  There's no reason
 
197
   * to call this, since you can just rely on garbage collection (even
 
198
   * for the Sun implementation).  Exists only for compatibility
 
199
   * with Sun's JDK, where the compressor allocates native memory.
 
200
   * If you call any method (even reset) afterwards the behaviour is
 
201
   * <i>undefined</i>.  
 
202
   */
 
203
  public void end ()
 
204
  {
 
205
    outputWindow = null;
 
206
    input = null;
 
207
    dynHeader = null;
 
208
    litlenTree = null;
 
209
    distTree = null;
 
210
    adler = null;
 
211
  }
 
212
 
 
213
  /**
 
214
   * Returns true, if the inflater has finished.  This means, that no
 
215
   * input is needed and no output can be produced.
 
216
   */
 
217
  public boolean finished() 
 
218
  {
 
219
    return mode == FINISHED
 
220
        && (outputWindow == null || outputWindow.getAvailable() == 0);
 
221
  }
 
222
 
 
223
  /**
 
224
   * Gets the adler checksum.  This is either the checksum of all
 
225
   * uncompressed bytes returned by inflate(), or if needsDictionary()
 
226
   * returns true (and thus no output was yet produced) this is the
 
227
   * adler checksum of the expected dictionary.
 
228
   * @returns the adler checksum.
 
229
   */
 
230
  public int getAdler()
 
231
  {
 
232
    return needsDictionary() ? readAdler : (int) adler.getValue();
 
233
  }
 
234
  
 
235
  /**
 
236
   * Gets the number of unprocessed input.  Useful, if the end of the
 
237
   * stream is reached and you want to further process the bytes after
 
238
   * the deflate stream.  
 
239
   * @return the number of bytes of the input which were not processed.
 
240
   */
 
241
  public int getRemaining()
 
242
  {
 
243
    return input.getAvailableBytes();
 
244
  }
 
245
  
 
246
  /**
 
247
   * Gets the total number of processed compressed input bytes.
 
248
   * @return the total number of bytes of processed input bytes.
 
249
   */
 
250
  public int getTotalIn()
 
251
  {
 
252
    return (int) (totalIn - getRemaining());
 
253
  }
 
254
 
 
255
  /**
 
256
   * Gets the total number of processed compressed input bytes.
 
257
   * @return the total number of bytes of processed input bytes.
 
258
   * @since 1.5
 
259
   */
 
260
  public long getBytesRead()
 
261
  {
 
262
    return totalIn - getRemaining();
 
263
  }
 
264
 
 
265
  /**
 
266
   * Gets the total number of output bytes returned by inflate().
 
267
   * @return the total number of output bytes.
 
268
   */
 
269
  public int getTotalOut()
 
270
  {
 
271
    return (int) totalOut;
 
272
  }
 
273
 
 
274
  /**
 
275
   * Gets the total number of output bytes returned by inflate().
 
276
   * @return the total number of output bytes.
 
277
   * @since 1.5
 
278
   */
 
279
  public long getBytesWritten()
 
280
  {
 
281
    return totalOut;
 
282
  }
 
283
 
 
284
  /**
 
285
   * Inflates the compressed stream to the output buffer.  If this
 
286
   * returns 0, you should check, whether needsDictionary(),
 
287
   * needsInput() or finished() returns true, to determine why no 
 
288
   * further output is produced.
 
289
   * @param buf the output buffer.
 
290
   * @return the number of bytes written to the buffer, 0 if no further
 
291
   * output can be produced.  
 
292
   * @exception DataFormatException if deflated stream is invalid.
 
293
   * @exception IllegalArgumentException if buf has length 0.
 
294
   */
 
295
  public int inflate (byte[] buf) throws DataFormatException
 
296
  {
 
297
    return inflate (buf, 0, buf.length);
 
298
  }
 
299
 
 
300
  /**
 
301
   * Inflates the compressed stream to the output buffer.  If this
 
302
   * returns 0, you should check, whether needsDictionary(),
 
303
   * needsInput() or finished() returns true, to determine why no 
 
304
   * further output is produced.
 
305
   * @param buf the output buffer.
 
306
   * @param off the offset into buffer where the output should start.
 
307
   * @param len the maximum length of the output.
 
308
   * @return the number of bytes written to the buffer, 0 if no further
 
309
   * output can be produced.  
 
310
   * @exception DataFormatException if deflated stream is invalid.
 
311
   * @exception IndexOutOfBoundsException if the off and/or len are wrong.
 
312
   */
 
313
  public int inflate (byte[] buf, int off, int len) throws DataFormatException
 
314
  {
 
315
    /* Check for correct buff, off, len triple */
 
316
    if (0 > off || off > off + len || off + len > buf.length)
 
317
      throw new ArrayIndexOutOfBoundsException();
 
318
    int count = 0;
 
319
    for (;;)
 
320
      {
 
321
        if (outputWindow.getAvailable() == 0)
 
322
          {
 
323
            if (!decode())
 
324
              break;
 
325
          }
 
326
        else if (len > 0)
 
327
          {
 
328
            int more = outputWindow.copyOutput(buf, off, len);
 
329
            adler.update(buf, off, more);
 
330
            off += more;
 
331
            count += more;
 
332
            totalOut += more;
 
333
            len -= more;
 
334
          }
 
335
        else
 
336
          break;
 
337
      }
 
338
    return count;
 
339
  }
 
340
 
 
341
  /**
 
342
   * Returns true, if a preset dictionary is needed to inflate the input.
 
343
   */
 
344
  public boolean needsDictionary ()
 
345
  {
 
346
    return mode == DECODE_DICT && neededBits == 0;
 
347
  }
 
348
 
 
349
  /**
 
350
   * Returns true, if the input buffer is empty.
 
351
   * You should then call setInput(). <br>
 
352
   *
 
353
   * <em>NOTE</em>: This method also returns true when the stream is finished.
 
354
   */
 
355
  public boolean needsInput () 
 
356
  {
 
357
    return input == null || input.needsInput ();
 
358
  }
 
359
 
 
360
  /**
 
361
   * Resets the inflater so that a new stream can be decompressed.  All
 
362
   * pending input and output will be discarded.
 
363
   */
 
364
  public void reset ()
 
365
  {
 
366
    mode = nowrap ? DECODE_BLOCKS : DECODE_HEADER;
 
367
    totalIn = totalOut = 0;
 
368
    input.reset();
 
369
    outputWindow.reset();
 
370
    dynHeader = null;
 
371
    litlenTree = null;
 
372
    distTree = null;
 
373
    isLastBlock = false;
 
374
    adler.reset();
 
375
  }
 
376
 
 
377
  /**
 
378
   * Sets the preset dictionary.  This should only be called, if
 
379
   * needsDictionary() returns true and it should set the same
 
380
   * dictionary, that was used for deflating.  The getAdler()
 
381
   * function returns the checksum of the dictionary needed.
 
382
   * @param buffer the dictionary.
 
383
   * @exception IllegalStateException if no dictionary is needed.
 
384
   * @exception IllegalArgumentException if the dictionary checksum is
 
385
   * wrong.  
 
386
   */
 
387
  public void setDictionary (byte[] buffer)
 
388
  {
 
389
    setDictionary(buffer, 0, buffer.length);
 
390
  }
 
391
 
 
392
  /**
 
393
   * Sets the preset dictionary.  This should only be called, if
 
394
   * needsDictionary() returns true and it should set the same
 
395
   * dictionary, that was used for deflating.  The getAdler()
 
396
   * function returns the checksum of the dictionary needed.
 
397
   * @param buffer the dictionary.
 
398
   * @param off the offset into buffer where the dictionary starts.
 
399
   * @param len the length of the dictionary.
 
400
   * @exception IllegalStateException if no dictionary is needed.
 
401
   * @exception IllegalArgumentException if the dictionary checksum is
 
402
   * wrong.  
 
403
   * @exception IndexOutOfBoundsException if the off and/or len are wrong.
 
404
   */
 
405
  public void setDictionary (byte[] buffer, int off, int len)
 
406
  {
 
407
    if (!needsDictionary())
 
408
      throw new IllegalStateException();
 
409
 
 
410
    adler.update(buffer, off, len);
 
411
    if ((int) adler.getValue() != readAdler)
 
412
      throw new IllegalArgumentException("Wrong adler checksum");
 
413
    adler.reset();
 
414
    outputWindow.copyDict(buffer, off, len);
 
415
    mode = DECODE_BLOCKS;
 
416
  }
 
417
 
 
418
  /**
 
419
   * Sets the input.  This should only be called, if needsInput()
 
420
   * returns true.
 
421
   * @param buf the input.
 
422
   * @exception IllegalStateException if no input is needed.
 
423
   */
 
424
  public void setInput (byte[] buf) 
 
425
  {
 
426
    setInput (buf, 0, buf.length);
 
427
  }
 
428
 
 
429
  /**
 
430
   * Sets the input.  This should only be called, if needsInput()
 
431
   * returns true.
 
432
   * @param buf the input.
 
433
   * @param off the offset into buffer where the input starts.
 
434
   * @param len the length of the input.  
 
435
   * @exception IllegalStateException if no input is needed.
 
436
   * @exception IndexOutOfBoundsException if the off and/or len are wrong.
 
437
   */
 
438
  public void setInput (byte[] buf, int off, int len) 
 
439
  {
 
440
    input.setInput (buf, off, len);
 
441
    totalIn += len;
 
442
  }
 
443
 
 
444
  /**
 
445
   * Decodes the deflate header.
 
446
   * @return false if more input is needed. 
 
447
   * @exception DataFormatException if header is invalid.
 
448
   */
 
449
  private boolean decodeHeader () throws DataFormatException
 
450
  {
 
451
    int header = input.peekBits(16);
 
452
    if (header < 0)
 
453
      return false;
 
454
    input.dropBits(16);
 
455
    
 
456
    /* The header is written in "wrong" byte order */
 
457
    header = ((header << 8) | (header >> 8)) & 0xffff;
 
458
    if (header % 31 != 0)
 
459
      throw new DataFormatException("Header checksum illegal");
 
460
    
 
461
    if ((header & 0x0f00) != (Deflater.DEFLATED << 8))
 
462
      throw new DataFormatException("Compression Method unknown");
 
463
 
 
464
    /* Maximum size of the backwards window in bits. 
 
465
     * We currently ignore this, but we could use it to make the
 
466
     * inflater window more space efficient. On the other hand the
 
467
     * full window (15 bits) is needed most times, anyway.
 
468
     int max_wbits = ((header & 0x7000) >> 12) + 8;
 
469
     */
 
470
    
 
471
    if ((header & 0x0020) == 0) // Dictionary flag?
 
472
      {
 
473
        mode = DECODE_BLOCKS;
 
474
      }
 
475
    else
 
476
      {
 
477
        mode = DECODE_DICT;
 
478
        neededBits = 32;      
 
479
      }
 
480
    return true;
 
481
  }
 
482
   
 
483
  /**
 
484
   * Decodes the dictionary checksum after the deflate header.
 
485
   * @return false if more input is needed. 
 
486
   */
 
487
  private boolean decodeDict ()
 
488
  {
 
489
    while (neededBits > 0)
 
490
      {
 
491
        int dictByte = input.peekBits(8);
 
492
        if (dictByte < 0)
 
493
          return false;
 
494
        input.dropBits(8);
 
495
        readAdler = (readAdler << 8) | dictByte;
 
496
        neededBits -= 8;
 
497
      }
 
498
    return false;
 
499
  }
 
500
 
 
501
  /**
 
502
   * Decodes the huffman encoded symbols in the input stream.
 
503
   * @return false if more input is needed, true if output window is
 
504
   * full or the current block ends.
 
505
   * @exception DataFormatException if deflated stream is invalid.  
 
506
   */
 
507
  private boolean decodeHuffman () throws DataFormatException
 
508
  {
 
509
    int free = outputWindow.getFreeSpace();
 
510
    while (free >= 258)
 
511
      {
 
512
        int symbol;
 
513
        switch (mode)
 
514
          {
 
515
          case DECODE_HUFFMAN:
 
516
            /* This is the inner loop so it is optimized a bit */
 
517
            while (((symbol = litlenTree.getSymbol(input)) & ~0xff) == 0)
 
518
              {
 
519
                outputWindow.write(symbol);
 
520
                if (--free < 258)
 
521
                  return true;
 
522
              } 
 
523
            if (symbol < 257)
 
524
              {
 
525
                if (symbol < 0)
 
526
                  return false;
 
527
                else
 
528
                  {
 
529
                    /* symbol == 256: end of block */
 
530
                    distTree = null;
 
531
                    litlenTree = null;
 
532
                    mode = DECODE_BLOCKS;
 
533
                    return true;
 
534
                  }
 
535
              }
 
536
                
 
537
            try
 
538
              {
 
539
                repLength = CPLENS[symbol - 257];
 
540
                neededBits = CPLEXT[symbol - 257];
 
541
              }
 
542
            catch (ArrayIndexOutOfBoundsException ex)
 
543
              {
 
544
                throw new DataFormatException("Illegal rep length code");
 
545
              }
 
546
            /* fall through */
 
547
          case DECODE_HUFFMAN_LENBITS:
 
548
            if (neededBits > 0)
 
549
              {
 
550
                mode = DECODE_HUFFMAN_LENBITS;
 
551
                int i = input.peekBits(neededBits);
 
552
                if (i < 0)
 
553
                  return false;
 
554
                input.dropBits(neededBits);
 
555
                repLength += i;
 
556
              }
 
557
            mode = DECODE_HUFFMAN_DIST;
 
558
            /* fall through */
 
559
          case DECODE_HUFFMAN_DIST:
 
560
            symbol = distTree.getSymbol(input);
 
561
            if (symbol < 0)
 
562
              return false;
 
563
            try 
 
564
              {
 
565
                repDist = CPDIST[symbol];
 
566
                neededBits = CPDEXT[symbol];
 
567
              }
 
568
            catch (ArrayIndexOutOfBoundsException ex)
 
569
              {
 
570
                throw new DataFormatException("Illegal rep dist code");
 
571
              }
 
572
            /* fall through */
 
573
          case DECODE_HUFFMAN_DISTBITS:
 
574
            if (neededBits > 0)
 
575
              {
 
576
                mode = DECODE_HUFFMAN_DISTBITS;
 
577
                int i = input.peekBits(neededBits);
 
578
                if (i < 0)
 
579
                  return false;
 
580
                input.dropBits(neededBits);
 
581
                repDist += i;
 
582
              }
 
583
            outputWindow.repeat(repLength, repDist);
 
584
            free -= repLength;
 
585
            mode = DECODE_HUFFMAN;
 
586
            break;
 
587
          default:
 
588
            throw new IllegalStateException();
 
589
          }
 
590
      }
 
591
    return true;
 
592
  }
 
593
 
 
594
  /**
 
595
   * Decodes the adler checksum after the deflate stream.
 
596
   * @return false if more input is needed. 
 
597
   * @exception DataFormatException if checksum doesn't match.
 
598
   */
 
599
  private boolean decodeChksum () throws DataFormatException
 
600
  {
 
601
    while (neededBits > 0)
 
602
      {
 
603
        int chkByte = input.peekBits(8);
 
604
        if (chkByte < 0)
 
605
          return false;
 
606
        input.dropBits(8);
 
607
        readAdler = (readAdler << 8) | chkByte;
 
608
        neededBits -= 8;
 
609
      }
 
610
    if ((int) adler.getValue() != readAdler)
 
611
      throw new DataFormatException("Adler chksum doesn't match: "
 
612
                                    +Integer.toHexString((int)adler.getValue())
 
613
                                    +" vs. "+Integer.toHexString(readAdler));
 
614
    mode = FINISHED;
 
615
    return false;
 
616
  }
 
617
 
 
618
  /**
 
619
   * Decodes the deflated stream.
 
620
   * @return false if more input is needed, or if finished. 
 
621
   * @exception DataFormatException if deflated stream is invalid.
 
622
   */
 
623
  private boolean decode () throws DataFormatException
 
624
  {
 
625
    switch (mode) 
 
626
      {
 
627
      case DECODE_HEADER:
 
628
        return decodeHeader();
 
629
      case DECODE_DICT:
 
630
        return decodeDict();
 
631
      case DECODE_CHKSUM:
 
632
        return decodeChksum();
 
633
 
 
634
      case DECODE_BLOCKS:
 
635
        if (isLastBlock)
 
636
          {
 
637
            if (nowrap)
 
638
              {
 
639
                mode = FINISHED;
 
640
                return false;
 
641
              }
 
642
            else
 
643
              {
 
644
                input.skipToByteBoundary();
 
645
                neededBits = 32;
 
646
                mode = DECODE_CHKSUM;
 
647
                return true;
 
648
              }
 
649
          }
 
650
 
 
651
        int type = input.peekBits(3);
 
652
        if (type < 0)
 
653
          return false;
 
654
        input.dropBits(3);
 
655
 
 
656
        if ((type & 1) != 0)
 
657
          isLastBlock = true;
 
658
        switch (type >> 1)
 
659
          {
 
660
          case DeflaterConstants.STORED_BLOCK:
 
661
            input.skipToByteBoundary();
 
662
            mode = DECODE_STORED_LEN1;
 
663
            break;
 
664
          case DeflaterConstants.STATIC_TREES:
 
665
            litlenTree = InflaterHuffmanTree.defLitLenTree;
 
666
            distTree = InflaterHuffmanTree.defDistTree;
 
667
            mode = DECODE_HUFFMAN;
 
668
            break;
 
669
          case DeflaterConstants.DYN_TREES:
 
670
            dynHeader = new InflaterDynHeader();
 
671
            mode = DECODE_DYN_HEADER;
 
672
            break;
 
673
          default:
 
674
            throw new DataFormatException("Unknown block type "+type);
 
675
          }
 
676
        return true;
 
677
 
 
678
      case DECODE_STORED_LEN1:
 
679
        {
 
680
          if ((uncomprLen = input.peekBits(16)) < 0)
 
681
            return false;
 
682
          input.dropBits(16);
 
683
          mode = DECODE_STORED_LEN2;
 
684
        }
 
685
        /* fall through */
 
686
      case DECODE_STORED_LEN2:
 
687
        {
 
688
          int nlen = input.peekBits(16);
 
689
          if (nlen < 0)
 
690
            return false;
 
691
          input.dropBits(16);
 
692
          if (nlen != (uncomprLen ^ 0xffff))
 
693
            throw new DataFormatException("broken uncompressed block");
 
694
          mode = DECODE_STORED;
 
695
        }
 
696
        /* fall through */
 
697
      case DECODE_STORED:
 
698
        {
 
699
          int more = outputWindow.copyStored(input, uncomprLen);
 
700
          uncomprLen -= more;
 
701
          if (uncomprLen == 0)
 
702
            {
 
703
              mode = DECODE_BLOCKS;
 
704
              return true;
 
705
            }
 
706
          return !input.needsInput();
 
707
        }
 
708
 
 
709
      case DECODE_DYN_HEADER:
 
710
        if (!dynHeader.decode(input))
 
711
          return false;
 
712
        litlenTree = dynHeader.buildLitLenTree();
 
713
        distTree = dynHeader.buildDistTree();
 
714
        mode = DECODE_HUFFMAN;
 
715
        /* fall through */
 
716
      case DECODE_HUFFMAN:
 
717
      case DECODE_HUFFMAN_LENBITS:
 
718
      case DECODE_HUFFMAN_DIST:
 
719
      case DECODE_HUFFMAN_DISTBITS:
 
720
        return decodeHuffman();
 
721
      case FINISHED:
 
722
        return false;
 
723
      default:
 
724
        throw new IllegalStateException();
 
725
      } 
 
726
  }
 
727
}