// tests input stream with a length between 1k and 4k
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(1024, 4096));
// tests input stream with several data chunks, as 16k is more than a // single chunk can hold
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(16384, 16384));
// tests randomly chosen starting positions within source and // target stream for (int i = 0; i < ITERATIONS; i++) { byte[] inBytes = createRandomBytes(MIN_SIZE, MAX_SIZE_INCR); int posIn = RND.nextInt(inBytes.length); int posOut = RND.nextInt(MIN_SIZE);
checkTransferredContents(inputStreamProvider, outputStreamProvider, inBytes, posIn, posOut);
}
// tests reading beyond source EOF (must not transfer any bytes)
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(4096, 0), 4096, 0);
/* *Specialtestforfile-to-filetransferofmorethan2GB.Thistest *coversmultipleiterationsofFileChannel.transerTo(FileChannel), *whichChannelInputStream.transferTo()onlyappliesinthisparticular *case,andcannotgettestedusingasinglebyte[]duetosizelimitation *ofarrays.
*/
@Test publicvoid testMoreThanTwoGB() throws IOException { // prepare two temporary files to be compared at the end of the test // set the source file name
String sourceName = String.format("test3GBSource%s.tmp",
String.valueOf(RND.nextInt(Integer.MAX_VALUE)));
Path sourceFile = CWD.resolve(sourceName);
try { // set the target file name
String targetName = String.format("test3GBTarget%s.tmp",
String.valueOf(RND.nextInt(Integer.MAX_VALUE)));
Path targetFile = CWD.resolve(targetName);
try { // calculate initial position to be just short of 2GB finallong initPos = 2047*BYTES_PER_WRITE;
// create the source file with a hint to be sparse try (FileChannel fc = FileChannel.open(sourceFile, CREATE_NEW, SPARSE, WRITE, APPEND);) { // set initial position to avoid writing nearly 2GB
fc.position(initPos);
// fill the remainder of the file with random bytes int nw = (int)(NUM_WRITES - initPos/BYTES_PER_WRITE); for (int i = 0; i < nw; i++) { byte[] rndBytes = createRandomBytes(BYTES_PER_WRITE, 0);
ByteBuffer src = ByteBuffer.wrap(rndBytes);
fc.write(src);
}
}
// create the target file with a hint to be sparse try (FileChannel fc = FileChannel.open(targetFile, CREATE_NEW, WRITE, SPARSE);) {
}
// perform actual transfer, effectively by multiple invocations // of Filechannel.transferTo(FileChannel) try (InputStream inputStream = Channels.newInputStream(FileChannel.open(sourceFile));
OutputStream outputStream = Channels.newOutputStream(FileChannel.open(targetFile, WRITE))) { long count = inputStream.transferTo(outputStream);
// compare reported transferred bytes, must be 3 GB // less the value of the initial position
assertEquals(count, BYTES_WRITTEN - initPos);
}
// compare content of both files, failing if different
assertEquals(Files.mismatch(sourceFile, targetFile), -1);
// IllegalBlockingMode must be thrown when trying to perform // a transfer
assertThrows(IllegalBlockingModeException.class, () -> is.transferTo(os));
}
// testing non-blocking selectable input to arbitrary output // (here: byte array) try (SelectableChannel sc = pipe.source().configureBlocking(false);
InputStream is = Channels.newInputStream((ReadableByteChannel) sc);
OutputStream os = new ByteArrayOutputStream()) {
// IllegalBlockingMode must be thrown when trying to perform // a transfer
assertThrows(IllegalBlockingModeException.class, () -> is.transferTo(os));
}
} finally {
pipe.source().close();
pipe.sink().close();
}
}
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.