/* * Copyright 2009 Google, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test * @bug 4206909 4813885 8191918 * @summary Test basic functionality of DeflaterOutputStream/InflaterInputStream * and GZIPOutputStream/GZIPInputStream, including flush * @key randomness
*/
privatestaticclass PairedInputStream extends ByteArrayInputStream { private PairedOutputStream out = null; private Random random;
public PairedInputStream() { // The ByteArrayInputStream needs to start with a buffer, but we // need to set it to have no data super(newbyte[1]);
count = 0;
pos = 0;
random = new Random(new Date().getTime());
}
try (DeflaterOutputStream dos = new DeflaterOutputStream(pos, true)) {
dos.write(data);
}
check(readFully(iis, buf, buf.length));
check(Arrays.equals(data, buf));
}
privatestaticvoid TestFlushableGZIPOutputStream() throws Throwable { var random = new Random(new Date().getTime());
var byteOutStream = new ByteArrayOutputStream(); var output = new FlushableGZIPOutputStream(byteOutStream);
var data = newbyte[random.nextInt(1024 * 1024)]; var buf = newbyte[data.length];
random.nextBytes(data);
output.write(data); for (int i=0; i<data.length; i++) {
output.write(data[i]);
}
output.flush(); for (int i=0; i<data.length; i++) {
output.write(data[i]);
}
output.write(data);
output.close();
var baos = new ByteArrayOutputStream(); try (var gzis = new GZIPInputStream(new
ByteArrayInputStream(byteOutStream.toByteArray()))) {
gzis.transferTo(baos);
} var decompressedBytes = baos.toByteArray();
check(decompressedBytes.length == data.length * 4);
}
privatestaticvoid check(InputStream is, OutputStream os) throws Throwable
{
Random random = new Random(new Date().getTime()); // Large writes for (int x = 0; x < 200 ; x++) { // byte[] data = new byte[random.nextInt(1024 * 1024)]; byte[] data = newbyte[1024]; byte[] buf = newbyte[data.length];
random.nextBytes(data);
// Small writes for (int x = 0; x < 2000 ; x++) { byte[] data = newbyte[random.nextInt(20) + 10]; byte[] buf = newbyte[data.length];
random.nextBytes(data);
os.write(data);
os.flush(); if (!readFully(is, buf, buf.length)) {
fail("Didn't read full buffer of " + buf.length);
}
check(Arrays.equals(data, buf));
}
String quit = "QUIT\r\n";
// Close it out
os.write(quit.getBytes());
os.close();
StringBuilder sb = new StringBuilder();
check(readLineIfAvailable(is, sb));
equal(sb.toString(), quit);
}
/** Check that written, flushed and read */ privatestaticvoid WriteFlushRead() throws Throwable {
PairedInputStream pis = new PairedInputStream();
InflaterInputStream iis = new InflaterInputStream(pis);
PairedOutputStream pos = new PairedOutputStream(pis);
pis.setPairedOutputStream(pos);
DeflaterOutputStream dos = new DeflaterOutputStream(pos, true);
check(iis, dos);
}
privatestaticvoid GZWriteFlushRead() throws Throwable {
PairedInputStream pis = new PairedInputStream();
PairedOutputStream pos = new PairedOutputStream(pis);
pis.setPairedOutputStream(pos);
GZIPOutputStream gos = new GZIPOutputStream(pos, true);
gos.flush(); // flush the head out, so gis can read
GZIPInputStream gis = new GZIPInputStream(pis);
check(gis, gos);
}
privatestaticvoid checkLOP(InputStream is, OutputStream os) throws Throwable
{ boolean flushed = false; int count = 0;
// Do at least a certain number of lines, but too many without a // flush means this test isn't testing anything while ((count < 10 && flushed) || (count < 1000 && !flushed)) {
String command = "PING " + count + "\r\n";
os.write(command.getBytes());
/** Validate that we need to use flush at least once on a line
* oriented protocol */ privatestaticvoid LineOrientedProtocol() throws Throwable {
PairedInputStream pis = new PairedInputStream();
InflaterInputStream iis = new InflaterInputStream(pis);
PairedOutputStream pos = new PairedOutputStream(pis);
pis.setPairedOutputStream(pos);
DeflaterOutputStream dos = new DeflaterOutputStream(pos, true);
checkLOP(iis, dos);
}
privatestaticvoid GZLineOrientedProtocol() throws Throwable {
PairedInputStream pis = new PairedInputStream();
PairedOutputStream pos = new PairedOutputStream(pis);
pis.setPairedOutputStream(pos);
GZIPOutputStream gos = new GZIPOutputStream(pos, true);
gos.flush(); // flush the head out, so gis can read
GZIPInputStream gis = new GZIPInputStream(pis);
/** * Here we make sure we have received data, so that the header has been for * sure written to the output stream already.
*/
@Override publicsynchronizedvoid write(byte[] bytes, int i, int i1) throws IOException { super.write(bytes, i, i1);
hasData = true;
}
@Override publicsynchronizedvoid flush() throws IOException { if (!hasData) { return; // do not allow the gzip header to be flushed on its own
}
// trick the deflater to flush /** * Now this is tricky: We force the Deflater to flush its data by * switching compression level. As yet, a perplexingly simple workaround * for * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html
*/ if (!def.finished()) {
def.setInput(EMPTYBYTEARRAY, 0, 0);
/* * Keep on calling deflate until it runs dry. The default implementation * only does it once and can therefore hold onto data when they need to be * flushed out.
*/
@Override protectedvoid deflate() throws IOException { int len; do {
len = def.deflate(buf, 0, buf.length); if (len > 0) {
out.write(buf, 0, len);
}
} while (len != 0);
}
}
¤ Dauer der Verarbeitung: 0.18 Sekunden
(vorverarbeitet)
¤
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 ist noch experimentell.