emul/mini/src/main/java/java/util/zip/ZipInputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Jan 2013 14:03:49 +0100
branchemul
changeset 611 9839e9a75bcf
parent 609 48ef38e9677e
child 694 0d277415ed02
permissions -rw-r--r--
Implementation of ZipInputStream
jaroslav@609
     1
/*
jaroslav@609
     2
 * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
jaroslav@609
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@609
     4
 *
jaroslav@609
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@609
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@609
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@609
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@609
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@609
    10
 *
jaroslav@609
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@609
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@609
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@609
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@609
    15
 * accompanied this code).
jaroslav@609
    16
 *
jaroslav@609
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@609
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@609
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@609
    20
 *
jaroslav@609
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@609
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@609
    23
 * questions.
jaroslav@609
    24
 */
jaroslav@609
    25
jaroslav@609
    26
package java.util.zip;
jaroslav@609
    27
jaroslav@609
    28
import java.io.InputStream;
jaroslav@609
    29
import java.io.IOException;
jaroslav@609
    30
import java.io.EOFException;
jaroslav@609
    31
import java.io.PushbackInputStream;
jaroslav@609
    32
import static java.util.zip.ZipConstants64.*;
jaroslav@611
    33
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@609
    34
jaroslav@609
    35
/**
jaroslav@609
    36
 * This class implements an input stream filter for reading files in the
jaroslav@609
    37
 * ZIP file format. Includes support for both compressed and uncompressed
jaroslav@609
    38
 * entries.
jaroslav@609
    39
 *
jaroslav@609
    40
 * @author      David Connelly
jaroslav@609
    41
 */
jaroslav@609
    42
public
jaroslav@609
    43
