sbt/launch/Using.scala

37 lines
888 B
Scala
Raw Normal View History

2009-09-26 08:18:04 +02:00
package xsbt.boot
import java.io.{Closeable, File, FileInputStream, FileOutputStream, InputStream, OutputStream}
object Using extends NotNull
{
def apply[R <: Closeable,T](create: => R)(f: R => T): T = withResource(create)(f)
def withResource[R <: Closeable,T](r: R)(f: R => T): T = try { f(r) } finally { r.close() }
}
object Copy
{
def apply(files: Seq[File], toDirectory: File): Unit = files.foreach(file => apply(file, toDirectory))
def apply(file: File, toDirectory: File)
2009-09-26 08:18:04 +02:00
{
toDirectory.mkdirs()
2009-09-26 08:18:04 +02:00
Using(new FileInputStream(file)) { in =>
Using(new FileOutputStream(new File(toDirectory, file.getName))) { out =>
2009-09-26 08:18:04 +02:00
transfer(in, out)
}
}
}
def transfer(in: InputStream, out: OutputStream)
{
val buffer = new Array[Byte](8192)
def next()
{
val read = in.read(buffer)
if(read > 0)
{
out.write(buffer, 0, read)
next()
}
}
next()
}
}