jdk/test/java/util/zip/LargeZip.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8553 46c2babb1e44
child 10365 8a89ee820687
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8553
diff changeset
     2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
     4
 *
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2438
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2438
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    10
 *
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    15
 * accompanied this code).
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    16
 *
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2438
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2438
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2438
diff changeset
    23
 * questions.
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    24
 */
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    25
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    26
import java.io.*;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    27
import java.nio.*;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    28
import java.util.*;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    29
import java.util.zip.*;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    30
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    31
public class LargeZip {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    32
    // If true, don't delete large ZIP file created for test.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    33
    static final boolean debug = System.getProperty("debug") != null;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    34
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    35
    //static final int DATA_LEN = 1024 * 1024;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    36
    static final int DATA_LEN = 80 * 1024;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    37
    static final int DATA_SIZE = 8;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    38
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    39
    static long fileSize = 6L * 1024L * 1024L * 1024L; // 6GB
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    40
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    41
    static boolean userFile = false;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    42
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    43
    static byte[] data;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    44
    static File largeFile;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    45
    static String lastEntryName;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    46
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    47
    /* args can be empty, in which case check a 3 GB file which is created for
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    48
     * this test (and then deleted).  Or it can be a number, in which case
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    49
     * that designates the size of the file that's created for this test (and
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    50
     * then deleted).  Or it can be the name of a file to use for the test, in
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    51
     * which case it is *not* deleted.  Note that in this last case, the data
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    52
     * comparison might fail.
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    53
     */
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    54
    static void realMain (String[] args) throws Throwable {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    55
        if (args.length > 0) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    56
            try {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    57
                fileSize = Long.parseLong(args[0]);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    58
                System.out.println("Testing with file of size " + fileSize);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    59
            } catch (NumberFormatException ex) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    60
                largeFile = new File(args[0]);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    61
                if (!largeFile.exists()) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    62
                    throw new Exception("Specified file " + args[0] + " does not exist");
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    63
                }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    64
                userFile = true;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    65
                System.out.println("Testing with user-provided file " + largeFile);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    66
            }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    67
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    68
        File testDir = null;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    69
        if (largeFile == null) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    70
            testDir = new File(System.getProperty("test.scratch", "."),
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    71
                                    "LargeZip");
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    72
            if (testDir.exists()) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    73
                if (!testDir.delete()) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    74
                    throw new Exception("Cannot delete already-existing test directory");
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    75
                }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    76
            }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    77
            check(!testDir.exists() && testDir.mkdirs());
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    78
            largeFile = new File(testDir, "largezip.zip");
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    79
            createLargeZip();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    80
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    81
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    82
        readLargeZip1();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    83
        readLargeZip2();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    84
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    85
        if (!userFile && !debug) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    86
            check(largeFile.delete());
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    87
            check(testDir.delete());
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    88
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    89
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    90
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    91
    static void createLargeZip() throws Throwable {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    92
        int iterations = DATA_LEN / DATA_SIZE;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    93
        ByteBuffer bb = ByteBuffer.allocate(DATA_SIZE);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    94
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    95
        for (int i = 0; i < iterations; i++) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    96
            bb.putDouble(0, Math.random());
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    97
            baos.write(bb.array(), 0, DATA_SIZE);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    98
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
    99
        data = baos.toByteArray();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   100
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   101
        try (FileOutputStream fos = new FileOutputStream(largeFile);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   102
             BufferedOutputStream bos = new BufferedOutputStream(fos);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   103
             ZipOutputStream zos = new ZipOutputStream(bos))
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   104
        {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   105
            long length = 0;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   106
            while (length < fileSize) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   107
                ZipEntry ze = new ZipEntry("entry-" + length);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   108
                lastEntryName = ze.getName();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   109
                zos.putNextEntry(ze);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   110
                zos.write(data, 0, data.length);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   111
                zos.closeEntry();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   112
                length = largeFile.length();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   113
            }
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   114
            System.out.println("Last entry written is " + lastEntryName);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   115
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   116
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   117
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   118
    static void readLargeZip1() throws Throwable {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   119
        ZipFile zipFile = new ZipFile(largeFile);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   120
        ZipEntry entry = null;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   121
        String entryName = null;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   122
        int count = 0;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   123
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   124
        while (entries.hasMoreElements()) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   125
            entry = entries.nextElement();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   126
            entryName = entry.getName();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   127
            count++;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   128
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   129
        System.out.println("Number of entries read: " + count);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   130
        System.out.println("Last entry read is " + entryName);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   131
        check(!entry.isDirectory());
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   132
        if (check(entryName.equals(lastEntryName))) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   133
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   134
            InputStream is = zipFile.getInputStream(entry);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   135
            byte buf[] = new byte[4096];
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   136
            int len;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   137
            while ((len = is.read(buf)) >= 0) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   138
                baos.write(buf, 0, len);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   139
            }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   140
            baos.close();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   141
            is.close();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   142
            check(Arrays.equals(data, baos.toByteArray()));
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   143
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   144
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   145
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   146
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   147
    static void readLargeZip2() throws Throwable {
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   148
        try (FileInputStream fis = new FileInputStream(largeFile);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   149
             BufferedInputStream bis = new BufferedInputStream(fis);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   150
             ZipInputStream zis = new ZipInputStream(bis))
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   151
        {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   152
            ZipEntry entry = null;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   153
            String entryName = null;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   154
            int count = 0;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   155
            while ((entry = zis.getNextEntry()) != null) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   156
                entryName = entry.getName();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   157
                if (entryName.equals(lastEntryName)) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   158
                    break;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   159
                }
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   160
                count++;
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   161
            }
8553
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   162
            System.out.println("Number of entries read: " + count);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   163
            System.out.println("Last entry read is " + entryName);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   164
            check(!entry.isDirectory());
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   165
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   166
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   167
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   168
            byte buf[] = new byte[4096];
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   169
            int len;
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   170
            while ((len = zis.read(buf)) >= 0) {
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   171
                baos.write(buf, 0, len);
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   172
            }
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   173
            baos.close();
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   174
            check(Arrays.equals(data, baos.toByteArray()));
46c2babb1e44 7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents: 5506
diff changeset
   175
            check(zis.getNextEntry() == null);
2438
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   176
        }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   177
    }
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   178
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   179
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   180
    //--------------------- Infrastructure ---------------------------
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   181
    static volatile int passed = 0, failed = 0;
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   182
    static void pass() {passed++;}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   183
    static void pass(String msg) {System.out.println(msg); passed++;}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   184
    static void fail() {failed++; Thread.dumpStack();}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   185
    static void fail(String msg) {System.out.println(msg); fail();}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   186
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   187
    static void unexpected(Throwable t, String msg) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   188
        System.out.println(msg); failed++; t.printStackTrace();}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   189
    static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   190
    static void equal(Object x, Object y) {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   191
        if (x == null ? y == null : x.equals(y)) pass();
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   192
        else fail(x + " not equal to " + y);}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   193
    public static void main(String[] args) throws Throwable {
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   194
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   195
        System.out.println("\nPassed = " + passed + " failed = " + failed);
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   196
        if (failed > 0) throw new AssertionError("Some tests failed");}
21c111b51aa8 4681995: Add support for large (> 4GB) zip/jar files
sherman
parents:
diff changeset
   197
}