code: 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();
      }
    }
docs: 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.
  
  @param is      The `InputStream` to copy bytes from. It is closed regardless of copy results.
  @param file    The `File` to copy bytes to
  @param timeout The timeout (in ms) of the copy.
  
  @return The size of bytes written to `file`
  
  @throws IOException
  @throws TimeoutException If `timeout` is exceeded