I seem to need your help again :/
given the following code:
/**
* downloads a url (file) to a desired file name
*/
def downloadFile(url: URL, filename: String) {
commonOp(url2InputStream(url), filename)
}
/**
* common method for writing data from an inputstream to an outputstream
*/
def commonOp(is: InputStream, filename: String) {
val out: OutputStream = file2OutputStream(filename)
try {
deleteFileIfExists(filename)
copy(is, out)
} catch {
case e: Exception => println(e.printStackTrace())
}
out.close()
is.close()
}
/**
* copies an inputstream to an outputstream
*/
def copy(in: InputStream, out: OutputStream) {
val buffer: Array[Byte] = new Array[Byte](1024)
var sum: Int = 0
Iterator.continually(in.read(buffer)).takeWhile(_ != -1).foreach({ n => out.write(buffer, 0, n); (sum += buffer.length); println(sum + " written to output "); })
}
/**
* handling of bzip archive files
*/
def unzipFile(fn: String, outputFileName: String) {
val in = new BZip2CompressorInputStream(new FileInputStream(fn))
commonOp(in, outputFileName)
}
which is in essence downloading a remote file from a url and unzipping it. is there a decent way to write this code in a more elegant non side effecty fashion?
thank you.