Skip to content

Commit 0af5f02

Browse files
author
duke
committedNov 2, 2021
Automatic merge of jdk:master into master
2 parents ee3b098 + 6a04899 commit 0af5f02

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
 

‎test/jdk/java/nio/channels/Channels/TransferTo.java

+35
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public class TransferTo {
6464

6565
private static final int ITERATIONS = 10;
6666

67+
private static final int NUM_WRITES = 3 * 1024;
68+
private static final int BYTES_PER_WRITE = 1024 * 1024;
69+
private static final long BYTES_WRITTEN = (long) NUM_WRITES * BYTES_PER_WRITE;
70+
6771
private static final Random RND = RandomFactory.getRandom();
6872

6973
/*
@@ -126,6 +130,37 @@ public void testStreamContents(InputStreamProvider inputStreamProvider,
126130
checkTransferredContents(inputStreamProvider, outputStreamProvider, createRandomBytes(4096, 0), 0, 4096);
127131
}
128132

133+
/*
134+
* Special test for file-to-file transfer of more than two GB.
135+
* This test covers multiple iterations of FileChannel.transerTo(FileChannel),
136+
* which ChannelInputStream.transferTo() only applies in this particular case,
137+
* and cannot get tested using a single byte[] due to size limitation of arrays.
138+
*/
139+
@Test
140+
public void testMoreThanTwoGB() throws IOException {
141+
// preparing two temporary files which will be compared at the end of the test
142+
Path sourceFile = Files.createTempFile(null, null);
143+
Path targetFile = Files.createTempFile(null, null);
144+
145+
// writing 3 GB of random bytes into source file
146+
for (int i = 0; i < NUM_WRITES; i++)
147+
Files.write(sourceFile, createRandomBytes(BYTES_PER_WRITE, 0), StandardOpenOption.APPEND);
148+
149+
// performing actual transfer, effectively by multiple invocations of Filechannel.transferTo(FileChannel)
150+
long count;
151+
try (InputStream inputStream = Channels.newInputStream(FileChannel.open(sourceFile));
152+
OutputStream outputStream = Channels
153+
.newOutputStream(FileChannel.open(targetFile, StandardOpenOption.WRITE))) {
154+
count = inputStream.transferTo(outputStream);
155+
}
156+
157+
// comparing reported transferred bytes, must be 3 GB
158+
assertEquals(count, BYTES_WRITTEN);
159+
160+
// comparing content of both files, failing in case of any difference
161+
assertEquals(Files.mismatch(sourceFile, targetFile), -1);
162+
}
163+
129164
/*
130165
* Asserts that the transferred content is correct, i. e. compares the actually transferred bytes
131166
* to the expected assumption. The position of the input and output stream before the transfer is

0 commit comments

Comments
 (0)
Please sign in to comment.