@FixFor("MODE-1358") @Test public void shouldCopyFilesUsingStreams() throws Exception { // Copy a large file into a temporary file ... File tempFile = File.createTempFile("copytest", "pdf"); RandomAccessFile destinationRaf = null; RandomAccessFile originalRaf = null; try { URL sourceUrl = getClass().getResource("/docs/postgresql-8.4.1-US.pdf"); assertThat(sourceUrl, is(notNullValue())); File sourceFile = new File(sourceUrl.toURI()); assertThat(sourceFile.exists(), is(true)); assertThat(sourceFile.canRead(), is(true)); assertThat(sourceFile.isFile(), is(true)); boolean useBufferedStream = true; final int bufferSize = AbstractBinaryStore.bestBufferSize(sourceFile.length()); destinationRaf = new RandomAccessFile(tempFile, "rw"); originalRaf = new RandomAccessFile(sourceFile, "r"); FileChannel destinationChannel = destinationRaf.getChannel(); OutputStream output = Channels.newOutputStream(destinationChannel); if (useBufferedStream) output = new BufferedOutputStream(output, bufferSize); // Create an input stream to the original file ... FileChannel originalChannel = originalRaf.getChannel(); InputStream input = Channels.newInputStream(originalChannel); if (useBufferedStream) input = new BufferedInputStream(input, bufferSize); // Copy the content ... Stopwatch sw = new Stopwatch(); sw.start(); IoUtil.write(input, output, bufferSize); sw.stop(); System.out.println( "Time to copy \"" + sourceFile.getName() + "\" (" + sourceFile.length() + " bytes): " + sw.getTotalDuration()); } finally { tempFile.delete(); if (destinationRaf != null) destinationRaf.close(); if (originalRaf != null) originalRaf.close(); } }