public static long copyToFileAndClose ( InputStream is , File file , long timeout ) throws IOException , TimeoutException { file . getParentFile ( ) . mkdirs ( ) ; try ( OutputStream os = new BufferedOutputStream ( new FileOutputStream ( file ) ) ) { final long retval = copyWithTimeout ( is , os , timeout ) ; // Workarround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf os . flush ( ) ; return retval ; } finally { is . close ( ) ; } }
Copy bytes from is to file but timeout if the copy takes too long . The timeout is best effort and not guaranteed . Specifically is . read will not be interrupted .