author | kevinw |
Tue, 24 Feb 2009 19:03:58 +0000 | |
changeset 2166 | e01c813f9552 |
parent 2074 | 346a841b2dcc |
child 2438 | 21c111b51aa8 |
permissions | -rw-r--r-- |
2166
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
1 |
/* |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
2 |
* Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
4 |
* |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
7 |
* published by the Free Software Foundation. |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
8 |
* |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
13 |
* accompanied this code). |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
14 |
* |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
18 |
* |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
21 |
* have any questions. |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
22 |
*/ |
e01c813f9552
6809463: Missing license header in test LargeZipFile.java
kevinw
parents:
2074
diff
changeset
|
23 |
|
2074 | 24 |
import java.io.*; |
25 |
import java.nio.*; |
|
26 |
import java.util.*; |
|
27 |
import java.util.zip.*; |
|
28 |
||
29 |
public class LargeZipFile { |
|
30 |
// If true, don't delete large ZIP file created for test. |
|
31 |
static final boolean debug = System.getProperty("debug") != null; |
|
32 |
||
33 |
static final int DATA_LEN = 1024 * 1024; |
|
34 |
static final int DATA_SIZE = 8; |
|
35 |
||
36 |
static long fileSize = 3L * 1024L * 1024L * 1024L; // 3GB |
|
37 |
||
38 |
static boolean userFile = false; |
|
39 |
||
40 |
static byte[] data; |
|
41 |
static File largeFile; |
|
42 |
static String lastEntryName; |
|
43 |
||
44 |
/* args can be empty, in which case check a 3 GB file which is created for |
|
45 |
* this test (and then deleted). Or it can be a number, in which case |
|
46 |
* that designates the size of the file that's created for this test (and |
|
47 |
* then deleted). Or it can be the name of a file to use for the test, in |
|
48 |
* which case it is *not* deleted. Note that in this last case, the data |
|
49 |
* comparison might fail. |
|
50 |
*/ |
|
51 |
static void realMain (String[] args) throws Throwable { |
|
52 |
if (args.length > 0) { |
|
53 |
try { |
|
54 |
fileSize = Long.parseLong(args[0]); |
|
55 |
System.out.println("Testing with file of size " + fileSize); |
|
56 |
} catch (NumberFormatException ex) { |
|
57 |
largeFile = new File(args[0]); |
|
58 |
if (!largeFile.exists()) { |
|
59 |
throw new Exception("Specified file " + args[0] + " does not exist"); |
|
60 |
} |
|
61 |
userFile = true; |
|
62 |
System.out.println("Testing with user-provided file " + largeFile); |
|
63 |
} |
|
64 |
} |
|
65 |
File testDir = null; |
|
66 |
if (largeFile == null) { |
|
67 |
testDir = new File(System.getProperty("test.scratch", "."), |
|
68 |
"LargeZip"); |
|
69 |
if (testDir.exists()) { |
|
70 |
if (!testDir.delete()) { |
|
71 |
throw new Exception("Cannot delete already-existing test directory"); |
|
72 |
} |
|
73 |
} |
|
74 |
check(!testDir.exists() && testDir.mkdirs()); |
|
75 |
largeFile = new File(testDir, "largezip.zip"); |
|
76 |
createLargeZip(); |
|
77 |
} |
|
78 |
||
79 |
readLargeZip(); |
|
80 |
||
81 |
if (!userFile && !debug) { |
|
82 |
check(largeFile.delete()); |
|
83 |
check(testDir.delete()); |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
static void createLargeZip() throws Throwable { |
|
88 |
int iterations = DATA_LEN / DATA_SIZE; |
|
89 |
ByteBuffer bb = ByteBuffer.allocate(DATA_SIZE); |
|
90 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
91 |
for (int i = 0; i < iterations; i++) { |
|
92 |
bb.putDouble(0, Math.random()); |
|
93 |
baos.write(bb.array(), 0, DATA_SIZE); |
|
94 |
} |
|
95 |
data = baos.toByteArray(); |
|
96 |
||
97 |
ZipOutputStream zos = new ZipOutputStream( |
|
98 |
new BufferedOutputStream(new FileOutputStream(largeFile))); |
|
99 |
long length = 0; |
|
100 |
while (length < fileSize) { |
|
101 |
ZipEntry ze = new ZipEntry("entry-" + length); |
|
102 |
lastEntryName = ze.getName(); |
|
103 |
zos.putNextEntry(ze); |
|
104 |
zos.write(data, 0, data.length); |
|
105 |
zos.closeEntry(); |
|
106 |
length = largeFile.length(); |
|
107 |
} |
|
108 |
System.out.println("Last entry written is " + lastEntryName); |
|
109 |
zos.close(); |
|
110 |
} |
|
111 |
||
112 |
static void readLargeZip() throws Throwable { |
|
113 |
ZipFile zipFile = new ZipFile(largeFile); |
|
114 |
ZipEntry entry = null; |
|
115 |
String entryName = null; |
|
116 |
int count = 0; |
|
117 |
Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
|
118 |
while (entries.hasMoreElements()) { |
|
119 |
entry = entries.nextElement(); |
|
120 |
entryName = entry.getName(); |
|
121 |
count++; |
|
122 |
} |
|
123 |
System.out.println("Number of entries read: " + count); |
|
124 |
System.out.println("Last entry read is " + entryName); |
|
125 |
check(!entry.isDirectory()); |
|
126 |
if (check(entryName.equals(lastEntryName))) { |
|
127 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
128 |
InputStream is = zipFile.getInputStream(entry); |
|
129 |
byte buf[] = new byte[4096]; |
|
130 |
int len; |
|
131 |
while ((len = is.read(buf)) >= 0) { |
|
132 |
baos.write(buf, 0, len); |
|
133 |
} |
|
134 |
baos.close(); |
|
135 |
is.close(); |
|
136 |
check(Arrays.equals(data, baos.toByteArray())); |
|
137 |
} |
|
138 |
try { |
|
139 |
zipFile.close(); |
|
140 |
} catch (IOException ioe) {/* what can you do */ } |
|
141 |
} |
|
142 |
||
143 |
//--------------------- Infrastructure --------------------------- |
|
144 |
static volatile int passed = 0, failed = 0; |
|
145 |
static void pass() {passed++;} |
|
146 |
static void pass(String msg) {System.out.println(msg); passed++;} |
|
147 |
static void fail() {failed++; Thread.dumpStack();} |
|
148 |
static void fail(String msg) {System.out.println(msg); fail();} |
|
149 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
|
150 |
static void unexpected(Throwable t, String msg) { |
|
151 |
System.out.println(msg); failed++; t.printStackTrace();} |
|
152 |
static boolean check(boolean cond) {if (cond) pass(); else fail(); return cond;} |
|
153 |
static void equal(Object x, Object y) { |
|
154 |
if (x == null ? y == null : x.equals(y)) pass(); |
|
155 |
else fail(x + " not equal to " + y);} |
|
156 |
public static void main(String[] args) throws Throwable { |
|
157 |
try {realMain(args);} catch (Throwable t) {unexpected(t);} |
|
158 |
System.out.println("\nPassed = " + passed + " failed = " + failed); |
|
159 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
|
160 |
} |
|
161 |