Skip to content

Instantly share code, notes, and snippets.

@mumoshu
Created December 9, 2011 00:58
Show Gist options
  • Save mumoshu/1449580 to your computer and use it in GitHub Desktop.
Save mumoshu/1449580 to your computer and use it in GitHub Desktop.
Read a file using Stream (Scala)
import java.io.File
import java.io.FileInputStream
case class Chunk(length: Int, bytes: Array[Byte])
def fileContentStream(fileIn: FileInputStream): Stream[Chunk] = {
val bytes = Array.fill[Byte](1024)(0)
val length = fileIn.read(bytes)
Chunk(length, bytes) #:: fileContentStream(fileIn)
}
def usingFileInput[T](filename: String)(f: (Stream[Chunk]) => T): T = {
val fileInputStream = new FileInputStream(new File(filename))
val stream = fileContentStream(fileInputStream) takeWhile { chunk => chunk.length > 0 }
val result = f(stream)
fileInputStream.close()
result
}
val file = new File("/Users/mumoshu/Pictures/512.png")
val fileIn = new FileInputStream(file)
in(fileIn) filter { chunk => chunk._2 > 0 } foreach { chunk => chunk._2 }
@enragedginger
Copy link

This is a nice implementation, but given that Stream employs memoization behind the scenes, wouldn't you potentially run into memory issues with this approach and therefore want to use Iterator instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment