// Start a Process to generate the test data to stdout and stderr
ProcessBuilder pb = new ProcessBuilder(javaExe, "-classpath", classpath, "SkipTest$GenerateData");
System.out.printf("cmd: %s%n", pb.command());
Process process = pb.start();
/* *Verifythedataexpectedisreceivedmixingreadsandskip.
*/ try (InputStream in = process.getInputStream()) {
// Note: Process.getInputStream() actually returns a BufferedInputStream whose // skip() method works perfectly, partially obscuring the bug. Only when the // BufferedInputStream's buffer is drained, and it passes the skip() call to // the underlying native stream, does the problem occur.
long n = in.skip(-1); if (n != 0) { thrownew AssertionError("skip(-1) should return 0");
}
n = in.skip(0); if (n != 0) { thrownew AssertionError("skip(0) should return 0");
}
// Now iterate all the data blocks int header; for (int expectedHeader = 'A'; (header = in.read()) != -1; expectedHeader++) { // The header byte should be simple 'A' to 'Z'. // When the bug hits, we will get lowercase letters instead. if (header != expectedHeader) { thrownew AssertionError("header char wrong, expected: " +
expectedHeader + ", actual: " + header);
}
// Handle the data bytes. // If the correct number of bytes are not skipped, // then subsequent reads become out-of-sync; int remaining = BLOCK_SIZE; do {
remaining -= in.skip(remaining);
} while (remaining != 0);
}
n = in.skip(1); if (n != 0) { thrownew AssertionError("skip(1) at eof should return 0");
}
}
/** *Dothesameforthestandarderrorstream.
*/ try (InputStream in = process.getErrorStream()) { long n = in.skip(-1); if (n != 0) { thrownew AssertionError("skip(-1) should return 0");
}
n = in.skip(0); if (n != 0) { thrownew AssertionError("skip(0) should return 0");
}
// Now iterate all the data blocks int header; for (int expectedHeader = 'A'; (header = in.read()) != -1; expectedHeader++) { // The header byte should be simple 'A' to 'Z'. // When the bug hits, we will get lowercase letters instead. if (header != expectedHeader) { thrownew AssertionError("header char wrong, expected: " +
expectedHeader + ", actual: " + header);
}
// Handle the data bytes. // If the correct number of bytes are not skipped, // then subsequent reads become out-of-sync; int remaining = BLOCK_SIZE; do {
remaining -= in.skip(remaining);
} while (remaining != 0);
}
n = in.skip(1); if (n != 0) { thrownew AssertionError("skip(1) at eof should return 0");
}
}
}
publicstaticvoid main(String[] args) { // Generate test data containing a series of data blocks of length BLOCK_SIZE, // each with a one-byte header. For example's sake, the "header" is a capital letter, // and the "data" is the lowercase version of that letter repeated BLOCK_SIZE times: try (OutputStream out = new BufferedOutputStream(System.out)) { for (int header = 'A'; header <= 'Z'; header++) {
out.write(header);
int data = Character.toLowerCase(header); for (int i = 0; i < BLOCK_SIZE; i++) {
out.write(data);
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
} // Generate the same data to the error output try (OutputStream err = new BufferedOutputStream(System.err)) { for (int header = 'A'; header <= 'Z'; header++) {
err.write(header);
int data = Character.toLowerCase(header); for (int i = 0; i < BLOCK_SIZE; i++) {
err.write(data);
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
}
}
}
}
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.