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
|
|
|
|
|
{
|
2009-10-18 04:40:02 +02:00
|
|
|
def apply(files: List[File], toDirectory: File): Unit = files.foreach(file => apply(file, toDirectory))
|
2009-09-27 20:39:26 +02:00
|
|
|
def apply(file: File, toDirectory: File)
|
2009-09-26 08:18:04 +02:00
|
|
|
{
|
2009-09-27 20:39:26 +02:00
|
|
|
toDirectory.mkdirs()
|
2009-09-26 08:18:04 +02:00
|
|
|
Using(new FileInputStream(file)) { in =>
|
2009-09-27 20:39:26 +02:00
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|