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);
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.