public static long copy(InputStream in,OutputStream out,byte[] buffer) throws IOException {
  Preconditions.checkNotNull(in,"No InputStream specified");
  Preconditions.checkNotNull(out,"No OutputStream specified");
  try {
    long byteCount=0;
    int bytesRead;
    while ((bytesRead=in.read(buffer)) != -1) {
      out.write(buffer,0,bytesRead);
      byteCount+=bytesRead;
    }
    out.flush();
    return byteCount;
  }
  finally {
    try {
      in.close();
    }
 catch (    IOException ex) {
    }
    try {
      out.close();
    }
 catch (    IOException ex) {
    }
  }
}

public static long copy ( InputStream in , OutputStream out , byte [ ] buffer ) throws IOException { Preconditions . checkNotNull ( in , "No InputStream specified" ) ; Preconditions . checkNotNull ( out , "No OutputStream specified" ) ; try { long byteCount = 0 ; int bytesRead ; while ( ( bytesRead = in . read ( buffer ) ) != - 1 ) { out . write ( buffer , 0 , bytesRead ) ; byteCount += bytesRead ; } out . flush ( ) ; return byteCount ; } finally { try { in . close ( ) ; } catch ( IOException ex ) { } try { out . close ( ) ; } catch ( IOException ex ) { } } }
copy the contents of the given inputstream to the given outputstream . closes both streams when done .
