27 * concatenated .gz inputstream
28 */
29
30 import java.io.*;
31 import java.util.*;
32 import java.util.zip.*;
33
34 public class GZIPInputStreamRead {
35 public static void main(String[] args) throws Throwable {
36 Random rnd = new Random();
37 for (int i = 1; i < 100; i++) {
38 int members = rnd.nextInt(10) + 1;
39
40 ByteArrayOutputStream srcBAOS = new ByteArrayOutputStream();
41 ByteArrayOutputStream dstBAOS = new ByteArrayOutputStream();
42 for (int j = 0; j < members; j++) {
43 byte[] src = new byte[rnd.nextInt(8192) + 1];
44 rnd.nextBytes(src);
45 srcBAOS.write(src);
46
47 GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS);
48 gzos.write(src);
49 gzos.close();
50 }
51 byte[] srcBytes = srcBAOS.toByteArray();
52 byte[] dstBytes = dstBAOS.toByteArray();
53 // try different size of buffer to read the
54 // GZIPInputStream
55 /* just for fun when running manually
56 for (int j = 1; j < 10; j++) {
57 test(srcBytes, dstBytes, j);
58 }
59 */
60 for (int j = 0; j < 10; j++) {
61 int readBufSZ = rnd.nextInt(2048) + 1;
62 test(srcBytes,
63 dstBytes,
64 readBufSZ,
65 512); // the defualt buffer size
66 test(srcBytes,
67 dstBytes,
68 readBufSZ,
69 rnd.nextInt(4096) + 1);
70 }
71 }
72 }
73
74 private static void test(byte[] src, byte[] dst,
75 int readBufSize, int gzisBufSize)
76 throws Throwable
77 {
78 GZIPInputStream gzis = new GZIPInputStream(
79 new ByteArrayInputStream(dst),
80 gzisBufSize);
81 byte[] result = new byte[src.length + 10];
82 byte[] buf = new byte[readBufSize];
83 int n = 0;
84 int off = 0;
85
86 while ((n = gzis.read(buf, 0, buf.length)) != -1) {
87 System.arraycopy(buf, 0, result, off, n);
88 off += n;
89 // no range check, if overflow, let it fail
90 }
91 if (off != src.length || gzis.available() != 0 ||
92 !Arrays.equals(src, Arrays.copyOf(result, off))) {
93 throw new RuntimeException(
94 "GZIPInputStream reading failed! " +
95 ", src.len=" + src.length +
96 ", read=" + off);
97 }
98 gzis.close();
99 }
100 }
|
27 * concatenated .gz inputstream
28 */
29
30 import java.io.*;
31 import java.util.*;
32 import java.util.zip.*;
33
34 public class GZIPInputStreamRead {
35 public static void main(String[] args) throws Throwable {
36 Random rnd = new Random();
37 for (int i = 1; i < 100; i++) {
38 int members = rnd.nextInt(10) + 1;
39
40 ByteArrayOutputStream srcBAOS = new ByteArrayOutputStream();
41 ByteArrayOutputStream dstBAOS = new ByteArrayOutputStream();
42 for (int j = 0; j < members; j++) {
43 byte[] src = new byte[rnd.nextInt(8192) + 1];
44 rnd.nextBytes(src);
45 srcBAOS.write(src);
46
47 try (GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS)) {
48 gzos.write(src);
49 }
50 }
51 byte[] srcBytes = srcBAOS.toByteArray();
52 byte[] dstBytes = dstBAOS.toByteArray();
53 // try different size of buffer to read the
54 // GZIPInputStream
55 /* just for fun when running manually
56 for (int j = 1; j < 10; j++) {
57 test(srcBytes, dstBytes, j);
58 }
59 */
60 for (int j = 0; j < 10; j++) {
61 int readBufSZ = rnd.nextInt(2048) + 1;
62 test(srcBytes,
63 dstBytes,
64 readBufSZ,
65 512); // the defualt buffer size
66 test(srcBytes,
67 dstBytes,
68 readBufSZ,
69 rnd.nextInt(4096) + 1);
70 }
71 }
72 }
73
74 private static void test(byte[] src, byte[] dst,
75 int readBufSize, int gzisBufSize)
76 throws Throwable
77 {
78 try (ByteArrayInputStream bais = new ByteArrayInputStream(dst);
79 GZIPInputStream gzis = new GZIPInputStream(bais, gzisBufSize))
80 {
81 byte[] result = new byte[src.length + 10];
82 byte[] buf = new byte[readBufSize];
83 int n = 0;
84 int off = 0;
85
86 while ((n = gzis.read(buf, 0, buf.length)) != -1) {
87 System.arraycopy(buf, 0, result, off, n);
88 off += n;
89 // no range check, if overflow, let it fail
90 }
91 if (off != src.length || gzis.available() != 0 ||
92 !Arrays.equals(src, Arrays.copyOf(result, off))) {
93 throw new RuntimeException(
94 "GZIPInputStream reading failed! " +
95 ", src.len=" + src.length +
96 ", read=" + off);
97 }
98 }
99 }
100 }
|