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