Tuesday, 15 June 2010

Finally closing stream using Scala exception catching -


Anyone know the solution to this problem? I try to write again that eventually things are prepared in a functional manner, but I can not stop the stream now: -)

  import scala.util.control .exception._ def gunzip () = {capture ("gunziping file $ {f.getAbsolutePath}") (classOf [IOException], classOf [FileNotFoundException]). And finally (println ("how can I stop the stream?")) Either ({Val = new GZIPInputStream (New FileInputStream (f)) is stream.continually (is.read ()). TakeWhile (-1! =). Map (_ to tote) .toArray}} match {case left (e) = & gt; Error message {s} "$ {f.getAbsolutePath}!" Host $ {sentup.smtpHost} "logger.error (message, e) MailClient.send (message, message) new array [byte] (0) Case correct (V) = & gt; V}}   

I rewritten this on the basis of Senia's solution like this:

  def gunzip () = {logger.info ("Gunziping File $ {file.getAbsolutePath} ") Def closeAfterReading (c: InputStream) (f: InputStream => array [byte]) = {capture (classOf [IOException], classOf [FileNotFoundException]) .andFinally (c.close )) .either (f) (c) match {case left (e) = & gt; {Val message = s "$ {file.getAbsolutePath}" error in reading the IO file! {$ Server.error (message, e) new array [byte] (0)} case {v} = " & Gt; V}} closeAfterReading (new GZIPInputStream (new FileInputStream (file))) {has => Map (_ ToByte) .toArray}}  

No comments:

Post a Comment