2010-02-07 23:45:19 -05:00
|
|
|
/* sbt -- Simple Build Tool
|
|
|
|
|
* Copyright 2009 Mark Harrah
|
|
|
|
|
*/
|
2009-09-26 02:18:04 -04:00
|
|
|
package xsbt.boot
|
|
|
|
|
|
|
|
|
|
import java.io.{Closeable, File, FileInputStream, FileOutputStream, InputStream, OutputStream}
|
|
|
|
|
|
|
|
|
|
object Using extends NotNull
|
|
|
|
|
{
|
2010-02-05 19:06:44 -05:00
|
|
|
def apply[R <: Closeable,T](create: R)(f: R => T): T = withResource(create)(f)
|
2009-09-26 02:18:04 -04:00
|
|
|
def withResource[R <: Closeable,T](r: R)(f: R => T): T = try { f(r) } finally { r.close() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object Copy
|
|
|
|
|
{
|
2009-10-17 22:40:02 -04:00
|
|
|
def apply(files: List[File], toDirectory: File): Unit = files.foreach(file => apply(file, toDirectory))
|
2009-09-27 14:39:26 -04:00
|
|
|
def apply(file: File, toDirectory: File)
|
2009-09-26 02:18:04 -04:00
|
|
|
{
|
2009-09-27 14:39:26 -04:00
|
|
|
toDirectory.mkdirs()
|
2009-09-26 02:18:04 -04:00
|
|
|
Using(new FileInputStream(file)) { in =>
|
2009-09-27 14:39:26 -04:00
|
|
|
Using(new FileOutputStream(new File(toDirectory, file.getName))) { out =>
|
2009-09-26 02:18:04 -04: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()
|
|
|
|
|
}
|
|
|
|
|
}
|