Almighty Bus Error

Blog about computer science, code snippets and tips.

GitHub repository with examples here.
Currently a Computer Engineering student at FCT/UNL.
May 19, 2009 at 11:16am
Tags: Snippet  Java 

Comments (View)

Snippet.Java: Dump InputStream → OutputStream

This piece of code keeps reading 1024 bytes from the InputStream and writing to the OutStream until the read function returns an error value (-1) which means there is not anything else to read.

protected void dumpStream( InputStream in, OutputStream out )
throws IOException {
    byte[] arr = new byte[1024];
    int n;
    do {
        n = in.read( arr );
        out.write( arr, 0, n );
    } while (n != -1);
}

Notes