T6836682.java revision 923:4fd20d5b7295
1/*
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @ignore
27 * @bug 6836682 7025988
28 * @summary JavacFileManager handles zip64 archives (64K+ entries and large file support)
29 * @compile  -XDignore.symbol.file T6836682.java Utils.java
30 * @run main T6836682
31 */
32import java.io.BufferedOutputStream;
33import java.io.File;
34import java.io.FileInputStream;
35import java.io.FileOutputStream;
36import java.io.IOException;
37import java.nio.file.Files;
38import java.nio.file.Path;
39import java.util.jar.JarOutputStream;
40import java.util.zip.ZipEntry;
41
42public class T6836682 {
43
44    private static final long GIGA = 1024 * 1024 * 1024;
45
46    static void createLargeFile(File outFile, long minlength) throws IOException {
47        FileOutputStream fos = null;
48        BufferedOutputStream bos = null;
49        byte[] buffer = new byte[Short.MAX_VALUE * 2];
50        try {
51            fos = new FileOutputStream(outFile);
52            bos = new BufferedOutputStream(fos);
53            long count = minlength / ( Short.MAX_VALUE * 2)  + 1;
54            for (long i = 0 ; i < count ; i++) {
55                bos.write(buffer);
56            }
57        } finally {
58            Utils.close(bos);
59            Utils.close(fos);
60        }
61        if (outFile.length() < minlength) {
62            throw new RuntimeException("could not create large file " + outFile.getAbsolutePath());
63        }
64    }
65
66    static void createJarWithLargeFile(File jarFile, File javaFile,
67            long minlength) throws IOException {
68        Utils.createClassFile(javaFile, null, true);
69        File largeFile = new File("large.data");
70        createLargeFile(largeFile, minlength);
71        String[] jarArgs = {
72            "0cvf",
73            jarFile.getAbsolutePath(),
74            largeFile.getName(),
75            Utils.getClassFileName(javaFile)
76        };
77        Utils.jarTool.run(jarArgs);
78        // deleted to prevent accidental linkage
79        new File(Utils.getClassFileName(javaFile)).delete();
80    }
81
82    static void createLargeJar(File jarFile, File javaFile) throws IOException {
83        File classFile = new File(Utils.getClassFileName(javaFile));
84        Utils.createClassFile(javaFile, null, true);
85        JarOutputStream jos = null;
86        FileInputStream fis = null;
87        try {
88            jos = new JarOutputStream(new FileOutputStream(jarFile));
89
90            for (int i = 0; i < Short.MAX_VALUE * 2 + 10; i++) {
91                jos.putNextEntry(new ZipEntry("X" + i + ".txt"));
92            }
93            jos.putNextEntry(new ZipEntry(classFile.getName()));
94            fis = new FileInputStream(classFile);
95            Utils.copyStream(fis, jos);
96        } finally {
97            Utils.close(jos);
98            Utils.close(fis);
99        }
100        // deleted to prevent accidental linkage
101        new File(Utils.getClassFileName(javaFile)).delete();
102    }
103
104    // a jar with entries exceeding 64k + a class file for the existential test
105    public static void testLargeJar(String... args) throws IOException {
106        File largeJar = new File("large.jar");
107        File javaFile = new File("Foo.java");
108        createLargeJar(largeJar, javaFile);
109
110        File testFile = new File("Bar.java");
111        try {
112            Utils.createJavaFile(testFile, javaFile);
113            if (!Utils.compile("-doe", "-verbose", "-cp",
114                    largeJar.getAbsolutePath(), testFile.getAbsolutePath())) {
115                throw new IOException("test failed");
116            }
117        } finally {
118            Utils.deleteFile(largeJar);
119        }
120    }
121
122    // a jar with an enormous file + a class file for the existential test
123    public static void testHugeJar(String... args) throws IOException {
124        final File largeJar = new File("huge.jar");
125        final File javaFile = new File("Foo.java");
126
127        final Path path = largeJar.getAbsoluteFile().getParentFile().toPath();
128        final long available = Files.getFileStore(path).getUsableSpace();
129        final long MAX_VALUE = 0xFFFF_FFFFL;
130
131        final long absolute  = MAX_VALUE + 1L;
132        final long required  = (long)(absolute * 1.1); // pad for sundries
133        System.out.println("\tavailable: " + available / GIGA + " GB");
134        System.out.println("\required: " + required / GIGA + " GB");
135
136        if (available > required) {
137            createJarWithLargeFile(largeJar, javaFile, absolute);
138            File testFile = new File("Bar.java");
139            Utils.createJavaFile(testFile, javaFile);
140            try {
141                if (!Utils.compile("-doe", "-verbose", "-cp",
142                        largeJar.getAbsolutePath(), testFile.getAbsolutePath())) {
143                    throw new IOException("test failed");
144                }
145            } finally {
146                Utils.deleteFile(largeJar);
147            }
148        } else {
149            System.out.println("Warning: test passes vacuously, requirements exceeds available space");
150        }
151    }
152
153    public static void main(String... args) throws IOException {
154        testLargeJar();
155        testHugeJar();
156    }
157}
158