cdm/src/main/java/ucar/nc2/util/IO.java
static public long writeToFile(InputStream in, String fileOutName) throws IOException {
    try (FileOutputStream fout = new FileOutputStream( fileOutName)) {
      OutputStream out = new BufferedOutputStream(fout);
      return IO.copy(in, out);
    } finally {
      if (null != in) in.close();
    }
  }
static public long writeToFile(InputStream in, String fileOutName) throws IOException {
    try (FileOutputStream fout = new FileOutputStream( fileOutName)) {
      OutputStream out = new BufferedOutputStream(fout);
      return IO.copy(in, out);
    } finally {
      if (null != in) in.close();
    }
  }
static public long writeToFile ( InputStream in , String fileOutName ) throws IOException { try ( FileOutputStream fout = new FileOutputStream ( fileOutName ) ) { OutputStream out = new BufferedOutputStream ( fout ) ; return IO . copy ( in , out ) ; } finally { if ( null != in ) in . close ( ) ; } }
copy input stream to file. close input stream when done.

@param in          copy from here
@param fileOutName open this file (overwrite) and copy to it.
@return number of bytes copied
@throws java.io.IOException on io error
copy input stream to file . close input stream when done .