sbt/util/io/Using.scala

95 lines
4.2 KiB
Scala
Raw Normal View History

2009-08-16 20:29:08 +02:00
/* sbt -- Simple Build Tool
* Copyright 2008, 2009 Mark Harrah
*/
2010-06-09 06:56:07 +02:00
package sbt
2009-08-16 20:29:08 +02:00
import java.io.{Closeable, File, FileInputStream, FileOutputStream, InputStream, OutputStream}
import java.io.{ByteArrayOutputStream, InputStreamReader, OutputStreamWriter}
import java.io.{BufferedReader, BufferedWriter, FileReader, FileWriter, Reader, Writer}
import java.util.zip.{GZIPInputStream, GZIPOutputStream}
import java.net.{URL, URISyntaxException}
import java.nio.charset.{Charset, CharsetDecoder, CharsetEncoder}
import java.nio.channels.FileChannel
import java.util.jar.{Attributes, JarEntry, JarFile, JarInputStream, JarOutputStream, Manifest}
import java.util.zip.{GZIPOutputStream, ZipEntry, ZipFile, ZipInputStream, ZipOutputStream}
2010-06-09 06:56:07 +02:00
import xsbt.ErrorHandling.translate
import Using._
2009-08-16 20:29:08 +02:00
2010-06-09 06:56:07 +02:00
abstract class Using[Source, T] extends NotNull
2009-08-16 20:29:08 +02:00
{
protected def open(src: Source): T
def apply[R](src: Source)(f: T => R): R =
{
val resource = open(src)
try { f(resource) }
finally { close(resource) }
}
protected def close(out: T): Unit
}
import scala.reflect.{Manifest => SManifest}
2010-06-09 06:56:07 +02:00
abstract class WrapUsing[Source, T](implicit srcMf: SManifest[Source], targetMf: SManifest[T]) extends Using[Source, T]
2009-08-16 20:29:08 +02:00
{
protected def label[S](m: SManifest[S]) = m.erasure.getSimpleName
protected def openImpl(source: Source): T
protected final def open(source: Source): T =
translate("Error wrapping " + label(srcMf) + " in " + label(targetMf) + ": ") { openImpl(source) }
}
2010-06-09 06:56:07 +02:00
trait OpenFile[T] extends Using[File, T]
2009-08-16 20:29:08 +02:00
{
protected def openImpl(file: File): T
protected final def open(file: File): T =
{
val parent = file.getParentFile
if(parent != null)
2010-06-09 06:56:07 +02:00
IO.createDirectory(parent)
2009-08-31 16:41:43 +02:00
openImpl(file)
2009-08-16 20:29:08 +02:00
}
}
2010-06-09 06:56:07 +02:00
object Using
2009-08-16 20:29:08 +02:00
{
2010-06-09 06:56:07 +02:00
def wrap[Source, T<: Closeable](openF: Source => T)(implicit srcMf: SManifest[Source], targetMf: SManifest[T]): Using[Source,T] =
wrap(openF, closeCloseable)
def wrap[Source, T](openF: Source => T, closeF: T => Unit)(implicit srcMf: SManifest[Source], targetMf: SManifest[T]): Using[Source,T] =
new WrapUsing[Source, T]
2009-08-16 20:29:08 +02:00
{
def openImpl(source: Source) = openF(source)
def close(t: T) = closeF(t)
}
2010-06-09 06:56:07 +02:00
def resource[Source, T <: Closeable](openF: Source => T): Using[Source,T] =
resource(openF, closeCloseable)
def resource[Source, T](openF: Source => T, closeF: T => Unit): Using[Source,T] =
new Using[Source,T]
2009-08-16 20:29:08 +02:00
{
def open(s: Source) = openF(s)
def close(s: T) = closeF(s)
}
2010-06-09 06:56:07 +02:00
def file[T <: Closeable](openF: File => T): OpenFile[T] = file(openF, closeCloseable)
2009-08-16 20:29:08 +02:00
def file[T](openF: File => T, closeF: T => Unit): OpenFile[T] =
new OpenFile[T]
{
def openImpl(file: File) = openF(file)
def close(t: T) = closeF(t)
}
2010-06-09 06:56:07 +02:00
private def closeCloseable[T <: Closeable]: T => Unit = _.close()
2009-08-16 20:29:08 +02:00
2010-06-09 06:56:07 +02:00
def fileOutputStream(append: Boolean = false) = file(f => new FileOutputStream(f, append))
2009-08-16 20:29:08 +02:00
def fileInputStream = file(f => new FileInputStream(f))
def urlInputStream = resource( (u: URL) => translate("Error opening " + u + ": ")(u.openStream))
def fileOutputChannel = file(f => new FileOutputStream(f).getChannel)
def fileInputChannel = file(f => new FileInputStream(f).getChannel)
2010-06-09 06:56:07 +02:00
def fileWriter(charset: Charset = IO.utf8, append: Boolean = false) =
2009-08-16 20:29:08 +02:00
file(f => new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f, append), charset)) )
def fileReader(charset: Charset) = file(f => new BufferedReader(new InputStreamReader(new FileInputStream(f), charset)) )
def jarFile(verify: Boolean) = file(f => new JarFile(f, verify), (_: JarFile).close())
def zipFile = file(f => new ZipFile(f), (_: ZipFile).close())
def streamReader = wrap{ (_: (InputStream, Charset)) match { case (in, charset) => new InputStreamReader(in, charset) } }
def gzipInputStream = wrap( (in: InputStream) => new GZIPInputStream(in) )
def zipInputStream = wrap( (in: InputStream) => new ZipInputStream(in))
def gzipOutputStream = wrap((out: OutputStream) => new GZIPOutputStream(out), (_: GZIPOutputStream).finish())
def jarOutputStream = wrap( (out: OutputStream) => new JarOutputStream(out))
def jarInputStream = wrap( (in: InputStream) => new JarInputStream(in))
def zipEntry(zip: ZipFile) = resource( (entry: ZipEntry) =>
translate("Error opening " + entry.getName + " in " + zip + ": ") { zip.getInputStream(entry) } )
}