public static int copy(InputStream in, OutputStream out) throws IOException {
        assert in != null : "No input stream specified";
        assert out != null : "No output stream specified";
        try {
            int byteCount = 0;
            byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = -1;
            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 int copy ( InputStream in , OutputStream out ) throws IOException { assert in != null : "No input stream specified" ; assert out != null : "No output stream specified" ; try { int byteCount = 0 ; byte [ ] buffer = new byte [ BUFFER_SIZE ] ; int bytesRead = - 1 ; 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.

@param in  the stream to copy from
@param out the stream to copy to
@return the number of bytes copied
@throws IOException in case of I/O errors