Reputation: 13269
To read from a stream in Java I would do the usual:
byte buff[] = new byte[10]
int len = 0;
while ((len = inputStream.read(buff)) != -1){
...do something with buff..
}
I know scala offers things like Source.fromInputStream but I see it a bit heavy to be honest. I know the above won't work in Scala because the assignment doesn't return the value. Is there a simple way without using the library?
Upvotes: 0
Views: 164
Reputation: 7353
It is possible to close over mutable state and use Iterator.continually
like so:
val buff = Array.ofDim[Byte](10)
Iterator.continually(inputStream.read(buff))
.takeWhile(_ != -1)
.foreach { len =>
// do something wit buff and len
}
being a more or less direct translation of Java code. I'd reach for the libraries based on task at hand, however.
Upvotes: 1