Note that there are some explanatory texts on larger screens.

plurals
  1. POScala - factoring out constructors
    text
    copied!<p>i seem to have a relatively simple problem: I do have some code that downloads a file from http and performs an unzip operation. these 2 codes seem pretty similar:</p> <pre><code> def downloadFile(url: URL, filename: String) { var out: OutputStream = null var in: InputStream = null try { val outPutFile = new File(filename) if (outPutFile.exists()) { outPutFile.delete() } val uc = url.openConnection() val connection = uc.asInstanceOf[HttpURLConnection] connection.setRequestMethod("GET") in = connection.getInputStream() out = new BufferedOutputStream(new FileOutputStream(filename)) copy(in, out) } catch { case e: Exception =&gt; println(e.printStackTrace()) } out.close() in.close() } def unzipFile(file: File): String = { var out: OutputStream = null val outputFileName = "uncompressed" + file.getName() println("trying to acess " + file.getName() + file.getAbsoluteFile()) val in = new BZip2CompressorInputStream(new FileInputStream(file)) val outfile = new File(outputFileName) if (outfile.exists()) { outfile.delete() } out = new FileOutputStream(outputFileName) copy(in, out) in.close() out.close() return outputFileName } 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 =&gt; out.write(buffer, 0, n); (sum += buffer.length); println(sum + " written to output "); }) } </code></pre> <p>is there a way to rewrite the download / unzip method into one method and to factor out the constrcutors to achieve a dependency - injection like behavior?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload