8276994: java/nio/channels/Channels/TransferTo.java leaves multi-GB files in /tmp

Reviewed-by: alanb
This commit is contained in:
Lance Andersen 2021-11-11 19:09:17 +00:00
parent 8aae88b0fc
commit b0d7a9daa6

View File

@ -138,27 +138,35 @@ public class TransferTo {
*/
@Test
public void testMoreThanTwoGB() throws IOException {
// preparing two temporary files which will be compared at the end of the test
Path sourceFile = Files.createTempFile(null, null);
Path targetFile = Files.createTempFile(null, null);
Path sourceFile = Files.createTempFile("test2GBSource", null);
try {
// preparing two temporary files which will be compared at the end of the test
Path targetFile = Files.createTempFile("test2GBtarget", null);
try {
// writing 3 GB of random bytes into source file
for (int i = 0; i < NUM_WRITES; i++)
Files.write(sourceFile, createRandomBytes(BYTES_PER_WRITE, 0), StandardOpenOption.APPEND);
// writing 3 GB of random bytes into source file
for (int i = 0; i < NUM_WRITES; i++)
Files.write(sourceFile, createRandomBytes(BYTES_PER_WRITE, 0), StandardOpenOption.APPEND);
// performing actual transfer, effectively by multiple invocations of Filechannel.transferTo(FileChannel)
long count;
try (InputStream inputStream = Channels.newInputStream(FileChannel.open(sourceFile));
OutputStream outputStream = Channels
.newOutputStream(FileChannel.open(targetFile, StandardOpenOption.WRITE))) {
count = inputStream.transferTo(outputStream);
}
// performing actual transfer, effectively by multiple invocations of Filechannel.transferTo(FileChannel)
long count;
try (InputStream inputStream = Channels.newInputStream(FileChannel.open(sourceFile));
OutputStream outputStream = Channels
.newOutputStream(FileChannel.open(targetFile, StandardOpenOption.WRITE))) {
count = inputStream.transferTo(outputStream);
// comparing reported transferred bytes, must be 3 GB
assertEquals(count, BYTES_WRITTEN);
// comparing content of both files, failing in case of any difference
assertEquals(Files.mismatch(sourceFile, targetFile), -1);
} finally {
Files.delete(targetFile);
}
} finally {
Files.delete(sourceFile);
}
// comparing reported transferred bytes, must be 3 GB
assertEquals(count, BYTES_WRITTEN);
// comparing content of both files, failing in case of any difference
assertEquals(Files.mismatch(sourceFile, targetFile), -1);
}
/*