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