class ZipInputStream extends InflaterInputStream implements ZipConstants {
jaroslav@609
    44
    private ZipEntry entry;
jaroslav@609
    45
    private int flag;
jaroslav@609
    46
    private CRC32 crc = new CRC32();
jaroslav@609
    47
    private long remaining;
jaroslav@609
    48
    private byte[] tmpbuf = new byte[512];
jaroslav@609
    49
jaroslav@609
    50
    private static final int STORED = ZipEntry.STORED;
jaroslav@609
    51
    private static final int DEFLATED = ZipEntry.DEFLATED;
jaroslav@609
    52
jaroslav@609
    53
    private boolean closed = false;
jaroslav@609
    54
    // this flag is set to true after EOF has reached for
jaroslav@609
    55
    // one entry
jaroslav@609
    56
    private boolean entryEOF = false;
jaroslav@609
    57
jaroslav@609
    58
    /**
jaroslav@609
    59
     * Check to make sure that this stream has not been closed
jaroslav@609
    60
     */
jaroslav@609
    61
    private void ensureOpen() throws IOException {
jaroslav@609
    62
        if (closed) {
jaroslav@609
    63
            throw new IOException("Stream closed");
jaroslav@609
    64
        }
jaroslav@609
    65
    }
jaroslav@609
    66
jaroslav@609
    67
    /**
jaroslav@609
    68
     * Creates a new ZIP input stream.
jaroslav@609
    69
     *
jaroslav@609
    70
     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to
jaroslav@609
    71
     * decode the entry names.
jaroslav@609
    72
     *
jaroslav@609
    73
     * @param in the actual input stream
jaroslav@609
    74
     */
jaroslav@609
    75
    public ZipInputStream(InputStream in) {
jaroslav@611
    76
//        this(in, "UTF-8");
jaroslav@611
    77
        super(new PushbackInputStream(in, 512), new Inflater(true), 512);
jaroslav@611
    78
        usesDefaultInflater = true;
jaroslav@611
    79
        if(in == null) {
jaroslav@611
    80
            throw new NullPointerException("in is null");
jaroslav@611
    81
        }
jaroslav@609
    82
    }
jaroslav@609
    83
jaroslav@609
    84
    /**
jaroslav@609
    85
     * Creates a new ZIP input stream.
jaroslav@609
    86
     *
jaroslav@609
    87
     * @param in the actual input stream
jaroslav@609
    88
     *
jaroslav@609
    89
     * @param charset
jaroslav@609
    90
     *        The {@linkplain java.nio.charset.Charset charset} to be
jaroslav@609
    91
     *        used to decode the ZIP entry name (ignored if the
jaroslav@609
    92
     *        <a href="package-summary.html#lang_encoding"> language
jaroslav@609
    93
     *        encoding bit</a> of the ZIP entry's general purpose bit
jaroslav@609
    94
     *        flag is set).
jaroslav@609
    95
     *
jaroslav@609
    96
     * @since 1.7
jaroslav@611
    97
     *
jaroslav@609
    98
    public ZipInputStream(InputStream in, Charset charset) {
jaroslav@609
    99
        super(new PushbackInputStream(in, 512), new Inflater(true), 512);
jaroslav@609
   100
        usesDefaultInflater = true;
jaroslav@609
   101
        if(in == null) {
jaroslav@609
   102
            throw new NullPointerException("in is null");
jaroslav@609
   103
        }
jaroslav@609
   104
        if (charset == null)
jaroslav@609
   105
            throw new NullPointerException("charset is null");
jaroslav@609
   106
        this.zc = ZipCoder.get(charset);
jaroslav@609
   107
    }
jaroslav@611
   108
    */
jaroslav@609
   109
jaroslav@609
   110
    /**
jaroslav@609
   111
     * Reads the next ZIP file entry and positions the stream at the
jaroslav@609
   112
     * beginning of the entry data.
jaroslav@609
   113
     * @return the next ZIP file entry, or null if there are no more entries
jaroslav@609
   114
     * @exception ZipException if a ZIP file error has occurred
jaroslav@609
   115
     * @exception IOException if an I/O error has occurred
jaroslav@609
   116
     */
jaroslav@609
   117
    public ZipEntry getNextEntry() throws IOException {
jaroslav@609
   118
        ensureOpen();
jaroslav@609
   119
        if (entry != null) {
jaroslav@609
   120
            closeEntry();
jaroslav@609
   121
        }
jaroslav@609
   122
        crc.reset();
jaroslav@609
   123
        inf.reset();
jaroslav@609
   124
        if ((entry = readLOC()) == null) {
jaroslav@609
   125
            return null;
jaroslav@609
   126
        }
jaroslav@609
   127
        if (entry.method == STORED) {
jaroslav@609
   128
            remaining = entry.size;
jaroslav@609
   129
        }
jaroslav@609
   130
        entryEOF = false;
jaroslav@609
   131
        return entry;
jaroslav@609
   132
    }
jaroslav@609
   133
jaroslav@609
   134
    /**
jaroslav@609
   135
     * Closes the current ZIP entry and positions the stream for reading the
jaroslav@609
   136
     * next entry.
jaroslav@609
   137
     * @exception ZipException if a ZIP file error has occurred
jaroslav@609
   138
     * @exception IOException if an I/O error has occurred
jaroslav@609
   139
     */
jaroslav@609
   140
    public void closeEntry() throws IOException {
jaroslav@609
   141
        ensureOpen();
jaroslav@609
   142
        while (read(tmpbuf, 0, tmpbuf.length) != -1) ;
jaroslav@609
   143
        entryEOF = true;
jaroslav@609
   144
    }
jaroslav@609
   145
jaroslav@609
   146
    /**
jaroslav@609
   147
     * Returns 0 after EOF has reached for the current entry data,
jaroslav@609
   148
     * otherwise always return 1.
jaroslav@609
   149
     * <p>
jaroslav@609
   150
     * Programs should not count on this method to return the actual number
jaroslav@609
   151
     * of bytes that could be read without blocking.
jaroslav@609
   152
     *
jaroslav@609
   153
     * @return     1 before EOF and 0 after EOF has reached for current entry.
jaroslav@609
   154
     * @exception  IOException  if an I/O error occurs.
jaroslav@609
   155
     *
jaroslav@609
   156
     */
jaroslav@609
   157
    public int available() throws IOException {
jaroslav@609
   158
        ensureOpen();
jaroslav@609
   159
        if (entryEOF) {
jaroslav@609
   160
            return 0;
jaroslav@609
   161
        } else {
jaroslav@609
   162
            return 1;
jaroslav@609
   163
        }
jaroslav@609
   164
    }
jaroslav@609
   165
jaroslav@609
   166
    /**
jaroslav@609
   167
     * Reads from the current ZIP entry into an array of bytes.
jaroslav@609
   168
     * If <code>len</code> is not zero, the method
jaroslav@609
   169
     * blocks until some input is available; otherwise, no
jaroslav@609
   170
     * bytes are read and <code>0</code> is returned.
jaroslav@609
   171
     * @param b the buffer into which the data is read
jaroslav@609
   172
     * @param off the start offset in the destination array <code>b</code>
jaroslav@609
   173
     * @param len the maximum number of bytes read
jaroslav@609
   174
     * @return the actual number of bytes read, or -1 if the end of the
jaroslav@609
   175
     *         entry is reached
jaroslav@609
   176
     * @exception  NullPointerException if <code>b</code> is <code>null</code>.
jaroslav@609
   177
     * @exception  IndexOutOfBoundsException if <code>off</code> is negative,
jaroslav@609
   178
     * <code>len</code> is negative, or <code>len</code> is greater than
jaroslav@609
   179
     * <code>b.length - off</code>
jaroslav@609
   180
     * @exception ZipException if a ZIP file error has occurred
jaroslav@609
   181
     * @exception IOException if an I/O error has occurred
jaroslav@609
   182
     */
jaroslav@609
   183
    public int read(byte[] b, int off, int len) throws IOException {
jaroslav@609
   184
        ensureOpen();
jaroslav@609
   185
        if (off < 0 || len < 0 || off > b.length - len) {
jaroslav@609
   186
            throw new IndexOutOfBoundsException();
jaroslav@609
   187
        } else if (len == 0) {
jaroslav@609
   188
            return 0;
jaroslav@609
   189
        }
jaroslav@609
   190
jaroslav@609
   191
        if (entry == null) {
jaroslav@609
   192
            return -1;
jaroslav@609
   193
        }
jaroslav@609
   194
        switch (entry.method) {
jaroslav@609
   195
        case DEFLATED:
jaroslav@609
   196
            len = super.read(b, off, len);
jaroslav@609
   197
            if (len == -1) {
jaroslav@609
   198
                readEnd(entry);
jaroslav@609
   199
                entryEOF = true;
jaroslav@609
   200
                entry = null;
jaroslav@609
   201
            } else {
jaroslav@609
   202
                crc.update(b, off, len);
jaroslav@609
   203
            }
jaroslav@609
   204
            return len;
jaroslav@609
   205
        case STORED:
jaroslav@609
   206
            if (remaining <= 0) {
jaroslav@609
   207
                entryEOF = true;
jaroslav@609
   208
                entry = null;
jaroslav@609
   209
                return -1;
jaroslav@609
   210
            }
jaroslav@609
   211
            if (len > remaining) {
jaroslav@609
   212
                len = (int)remaining;
jaroslav@609
   213
            }
jaroslav@609
   214
            len = in.read(b, off, len);
jaroslav@609
   215
            if (len == -1) {
jaroslav@609
   216
                throw new ZipException("unexpected EOF");
jaroslav@609
   217
            }
jaroslav@609
   218
            crc.update(b, off, len);
jaroslav@609
   219
            remaining -= len;
jaroslav@609
   220
            if (remaining == 0 && entry.crc != crc.getValue()) {
jaroslav@609
   221
                throw new ZipException(
jaroslav@609
   222
                    "invalid entry CRC (expected 0x" + Long.toHexString(entry.crc) +
jaroslav@609
   223
                    " but got 0x" + Long.toHexString(crc.getValue()) + ")");
jaroslav@609
   224
            }
jaroslav@609
   225
            return len;
jaroslav@609
   226
        default:
jaroslav@609
   227
            throw new ZipException("invalid compression method");
jaroslav@609
   228
        }
jaroslav@609
   229
    }
jaroslav@609
   230
jaroslav@609
   231
    /**
jaroslav@609
   232
     * Skips specified number of bytes in the current ZIP entry.
jaroslav@609
   233
     * @param n the number of bytes to skip
jaroslav@609
   234
     * @return the actual number of bytes skipped
jaroslav@609
   235
     * @exception ZipException if a ZIP file error has occurred
jaroslav@609
   236
     * @exception IOException if an I/O error has occurred
jaroslav@609
   237
     * @exception IllegalArgumentException if n < 0
jaroslav@609
   238
     */
jaroslav@609
   239
    public long skip(long n) throws IOException {
jaroslav@609
   240
        if (n < 0) {
jaroslav@609
   241
            throw new IllegalArgumentException("negative skip length");
jaroslav@609
   242
        }
jaroslav@609
   243
        ensureOpen();
jaroslav@609
   244
        int max = (int)Math.min(n, Integer.MAX_VALUE);
jaroslav@609
   245
        int total = 0;
jaroslav@609
   246
        while (total < max) {
jaroslav@609
   247
            int len = max - total;
jaroslav@609
   248
            if (len > tmpbuf.length) {
jaroslav@609
   249
                len = tmpbuf.length;
jaroslav@609
   250
            }
jaroslav@609
   251
            len = read(tmpbuf, 0, len);
jaroslav@609
   252
            if (len == -1) {
jaroslav@609
   253
                entryEOF = true;
jaroslav@609
   254
                break;
jaroslav@609
   255
            }
jaroslav@609
   256
            total += len;
jaroslav@609
   257
        }
jaroslav@609
   258
        return total;
jaroslav@609
   259
    }
jaroslav@609
   260
jaroslav@609
   261
    /**
jaroslav@609
   262
     * Closes this input stream and releases any system resources associated
jaroslav@609
   263
     * with the stream.
jaroslav@609
   264
     * @exception IOException if an I/O error has occurred
jaroslav@609
   265
     */
jaroslav@609
   266
    public void close() throws IOException {
jaroslav@609
   267
        if (!closed) {
jaroslav@609
   268
            super.close();
jaroslav@609
   269
            closed = true;
jaroslav@609
   270
        }
jaroslav@609
   271
    }
jaroslav@609
   272
jaroslav@609
   273
    private byte[] b = new byte[256];
jaroslav@609
   274
jaroslav@609
   275
    /*
jaroslav@609
   276
     * Reads local file (LOC) header for next entry.
jaroslav@609
   277
     */
jaroslav@609
   278
    private ZipEntry readLOC() throws IOException {
jaroslav@609
   279
        try {
jaroslav@609
   280
            readFully(tmpbuf, 0, LOCHDR);
jaroslav@609
   281
        } catch (EOFException e) {
jaroslav@609
   282
            return null;
jaroslav@609
   283
        }
jaroslav@609
   284
        if (get32(tmpbuf, 0) != LOCSIG) {
jaroslav@609
   285
            return null;
jaroslav@609
   286
        }
jaroslav@609
   287
        // get flag first, we need check EFS.
jaroslav@609
   288
        flag = get16(tmpbuf, LOCFLG);
jaroslav@609
   289
        // get the entry name and create the ZipEntry first
jaroslav@609
   290
        int len = get16(tmpbuf, LOCNAM);
jaroslav@609
   291
        int blen = b.length;
jaroslav@609
   292
        if (len > blen) {
jaroslav@609
   293
            do
jaroslav@609
   294
                blen = blen * 2;
jaroslav@609
   295
            while (len > blen);
jaroslav@609
   296
            b = new byte[blen];
jaroslav@609
   297
        }
jaroslav@609
   298
        readFully(b, 0, len);
jaroslav@609
   299
        // Force to use UTF-8 if the EFS bit is ON, even the cs is NOT UTF-8
jaroslav@609
   300
        ZipEntry e = createZipEntry(((flag & EFS) != 0)
jaroslav@611
   301
                                    ? toStringUTF8(b, len)
jaroslav@611
   302
                                    : toString(b, len));
jaroslav@609
   303
        // now get the remaining fields for the entry
jaroslav@609
   304
        if ((flag & 1) == 1) {
jaroslav@609
   305
            throw new ZipException("encrypted ZIP entry not supported");
jaroslav@609
   306
        }
jaroslav@609
   307
        e.method = get16(tmpbuf, LOCHOW);
jaroslav@609
   308
        e.time = get32(tmpbuf, LOCTIM);
jaroslav@609
   309
        if ((flag & 8) == 8) {
jaroslav@609
   310
            /* "Data Descriptor" present */
jaroslav@609
   311
            if (e.method != DEFLATED) {
jaroslav@609
   312
                throw new ZipException(
jaroslav@609
   313
                        "only DEFLATED entries can have EXT descriptor");
jaroslav@609
   314
            }
jaroslav@609
   315
        } else {
jaroslav@609
   316
            e.crc = get32(tmpbuf, LOCCRC);
jaroslav@609
   317
            e.csize = get32(tmpbuf, LOCSIZ);
jaroslav@609
   318
            e.size = get32(tmpbuf, LOCLEN);
jaroslav@609
   319
        }
jaroslav@609
   320
        len = get16(tmpbuf, LOCEXT);
jaroslav@609
   321
        if (len > 0) {
jaroslav@609
   322
            byte[] bb = new byte[len];
jaroslav@609
   323
            readFully(bb, 0, len);
jaroslav@609
   324
            e.setExtra(bb);
jaroslav@609
   325
            // extra fields are in "HeaderID(2)DataSize(2)Data... format
jaroslav@609
   326
            if (e.csize == ZIP64_MAGICVAL || e.size == ZIP64_MAGICVAL) {
jaroslav@609
   327
                int off = 0;
jaroslav@609
   328
                while (off + 4 < len) {
jaroslav@609
   329
                    int sz = get16(bb, off + 2);
jaroslav@609
   330
                    if (get16(bb, off) == ZIP64_EXTID) {
jaroslav@609
   331
                        off += 4;
jaroslav@609
   332
                        // LOC extra zip64 entry MUST include BOTH original and
jaroslav@609
   333
                        // compressed file size fields
jaroslav@609
   334
                        if (sz < 16 || (off + sz) > len ) {
jaroslav@609
   335
                            // Invalid zip64 extra fields, simply skip. Even it's
jaroslav@609
   336
                            // rare, it's possible the entry size happens to be
jaroslav@609
   337
                            // the magic value and it "accidnetly" has some bytes
jaroslav@609
   338
                            // in extra match the id.
jaroslav@609
   339
                            return e;
jaroslav@609
   340
                        }
jaroslav@609
   341
                        e.size = get64(bb, off);
jaroslav@609
   342
                        e.csize = get64(bb, off + 8);
jaroslav@609
   343
                        break;
jaroslav@609
   344
                    }
jaroslav@609
   345
                    off += (sz + 4);
jaroslav@609
   346
                }
jaroslav@609
   347
            }
jaroslav@609
   348
        }
jaroslav@609
   349
        return e;
jaroslav@609
   350
    }
jaroslav@609
   351
jaroslav@609
   352
    /**
jaroslav@609
   353
     * Creates a new <code>ZipEntry</code> object for the specified
jaroslav@609
   354
     * entry name.
jaroslav@609
   355
     *
jaroslav@609
   356
     * @param name the ZIP file entry name
jaroslav@609
   357
     * @return the ZipEntry just created
jaroslav@609
   358
     */
jaroslav@609
   359
    protected ZipEntry createZipEntry(String name) {
jaroslav@609
   360
        return new ZipEntry(name);
jaroslav@609
   361
    }
jaroslav@609
   362
jaroslav@609
   363
    /*
jaroslav@609
   364
     * Reads end of deflated entry as well as EXT descriptor if present.
jaroslav@609
   365
     */
jaroslav@609
   366
    private void readEnd(ZipEntry e) throws IOException {
jaroslav@609
   367
        int n = inf.getRemaining();
jaroslav@609
   368
        if (n > 0) {
jaroslav@609
   369
            ((PushbackInputStream)in).unread(buf, len - n, n);
jaroslav@609
   370
        }
jaroslav@609
   371
        if ((flag & 8) == 8) {
jaroslav@609
   372
            /* "Data Descriptor" present */
jaroslav@609
   373
            if (inf.getBytesWritten() > ZIP64_MAGICVAL ||
jaroslav@609
   374
                inf.getBytesRead() > ZIP64_MAGICVAL) {
jaroslav@609
   375
                // ZIP64 format
jaroslav@609
   376
                readFully(tmpbuf, 0, ZIP64_EXTHDR);
jaroslav@609
   377
                long sig = get32(tmpbuf, 0);
jaroslav@609
   378
                if (sig != EXTSIG) { // no EXTSIG present
jaroslav@609
   379
                    e.crc = sig;
jaroslav@609
   380
                    e.csize = get64(tmpbuf, ZIP64_EXTSIZ - ZIP64_EXTCRC);
jaroslav@609
   381
                    e.size = get64(tmpbuf, ZIP64_EXTLEN - ZIP64_EXTCRC);
jaroslav@609
   382
                    ((PushbackInputStream)in).unread(
jaroslav@609
   383
                        tmpbuf, ZIP64_EXTHDR - ZIP64_EXTCRC - 1, ZIP64_EXTCRC);
jaroslav@609
   384
                } else {
jaroslav@609
   385
                    e.crc = get32(tmpbuf, ZIP64_EXTCRC);
jaroslav@609
   386
                    e.csize = get64(tmpbuf, ZIP64_EXTSIZ);
jaroslav@609
   387
                    e.size = get64(tmpbuf, ZIP64_EXTLEN);
jaroslav@609
   388
                }
jaroslav@609
   389
            } else {
jaroslav@609
   390
                readFully(tmpbuf, 0, EXTHDR);
jaroslav@609
   391
                long sig = get32(tmpbuf, 0);
jaroslav@609
   392
                if (sig != EXTSIG) { // no EXTSIG present
jaroslav@609
   393
                    e.crc = sig;
jaroslav@609
   394
                    e.csize = get32(tmpbuf, EXTSIZ - EXTCRC);
jaroslav@609
   395
                    e.size = get32(tmpbuf, EXTLEN - EXTCRC);
jaroslav@609
   396
                    ((PushbackInputStream)in).unread(
jaroslav@609
   397
                                               tmpbuf, EXTHDR - EXTCRC - 1, EXTCRC);
jaroslav@609
   398
                } else {
jaroslav@609
   399
                    e.crc = get32(tmpbuf, EXTCRC);
jaroslav@609
   400
                    e.csize = get32(tmpbuf, EXTSIZ);
jaroslav@609
   401
                    e.size = get32(tmpbuf, EXTLEN);
jaroslav@609
   402
                }
jaroslav@609
   403
            }
jaroslav@609
   404
        }
jaroslav@609
   405
        if (e.size != inf.getBytesWritten()) {
jaroslav@609
   406
            throw new ZipException(
jaroslav@609
   407
                "invalid entry size (expected " + e.size +
jaroslav@609
   408
                " but got " + inf.getBytesWritten() + " bytes)");
jaroslav@609
   409
        }
jaroslav@609
   410
        if (e.csize != inf.getBytesRead()) {
jaroslav@609
   411
            throw new ZipException(
jaroslav@609
   412
                "invalid entry compressed size (expected " + e.csize +
jaroslav@609
   413
                " but got " + inf.getBytesRead() + " bytes)");
jaroslav@609
   414
        }
jaroslav@609
   415
        if (e.crc != crc.getValue()) {
jaroslav@609
   416
            throw new ZipException(
jaroslav@609
   417
                "invalid entry CRC (expected 0x" + Long.toHexString(e.crc) +
jaroslav@609
   418
                " but got 0x" + Long.toHexString(crc.getValue()) + ")");
jaroslav@609
   419
        }
jaroslav@609
   420
    }
jaroslav@609
   421
jaroslav@609
   422
    /*
jaroslav@609
   423
     * Reads bytes, blocking until all bytes are read.
jaroslav@609
   424
     */
jaroslav@609
   425
    private void readFully(byte[] b, int off, int len) throws IOException {
jaroslav@609
   426
        while (len > 0) {
jaroslav@609
   427
            int n = in.read(b, off, len);
jaroslav@609
   428
            if (n == -1) {
jaroslav@609
   429
                throw new EOFException();
jaroslav@609
   430
            }
jaroslav@609
   431
            off += n;
jaroslav@609
   432
            len -= n;
jaroslav@609
   433
        }
jaroslav@609
   434
    }
jaroslav@609
   435
jaroslav@609
   436
    /*
jaroslav@609
   437
     * Fetches unsigned 16-bit value from byte array at specified offset.
jaroslav@609
   438
     * The bytes are assumed to be in Intel (little-endian) byte order.
jaroslav@609
   439
     */
jaroslav@609
   440
    private static final int get16(byte b[], int off) {
jaroslav@609
   441
        return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
jaroslav@609
   442
    }
jaroslav@609
   443
jaroslav@609
   444
    /*
jaroslav@609
   445
     * Fetches unsigned 32-bit value from byte array at specified offset.
jaroslav@609
   446
     * The bytes are assumed to be in Intel (little-endian) byte order.
jaroslav@609
   447
     */
jaroslav@609
   448
    private static final long get32(byte b[], int off) {
jaroslav@609
   449
        return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
jaroslav@609
   450
    }
jaroslav@609
   451
jaroslav@609
   452
    /*
jaroslav@609
   453
     * Fetches signed 64-bit value from byte array at specified offset.
jaroslav@609
   454
     * The bytes are assumed to be in Intel (little-endian) byte order.
jaroslav@609
   455
     */
jaroslav@609
   456
    private static final long get64(byte b[], int off) {
jaroslav@609
   457
        return get32(b, off) | (get32(b, off+4) << 32);
jaroslav@609
   458
    }
jaroslav@611
   459
jaroslav@611
   460
    private static String toStringUTF8(byte[] arr, int len) {
jaroslav@611
   461
        return new String(arr, 0, len);
jaroslav@611
   462
    }
jaroslav@611
   463
    
jaroslav@611
   464
    private static String toString(byte[] b, int len) {
jaroslav@611
   465
        return new String(b, 0, len);
jaroslav@611
   466
    }
jaroslav@609
   467
}