GZIPInputStreamRead.java revision 3909
0N/A/*
0N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4691425
0N/A * @summary Test the read and write of GZIPInput/OutputStream, including
0N/A * concatenated .gz inputstream
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.util.zip.*;
0N/A
0N/Apublic class GZIPInputStreamRead {
0N/A public static void main(String[] args) throws Throwable {
0N/A Random rnd = new Random();
0N/A for (int i = 1; i < 100; i++) {
0N/A int members = rnd.nextInt(10) + 1;
0N/A
0N/A ByteArrayOutputStream srcBAOS = new ByteArrayOutputStream();
0N/A ByteArrayOutputStream dstBAOS = new ByteArrayOutputStream();
0N/A for (int j = 0; j < members; j++) {
0N/A byte[] src = new byte[rnd.nextInt(8192) + 1];
0N/A rnd.nextBytes(src);
0N/A srcBAOS.write(src);
0N/A
0N/A try (GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS)) {
0N/A gzos.write(src);
0N/A }
0N/A }
0N/A byte[] srcBytes = srcBAOS.toByteArray();
0N/A byte[] dstBytes = dstBAOS.toByteArray();
0N/A // try different size of buffer to read the
// GZIPInputStream
/* just for fun when running manually
for (int j = 1; j < 10; j++) {
test(srcBytes, dstBytes, j);
}
*/
for (int j = 0; j < 10; j++) {
int readBufSZ = rnd.nextInt(2048) + 1;
test(srcBytes,
dstBytes,
readBufSZ,
512); // the defualt buffer size
test(srcBytes,
dstBytes,
readBufSZ,
rnd.nextInt(4096) + 1);
}
}
}
private static void test(byte[] src, byte[] dst,
int readBufSize, int gzisBufSize)
throws Throwable
{
try (ByteArrayInputStream bais = new ByteArrayInputStream(dst);
GZIPInputStream gzis = new GZIPInputStream(bais, gzisBufSize))
{
byte[] result = new byte[src.length + 10];
byte[] buf = new byte[readBufSize];
int n = 0;
int off = 0;
while ((n = gzis.read(buf, 0, buf.length)) != -1) {
System.arraycopy(buf, 0, result, off, n);
off += n;
// no range check, if overflow, let it fail
}
if (off != src.length || gzis.available() != 0 ||
!Arrays.equals(src, Arrays.copyOf(result, off))) {
throw new RuntimeException(
"GZIPInputStream reading failed! " +
", src.len=" + src.length +
", read=" + off);
}
}
}
}