author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8553 | 46c2babb1e44 |
child 30046 | cf2c86e1819e |
permissions | -rw-r--r-- |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz 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) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
4 |
* |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
8 |
* |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
13 |
* accompanied this code). |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
14 |
* |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
18 |
* |
6374
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5618
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5618
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5618
diff
changeset
|
21 |
* questions. |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
22 |
*/ |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
23 |
|
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
24 |
/* @test |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
25 |
* @bug 4691425 |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
26 |
* @summary Test the read and write of GZIPInput/OutputStream, including |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
27 |
* concatenated .gz inputstream |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
28 |
*/ |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
29 |
|
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
30 |
import java.io.*; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
31 |
import java.util.*; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
32 |
import java.util.zip.*; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
33 |
|
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
34 |
public class GZIPInputStreamRead { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
35 |
public static void main(String[] args) throws Throwable { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
36 |
Random rnd = new Random(); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
37 |
for (int i = 1; i < 100; i++) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
38 |
int members = rnd.nextInt(10) + 1; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
39 |
|
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
40 |
ByteArrayOutputStream srcBAOS = new ByteArrayOutputStream(); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
41 |
ByteArrayOutputStream dstBAOS = new ByteArrayOutputStream(); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
42 |
for (int j = 0; j < members; j++) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
43 |
byte[] src = new byte[rnd.nextInt(8192) + 1]; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
44 |
rnd.nextBytes(src); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
45 |
srcBAOS.write(src); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
46 |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
47 |
try (GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS)) { |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
48 |
gzos.write(src); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
49 |
} |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
50 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
51 |
byte[] srcBytes = srcBAOS.toByteArray(); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
52 |
byte[] dstBytes = dstBAOS.toByteArray(); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
53 |
// try different size of buffer to read the |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
54 |
// GZIPInputStream |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
55 |
/* just for fun when running manually |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
56 |
for (int j = 1; j < 10; j++) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
57 |
test(srcBytes, dstBytes, j); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
58 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
59 |
*/ |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
60 |
for (int j = 0; j < 10; j++) { |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
61 |
int readBufSZ = rnd.nextInt(2048) + 1; |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
62 |
test(srcBytes, |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
63 |
dstBytes, |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
64 |
readBufSZ, |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
65 |
512); // the defualt buffer size |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
66 |
test(srcBytes, |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
67 |
dstBytes, |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
68 |
readBufSZ, |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
69 |
rnd.nextInt(4096) + 1); |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
70 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
71 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
72 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
73 |
|
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
74 |
private static void test(byte[] src, byte[] dst, |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
75 |
int readBufSize, int gzisBufSize) |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
76 |
throws Throwable |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
77 |
{ |
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
78 |
try (ByteArrayInputStream bais = new ByteArrayInputStream(dst); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
79 |
GZIPInputStream gzis = new GZIPInputStream(bais, gzisBufSize)) |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
80 |
{ |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
81 |
byte[] result = new byte[src.length + 10]; |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
82 |
byte[] buf = new byte[readBufSize]; |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
83 |
int n = 0; |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
84 |
int off = 0; |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
85 |
|
8553
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
86 |
while ((n = gzis.read(buf, 0, buf.length)) != -1) { |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
87 |
System.arraycopy(buf, 0, result, off, n); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
88 |
off += n; |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
89 |
// no range check, if overflow, let it fail |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
90 |
} |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
91 |
if (off != src.length || gzis.available() != 0 || |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
92 |
!Arrays.equals(src, Arrays.copyOf(result, off))) { |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
93 |
throw new RuntimeException( |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
94 |
"GZIPInputStream reading failed! " + |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
95 |
", src.len=" + src.length + |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
96 |
", read=" + off); |
46c2babb1e44
7021582: convert jar/zip code and tests to use try-with-resources
smarks
parents:
6374
diff
changeset
|
97 |
} |
5618
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
98 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
99 |
} |
d17b52843430
4691425: GZIPInputStream fails to read concatenated .gz files
sherman
parents:
diff
changeset
|
100 |
} |