NativeIO { public static void copyFileUnbuffered(File src, File dst) throws IOException { if (nativeLoaded && Shell.WINDOWS) { copyFileUnbuffered0(src.getAbsolutePath(), dst.getAbsolutePath()); } else { FileInputStream fis = null; FileOutputStream fos = null; FileChannel input = null; FileChannel output = null; try { fis = new FileInputStream(src); fos = new FileOutputStream(dst); input = fis.getChannel(); output = fos.getChannel(); long remaining = input.size(); long position = 0; long transferred = 0; while (remaining > 0) { transferred = input.transferTo(position, remaining, output); remaining -= transferred; position += transferred; } } finally { IOUtils.cleanup(LOG, output); IOUtils.cleanup(LOG, fos); IOUtils.cleanup(LOG, input); IOUtils.cleanup(LOG, fis); } } }  static boolean isAvailable(); static String getOwner(FileDescriptor fd); static FileInputStream getShareDeleteFileInputStream(File f); static FileInputStream getShareDeleteFileInputStream(File f, long seekOffset); static FileOutputStream getCreateForWriteFileOutputStream(File f, int permissions); static void renameTo(File src, File dst); @Deprecated static void link(File src, File dst); static void copyFileUnbuffered(File src, File dst);  }
@Test (timeout = 30000) public void testCopyFileUnbuffered() throws Exception { final String METHOD_NAME = GenericTestUtils.getMethodName(); File srcFile = new File(TEST_DIR, METHOD_NAME + ".src.dat"); File dstFile = new File(TEST_DIR, METHOD_NAME + ".dst.dat"); final int fileSize = 0x8000000; final int SEED = 0xBEEF; final int batchSize = 4096; final int numBatches = fileSize / batchSize; Random rb = new Random(SEED); FileChannel channel = null; RandomAccessFile raSrcFile = null; try { raSrcFile = new RandomAccessFile(srcFile, "rw"); channel = raSrcFile.getChannel(); byte bytesToWrite[] = new byte[batchSize]; MappedByteBuffer mapBuf; mapBuf = channel.map(MapMode.READ_WRITE, 0, fileSize); for (int i = 0; i < numBatches; i++) { rb.nextBytes(bytesToWrite); mapBuf.put(bytesToWrite); } NativeIO.copyFileUnbuffered(srcFile, dstFile); Assert.assertEquals(srcFile.length(), dstFile.length()); } finally { IOUtils.cleanup(LOG, channel); IOUtils.cleanup(LOG, raSrcFile); FileUtils.deleteQuietly(TEST_DIR); } }