-
Notifications
You must be signed in to change notification settings - Fork 2
/
GzipReader.java
698 lines (653 loc) · 28.5 KB
/
GzipReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/**
* Java Web Archive Toolkit - Software to read and validate ARC, WARC
* and GZip files. (http://jwat.org/)
* Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jwat.gzip;
import org.jwat.common.ByteCountingPushBackInputStream;
import org.jwat.common.Diagnosis;
import org.jwat.common.DiagnosisType;
import org.jwat.common.Diagnostics;
import org.jwat.common.ISO8859_1;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.zip.CRC32;
import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
/**
* A reader for (multi-part) GZip files. Validates header and CRC's.
* Entries are read sequentially from the input stream. Random access is
* supported indirectly if the used input stream supports this.
* Compressed data is available through an uncompressing input stream wrapper.
*
* @author nicl
*/
public class GzipReader implements Closeable {
/** Buffer size to use when read skipping. */
public static final int SKIP_READ_BUFFER_SIZE = 8192;
/** Read buffer used by the skip method. */
protected byte[] skip_read_buffer = new byte[SKIP_READ_BUFFER_SIZE];
/** Default input buffer size. */
public static final int DEFAULT_INPUT_BUFFER_SIZE = 8192;
/** Input stream of GZip (multi-part) file. */
protected ByteCountingPushBackInputStream pbin;
/** Inflater used to uncompress GZip entries. */
protected Inflater inf = new Inflater( true );
/** Checksum object used to calculate CRC16 and CRC32 values. */
protected CRC32 crc = new CRC32();
/** Last number of bytes read into the input buffer. */
protected int lastInput;
/** Input buffer used to feed the inflater. */
protected byte[] inputBytes;
/** ISO-8859-1 validating de-/encoder. */
protected final ISO8859_1 iso8859_1 = new ISO8859_1();
/** Compliance status for records parsed up to now. */
protected boolean bIsCompliant = true;
/** Validation errors and warnings. */
public final Diagnostics<Diagnosis> diagnostics = new Diagnostics<Diagnosis>();
/** Entries read. */
protected int entries = 0;
/** Entry offset, updated each time an entry is closed. */
protected long startOffset = -1;
/** Number of bytes consumed by this reader. */
protected long consumed;
/** Current GZip entry object. */
protected GzipEntry gzipEntry;
/** Partial GZip entry which could not be completely read. */
public GzipEntry partialEntry;
/** Buffer used to read header. */
protected byte[] headerBytes = new byte[10];
/** Buffer used to read the XLEN value. */
protected byte[] xlenBytes = new byte[2];
/** Buffer used to read the FNAME data. */
protected byte[] fnameBytes;
/** Buffer used to read the FCOMMENT data. */
protected byte[] fcommentBytes;
/** Buffer used to read the CRC16 value. */
protected byte[] crc16Bytes = new byte[2];
/** Buffer used to read trailer. */
protected byte[] trailerBytes = new byte[8];
/**
* Check head of <code>PushBackInputStream</code> for a GZip magic number.
* The state of the <code>PushBackInputStream</code> is the same after the
* call as before the call.
* @param pbin <code>PushBackInputStream</code> with GZip entries
* @return boolean indicating presence of a GZip magic number
* @throws IOException if an I/O error occurs while examining head of stream
*/
public static boolean isGzipped(ByteCountingPushBackInputStream pbin) throws IOException {
if (pbin == null) {
throw new IllegalArgumentException("'pbin'is null!");
}
byte[] magicBytes = new byte[2];
int magicNumber = 0xdeadbeef;
// Look for the leading 2 magic bytes in front of every valid GZip entry.
int read = pbin.readFully(magicBytes);
if (read == 2) {
magicNumber = ((magicBytes[1] & 255) << 8) | (magicBytes[0] & 255);
}
if (read > 0) {
pbin.unread(magicBytes, 0, read);
}
return (magicNumber == GzipConstants.GZIP_MAGIC);
}
/**
* Construct a GZip reader with a default input buffer size of
* DEFAULT_INPUT_BUFFER_SIZE.
* @param in input stream of GZip file
*/
public GzipReader(InputStream in) {
if (in == null) {
throw new IllegalArgumentException("in is null!");
}
pbin = new ByteCountingPushBackInputStream(in, DEFAULT_INPUT_BUFFER_SIZE);
inputBytes = new byte[DEFAULT_INPUT_BUFFER_SIZE];
}
/**
* Construct a GZip reader with the specified input buffer size.
* @param in input stream of GZip file
* @param buffer_size input buffer size to use
*/
public GzipReader(InputStream in, int buffer_size) {
if (in == null) {
throw new IllegalArgumentException("in is null!");
}
if (buffer_size <= 0) {
throw new IllegalArgumentException(
"buffer_size is less or equals to zero: " + buffer_size);
}
in = new BufferedInputStream(in, buffer_size);
pbin = new ByteCountingPushBackInputStream(in, DEFAULT_INPUT_BUFFER_SIZE);
inputBytes = new byte[DEFAULT_INPUT_BUFFER_SIZE];
}
/**
* Release resources associated with this reader.
* Also closes the <code>InputStream</code> supplied to the constructor.
* @throws IOException if an I/O error occurs while closing reader
*/
public void close() throws IOException {
if (gzipEntry != null) {
gzipEntry.close();
startOffset = pbin.getConsumed();
gzipEntry = null;
}
if (inf != null) {
inf.end();
inf = null;
}
if (pbin != null){
pbin.close();
}
pbin = null;
}
/**
* Returns a boolean indicating whether all entries parsed so far are compliant.
* @return a boolean indicating whether all entries parsed so far are compliant
*/
public boolean isCompliant() {
return bIsCompliant;
}
/**
* Returns the offset of the current entry or -1 if none have been read.
* @return the offset of the current entry or -1
*/
public long getStartOffset() {
return startOffset;
}
/**
* Returns the current offset in the input stream. Which could be anywhere
* in the (multi-part) GZip file.
* @return current offset in the input stream
*/
public long getOffset() {
if (pbin != null) {
return pbin.getConsumed();
} else {
return consumed;
}
}
/**
* Get number of bytes consumed by this reader.
* Updated after each record has been read.
* @return number of bytes consumed by this reader
*/
public long getConsumed() {
return consumed;
}
/**
* Get the next GZip entry header and prepare the compressed data for
* input stream retrieval.
* @return GZip entry or null
* @throws IOException if an I/O error occurs while reading entry
*/
public GzipEntry getNextEntry() throws IOException {
if (gzipEntry != null) {
gzipEntry.close();
gzipEntry = null;
}
int read = pbin.readFully(headerBytes);
if (read == 10) {
try {
crc.reset();
inf.reset();
startOffset = pbin.getConsumed() - 10;
gzipEntry = new GzipEntry();
gzipEntry.reader = this;
gzipEntry.startOffset = startOffset;
/*
* Header.
*/
gzipEntry.magic = ((headerBytes[1] & 255) << 8) | (headerBytes[0] & 255);
gzipEntry.cm = (short)(headerBytes[2] & 255);
gzipEntry.flg = (short)(headerBytes[3] & 255);
gzipEntry.mtime = ((headerBytes[7] & 255) << 24) | ((headerBytes[6] & 255) << 16) | ((headerBytes[5] & 255) << 8) | (headerBytes[4] & 255);
gzipEntry.date = (gzipEntry.mtime != 0) ? new Date(gzipEntry.mtime * 1000) : null;
gzipEntry.xfl = (short)(headerBytes[8] & 255);
gzipEntry.os = (short)(headerBytes[9] & 255);
crc.update(headerBytes);
if (gzipEntry.magic != GzipConstants.GZIP_MAGIC) {
gzipEntry.diagnostics.addError(
new Diagnosis(
DiagnosisType.INVALID_EXPECTED,
"Magic Value",
Integer.toHexString(gzipEntry.magic),
Integer.toHexString(GzipConstants.GZIP_MAGIC)
)
);
}
if (gzipEntry.cm != GzipConstants.CM_DEFLATE) {
// Currently only the deflate compression method is supported in GZip.
gzipEntry.diagnostics.addError(
new Diagnosis(
DiagnosisType.INVALID_EXPECTED,
"Compression Method",
Integer.toHexString(gzipEntry.cm),
Integer.toHexString(GzipConstants.CM_DEFLATE)
)
);
} else {
// Currently only the deflate compression method is supported in GZip.
// Check to see whether some xfl reserved bits have been used.
if ((gzipEntry.xfl & GzipConstants.DEFLATE_XLF_RESERVED) != 0) {
gzipEntry.diagnostics.addWarning(
new Diagnosis(
DiagnosisType.RESERVED,
"eXtra FLags",
Integer.toHexString(gzipEntry.xfl & GzipConstants.DEFLATE_XLF_RESERVED)
)
);
}
if ((gzipEntry.xfl & GzipConstants.DEFLATE_XFL_COMPRESSION_MASK) == GzipConstants.DEFLATE_XFL_COMPRESSION_MASK) {
gzipEntry.diagnostics.addError(
new Diagnosis(
DiagnosisType.INVALID_DATA,
"eXtra FLags",
Integer.toHexString(gzipEntry.xfl & GzipConstants.DEFLATE_XFL_COMPRESSION_MASK)
)
);
}
}
if ((gzipEntry.flg & GzipConstants.FLG_FRESERVED) != 0) {
gzipEntry.diagnostics.addWarning(
new Diagnosis(
DiagnosisType.RESERVED,
"FLaGs",
Integer.toHexString(gzipEntry.flg & GzipConstants.FLG_FRESERVED)
)
);
}
// TODO Do not worry about unknown OS'es.
/*
if (!GzipConstants.osIdxStr.containsKey((int)gzipEntry.os)) {
gzipEntry.diagnostics.addWarning(
new Diagnosis(
DiagnosisType.UNKNOWN,
"Operating System",
Integer.toString(gzipEntry.os)
)
);
}
*/
/*
* FTEXT.
*/
if ((gzipEntry.flg & GzipConstants.FLG_FTEXT) == GzipConstants.FLG_FTEXT) {
gzipEntry.bFText = true;
}
/*
* FEXTRA.
*/
if ((gzipEntry.flg & GzipConstants.FLG_FEXTRA) == GzipConstants.FLG_FEXTRA) {
read = pbin.read(xlenBytes);
if (read == 2) {
gzipEntry.xlen = ((xlenBytes[1] & 255) << 8) | (xlenBytes[0] & 255);
if (gzipEntry.xlen > 0) {
gzipEntry.extraBytes = new byte[gzipEntry.xlen];
read = pbin.readFully(gzipEntry.extraBytes);
if (read != gzipEntry.xlen) {
throw new EOFException("Unexpected EOF!");
}
int idx = 0;
boolean b = true;
GzipExtraData extraData;
int len;
while (b) {
if (idx <= gzipEntry.extraBytes.length - 4) {
extraData = new GzipExtraData();
extraData.si1 = (byte)(gzipEntry.extraBytes[idx++] & 255);
extraData.si2 = (byte)(gzipEntry.extraBytes[idx++] & 255);
len = ((gzipEntry.extraBytes[idx+1] & 255) << 8) | (gzipEntry.extraBytes[idx] & 255);
idx += 2;
if (idx + len <= gzipEntry.extraBytes.length) {
extraData.data = new byte[len];
System.arraycopy(gzipEntry.extraBytes, idx, extraData.data, 0, len);
idx += len;
gzipEntry.extraData.add(extraData);
} else {
b = false;
}
} else {
b = false;
}
}
if (idx != gzipEntry.extraBytes.length) {
gzipEntry.diagnostics.addError(
new Diagnosis(
DiagnosisType.INVALID_DATA,
"FEXTRA",
"Invalid structure",
"Data truncated"
)
);
}
} else {
gzipEntry.extraBytes = new byte[0];
}
} else {
throw new EOFException("Unexpected EOF!");
}
crc.update(xlenBytes);
crc.update(gzipEntry.extraBytes);
}
/*
* FNAME.
*/
if ((gzipEntry.flg & GzipConstants.FLG_FNAME) == GzipConstants.FLG_FNAME) {
fnameBytes = readZeroTerminated();
if (fnameBytes == null) {
throw new EOFException("Unexpected EOF!");
}
if (!iso8859_1.decode(fnameBytes, "")) {
gzipEntry.diagnostics.addWarning(
new Diagnosis(
DiagnosisType.INVALID_ENCODING,
"FName",
iso8859_1.decoded,
"ISO-8859-1"
)
);
}
gzipEntry.fname = iso8859_1.decoded;
crc.update(fnameBytes);
crc.update(0);
}
/*
* FCOMMENT.
*/
if ((gzipEntry.flg & GzipConstants.FLG_FCOMMENT) == GzipConstants.FLG_FCOMMENT) {
fcommentBytes = readZeroTerminated();
if (fcommentBytes == null) {
throw new EOFException("Unexpected EOF!");
}
if (!iso8859_1.decode(fcommentBytes, "\n")) {
gzipEntry.diagnostics.addWarning(
new Diagnosis(
DiagnosisType.INVALID_ENCODING,
"FComment",
iso8859_1.decoded,
"ISO-8859-1"
)
);
}
gzipEntry.fcomment = iso8859_1.decoded;
crc.update(fcommentBytes);
crc.update(0);
}
/*
* FHCRC.
*/
if ((gzipEntry.flg & GzipConstants.FLG_FHCRC) == GzipConstants.FLG_FHCRC) {
read = pbin.read(crc16Bytes);
if (read == 2) {
gzipEntry.bFhCrc = true;
gzipEntry.crc16 = ((crc16Bytes[1] & 255) << 8) | (crc16Bytes[0] & 255);
} else {
throw new EOFException("Unexpected EOF!");
}
}
/*
* Computed crc16.
*/
gzipEntry.comp_crc16 = ((int)crc.getValue()) & 0x0000ffff;
crc.reset();
if (gzipEntry.crc16 != null && gzipEntry.crc16 != gzipEntry.comp_crc16) {
gzipEntry.diagnostics.addError(
new Diagnosis(
DiagnosisType.INVALID_EXPECTED,
"CRC16",
Integer.toHexString(gzipEntry.crc16),
Integer.toHexString(gzipEntry.comp_crc16)
)
);
}
/*
* Prepare Entry InputStream.
*/
lastInput = 0;
gzipEntry.in = new GzipEntryInputStream(this, gzipEntry);
// Compliance
if (gzipEntry.diagnostics.hasErrors() || gzipEntry.diagnostics.hasWarnings()) {
gzipEntry.bIsCompliant = false;
} else {
gzipEntry.bIsCompliant = true;
}
bIsCompliant &= gzipEntry.bIsCompliant;
++entries;
} catch (EOFException e) {
partialEntry = gzipEntry;
diagnostics.addError(new Diagnosis(DiagnosisType.INVALID_DATA, "GZip file", "Unexpected EOF!"));
bIsCompliant = false;
gzipEntry = null;
}
} else {
// Require one or more entries to be present.
if (entries == 0) {
diagnostics.addError(new Diagnosis(DiagnosisType.ERROR_EXPECTED, "GZip file", "One or more entries"));
bIsCompliant = false;
}
if (pbin.read() != -1) {
diagnostics.addError(new Diagnosis(DiagnosisType.INVALID_DATA, "GZip file", "Unexpected trailing data!"));
bIsCompliant = false;
}
}
return gzipEntry;
}
/**
* Read non-compressed zero terminated data.
* @return zero terminated data
* @throws IOException if an I/O error occurs while reading data
*/
protected byte[] readZeroTerminated() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream(32);
int b;
while ((b = pbin.read()) > 0) {
out.write(b);
}
return (b != -1) ? out.toByteArray() : null;
}
/**
* Read the non-compressed trailing 8 bytes with CRC32 and ISize values.
* @param entry Gzip entry
* @throws IOException if an I/O error occurs while reading data
*/
protected void readTrailer(GzipEntry entry) throws IOException {
int read = pbin.readFully(trailerBytes);
entry.consumed = pbin.getConsumed() - entry.startOffset;
entry.compressed_size = inf.getBytesRead();
entry.uncompressed_size = inf.getBytesWritten();
consumed += entry.consumed;
entry.reader = null;
if (read == 8) {
entry.crc32 = ((trailerBytes[3] & 255) << 24) | ((trailerBytes[2] & 255) << 16) | ((trailerBytes[1] & 255) << 8) | (trailerBytes[0] & 255);
entry.isize = ((trailerBytes[7] & 255) << 24) | ((trailerBytes[6] & 255) << 16) | ((trailerBytes[5] & 255) << 8) | (trailerBytes[4] & 255);
entry.comp_crc32 = (int)(crc.getValue() & 0xffffffff);
entry.comp_isize = (int)(inf.getBytesWritten() & 0xffffffff);
if (entry.comp_crc32 != entry.crc32) {
entry.diagnostics.addError(
new Diagnosis(
DiagnosisType.INVALID_EXPECTED,
"CRC32",
Integer.toHexString(entry.crc32),
Integer.toHexString(entry.comp_crc32)
)
);
}
if (entry.comp_isize != entry.isize) {
entry.diagnostics.addError(
new Diagnosis(
DiagnosisType.INVALID_EXPECTED,
"ISize",
Long.toString(entry.isize),
Long.toString(entry.comp_isize)
)
);
}
} else {
gzipEntry.diagnostics.addError(new Diagnosis(DiagnosisType.INVALID_DATA, "GZip file", "Unexpected EOF!"));
bIsCompliant = false;
}
// Compliance
if (gzipEntry.diagnostics.hasErrors() || gzipEntry.diagnostics.hasWarnings()) {
gzipEntry.bIsCompliant = false;
} else {
gzipEntry.bIsCompliant = true;
}
bIsCompliant &= gzipEntry.bIsCompliant;
}
/**
* Read and uncompress data into a buffer.
* @param b destination buffer for uncompressed data
* @param off offset in buffer
* @param len length of uncompressed data to read
* @return number of bytes uncompressed or -1
* @throws DataFormatException if the compressed data is in an unknown format
* @throws IOException if an I/O error occurs while reading data
*/
protected int readInflated(byte[] b, int off, int len)
throws DataFormatException, IOException {
int inflated = 0;
while((inflated = inf.inflate(b, off, len)) == 0) {
if (inf.finished()) {
return -1;
} else if (inf.needsDictionary()) {
gzipEntry.diagnostics.addError(new Diagnosis(DiagnosisType.INVALID_DATA, "GZip file", "Unexpected EOF!"));
bIsCompliant = false;
throw new DataFormatException("Dictionary needed!");
} else if (inf.needsInput()) {
lastInput = pbin.read(inputBytes, 0, inputBytes.length);
if (lastInput == -1) {
gzipEntry.diagnostics.addError(new Diagnosis(DiagnosisType.INVALID_DATA, "GZip file", "Unexpected EOF!"));
bIsCompliant = false;
throw new DataFormatException("Data missing!");
}
inf.setInput(inputBytes, 0, lastInput);
} else {
gzipEntry.diagnostics.addError(new Diagnosis(DiagnosisType.INVALID_DATA, "GZip file", "Unexpected EOF!"));
bIsCompliant = false;
throw new DataFormatException("Inflater malfunction!");
}
}
return inflated;
}
/**
* <code>InputStream</code> to expose GZip'ed data in a controlled fashion.
*
* @author nicl
*/
protected static class GzipEntryInputStream extends InputStream {
/** GZip reader used to inflate. */
GzipReader reader;
/** Associated GZip entry. */
GzipEntry gzipEntry;
/** End of uncompressed file status. */
boolean bEof = false;
/** Small buffer used by the read() method. */
byte[] singleByteArray = new byte[1];
/**
* Construct input stream bound to a specific reader and entry.
* @param reader GZip reader
* @param gzipEntry GZip entry
*/
public GzipEntryInputStream(GzipReader reader,
GzipEntry gzipEntry) {
this.reader = reader;
this.gzipEntry = gzipEntry;
}
@Override
public void close() throws IOException {
while (!bEof) {
skip(reader.skip_read_buffer.length);
}
reader = null;
gzipEntry = null;
singleByteArray = null;
bEof = true;
}
@Override
public int available() throws IOException {
return !bEof ? 1 : 0;
}
@Override
public int read() throws IOException {
return read(singleByteArray, 0, 1) != -1 ? (singleByteArray[0] & 255) : -1;
}
@Override
public int read(byte[] b) throws IOException {
return read(b, 0, b.length);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (bEof) {
return -1;
}
int read;
try {
read = reader.readInflated(b, off, len);
} catch (DataFormatException e) {
gzipEntry.diagnostics.addError(new Diagnosis(DiagnosisType.INVALID_DATA, "GZip file", "Unexpected EOF!"));
reader.bIsCompliant = false;
throw new IOException(e);
}
if (read != -1) {
reader.crc.update(b, off, read);
}
else {
int remaining = reader.inf.getRemaining();
if (remaining > reader.lastInput) {
throw new IOException("Remaining larger than lastInput!");
}
reader.pbin.unread(reader.inputBytes, reader.lastInput - remaining,
remaining);
bEof = true;
reader.readTrailer(gzipEntry);
}
return read;
}
@Override
public long skip(long n) throws IOException {
if (bEof) {
return 0;
}
long remaining = n;
long skipped = 0;
long readLast = 0;
while (remaining > 0 && readLast != -1) {
remaining -= readLast;
skipped += readLast;
if (remaining > 0) {
readLast = read(reader.skip_read_buffer, 0,
(int) Math.min(remaining, SKIP_READ_BUFFER_SIZE));
}
}
return skipped;
}
@Override
public boolean markSupported() {
return false;
}
@Override
public synchronized void mark(int readlimit) {
}
@Override
public synchronized void reset() throws IOException {
throw new UnsupportedOperationException();
}
}
}