publicclass GZIPInputStreamRead { publicstaticvoid main(String[] args) throws Throwable {
Random rnd = new Random(); for (int i = 1; i < 100; i++) { int members = rnd.nextInt(10) + 1;
ByteArrayOutputStream srcBAOS = new ByteArrayOutputStream();
ByteArrayOutputStream dstBAOS = new ByteArrayOutputStream(); for (int j = 0; j < members; j++) { byte[] src = newbyte[rnd.nextInt(8192) + 1];
rnd.nextBytes(src);
srcBAOS.write(src);
try (GZIPOutputStream gzos = new GZIPOutputStream(dstBAOS)) {
gzos.write(src);
}
} byte[] srcBytes = srcBAOS.toByteArray(); byte[] dstBytes = dstBAOS.toByteArray(); // try different size of buffer to read the // GZIPInputStream /* just for fun when running manually for(intj=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);
}
}
}
privatestaticvoid 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 = newbyte[src.length + 10]; byte[] buf = newbyte[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))) { thrownew RuntimeException( "GZIPInputStream reading failed! " + ", src.len=" + src.length + ", read=" + off);
}
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.