Add methods to FileUtilities to read entire file into String or Array[Byte]

This commit is contained in:
Mark Harrah 2009-08-30 21:44:44 -04:00
parent 74adf5d9f6
commit 05f4f60553
1 changed files with 25 additions and 1 deletions

View File

@ -6,7 +6,7 @@ package xsbt
import OpenResource._
import ErrorHandling.translate
import java.io.{File, FileInputStream, InputStream, OutputStream}
import java.io.{ByteArrayOutputStream, File, FileInputStream, InputStream, OutputStream}
import java.net.{URI, URISyntaxException, URL}
import java.nio.charset.Charset
import java.util.jar.{Attributes, JarEntry, JarFile, JarInputStream, JarOutputStream, Manifest}
@ -345,4 +345,28 @@ object FileUtilities
else
error("String cannot be encoded by charset " + charset.name)
}
def read(file: File): String = read(file, defaultCharset)
def read(file: File, charset: Charset): String =
{
val out = new ByteArrayOutputStream(file.length.toInt)
fileInputStream(file){ in => transfer(in, out) }
out.toString(charset.name)
}
/** doesn't close the InputStream */
def read(in: InputStream): String = read(in, defaultCharset)
/** doesn't close the InputStream */
def read(in: InputStream, charset: Charset): String =
{
val out = new ByteArrayOutputStream
transfer(in, out)
out.toString(charset.name)
}
def readBytes(file: File): Array[Byte] = fileInputStream(file)(readBytes)
/** doesn't close the InputStream */
def readBytes(in: InputStream): Array[Byte] =
{
val out = new ByteArrayOutputStream
transfer(in, out)
out.toByteArray
}
}