question: private static void doCopyFile ( final File srcFile , final File destFile , final boolean preserveFileDate ) throws IOException { if ( destFile . exists ( ) && destFile . isDirectory ( ) ) { throw new IOException ( "Destination '" + destFile + "' exists but is a directory" ) ; } FileInputStream fis = null ; FileOutputStream fos = null ; FileChannel input = null ; FileChannel output = null ; try { fis = new FileInputStream ( srcFile ) ; fos = new FileOutputStream ( destFile ) ; input = fis . getChannel ( ) ; output = fos . getChannel ( ) ; final long size = input . size ( ) ; long pos = 0 ; long count = 0 ; while ( pos < size ) { final long remain = size - pos ; count = remain > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : remain ; final long bytesCopied = output . transferFrom ( input , pos , count ) ; if ( bytesCopied == 0 ) { break ; } pos += bytesCopied ; } } finally { ParseIOUtils . closeQuietly ( output ) ; ParseIOUtils . closeQuietly ( fos ) ; ParseIOUtils . closeQuietly ( input ) ; ParseIOUtils . closeQuietly ( fis ) ; } final long srcLen = srcFile . length ( ) ; final long dstLen = destFile . length ( ) ; if ( srcLen != dstLen ) { throw new IOException ( "Failed to copy full contents from '" + srcFile + "' to '" + destFile + "' Expected length: " + srcLen + " Actual: " + dstLen ) ; } if ( preserveFileDate ) { destFile . setLastModified ( srcFile . lastModified ( ) ) ; } }
target:  Internal copy file method. This caches the original file length, and throws an IOException if the output file length is different from the current input file length. So it may fail if the file changes size. It may also fail with "IllegalArgumentException: Negative size" if the input file is truncated part way through copying the data and the new file size is less than the current position.