Skip to content

Instantly share code, notes, and snippets.

@jimbojw
Created April 9, 2010 13:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimbojw/361148 to your computer and use it in GitHub Desktop.
Save jimbojw/361148 to your computer and use it in GitHub Desktop.
var
// path to something you want to download
path = "http://something/you/want/to/download",
// file you want to save it as
file = new java.io.File( "path/to/somewhere/local" ),
// create URL object and establish connection
url = new java.net.URL(path),
conn = url.openConnection(),
// input and output streams
bis = new java.io.BufferedInputStream(conn.inputStream),
out = new java.io.BufferedOutputStream(
new java.io.FileOutputStream(file)
),
// buffer to hold next read chunk and number of bytes read in chunk
buf = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 4096),
n = 0;
// read bytes until end of input, writing to buffer
while ((n = bis.read(buf, 0, buf.length)) != -1) {
out.write(buf, 0, n);
}
// flush output and close streams
out.flush();
bis.close();
out.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment