2010-02-08 05:45:19 +01:00
|
|
|
/* sbt -- Simple Build Tool
|
|
|
|
|
* Copyright 2009 Mark Harrah
|
|
|
|
|
*/
|
2009-09-26 08:18:04 +02:00
|
|
|
package xsbt.boot
|
|
|
|
|
|
|
|
|
|
import java.io.{Closeable, File, FileInputStream, FileOutputStream, InputStream, OutputStream}
|
|
|
|
|
|
2011-04-11 06:44:28 +02:00
|
|
|
object Using
|
2009-09-26 08:18:04 +02:00
|
|
|
{
|
2010-02-06 01:06:44 +01:00
|
|
|
def apply[R <: Closeable,T](create: R)(f: R => T): T = withResource(create)(f)
|
2009-09-26 08:18:04 +02:00
|
|
|
def withResource[R <: Closeable,T](r: R)(f: R => T): T = try { f(r) } finally { r.close() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object Copy
|
|
|
|
|
{
|
2011-07-02 05:38:03 +02:00
|
|
|
def apply(files: List[File], toDirectory: File): Boolean = files.map(file => apply(file, toDirectory)).contains(true)
|
|
|
|
|
def apply(file: File, toDirectory: File): Boolean =
|
2009-09-26 08:18:04 +02:00
|
|
|
{
|
2009-09-27 20:39:26 +02:00
|
|
|
toDirectory.mkdirs()
|
2011-07-02 05:38:03 +02:00
|
|
|
val to = new File(toDirectory, file.getName)
|
|
|
|
|
val missing = !to.exists
|
|
|
|
|
if(missing)
|
|
|
|
|
{
|
|
|
|
|
Using(new FileInputStream(file)) { in =>
|
|
|
|
|
Using(new FileOutputStream(to)) { out =>
|
|
|
|
|
transfer(in, out)
|
|
|
|
|
}
|
2009-09-26 08:18:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
2011-07-02 05:38:03 +02:00
|
|
|
missing
|
2009-09-26 08:18:04 +02:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|