diff options
author | Paul Phillips <paulp@improving.org> | 2009-08-17 21:35:41 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2009-08-17 21:35:41 +0000 |
commit | 0eee4ea68975abd79c907a934053c6a9dcb05e1b (patch) | |
tree | 31c815bdc44fd4f37d87e8b9576525cb3f70d6ac /src | |
parent | 8df11b38aaf1ee3a481aebba97f0ddcba535c241 (diff) | |
download | scala-0eee4ea68975abd79c907a934053c6a9dcb05e1b.tar.gz scala-0eee4ea68975abd79c907a934053c6a9dcb05e1b.tar.bz2 scala-0eee4ea68975abd79c907a934053c6a9dcb05e1b.zip |
Added bytes() method to File.
patches from today you can write something like this:
io.File("/etc/passwd").bytes grouped 512 map (_.sum) mkString ", "
res1: String = 45481, 46001, 45110, 45559, 45967, 29543
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/io/File.scala | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/src/library/scala/io/File.scala b/src/library/scala/io/File.scala index bf64ec35c6..6c15152921 100644 --- a/src/library/scala/io/File.scala +++ b/src/library/scala/io/File.scala @@ -10,7 +10,9 @@ package scala.io -import java.io.{ FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, File => JFile } +import java.io.{ + FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, + BufferedInputStream, File => JFile } import collection.Traversable object File @@ -42,6 +44,15 @@ class File(val file: JFile)(implicit val codec: Codec = Codec.default) extends c */ def lines(): Iterator[String] = toSource().getLines() + /** Convenience function for iterating over the bytes in a file. + * Note they are delivered as Ints as they come from the read() call, + * you can map (_.toByte) if you would really prefer Bytes. + */ + def bytes(): Iterator[Int] = { + val in = new BufferedInputStream(inputStream()) + Iterator continually in.read() takeWhile (_ != -1) + } + /** Convenience function to import entire file into a String. */ def slurp() = toSource().mkString |