@@ -64,6 +64,10 @@ public class TransferTo {
64
64
65
65
private static final int ITERATIONS = 10 ;
66
66
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
+
67
71
private static final Random RND = RandomFactory .getRandom ();
68
72
69
73
/*
@@ -126,6 +130,37 @@ public void testStreamContents(InputStreamProvider inputStreamProvider,
126
130
checkTransferredContents (inputStreamProvider , outputStreamProvider , createRandomBytes (4096 , 0 ), 0 , 4096 );
127
131
}
128
132
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
+
129
164
/*
130
165
* Asserts that the transferred content is correct, i. e. compares the actually transferred bytes
131
166
* to the expected assumption. The position of the input and output stream before the transfer is
0 commit comments