Skip to content

Instantly share code, notes, and snippets.

@mumoshu
Created December 9, 2011 00:58
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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 }
@ChetanBhasin
Copy link

I was trying to run this code of yours but I get an error. Compiler doesn't recognises 'in'. Could you please explain what 'in' is in your code?

@hugolu
Copy link

hugolu commented Mar 14, 2016

the returned value of fileContentStream() should be
if (length == 0) Stream.empty else Chunk(length, bytes) #:: fileContentStream(fileIn)

@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