This page is no longer maintained — Please continue to the home page at www.scala-lang.org

(Re: Stream from InputStream (RESOLVED)

No replies
John Ky
Joined: 2009-10-06,
User offline. Last seen 42 years 45 weeks ago.
Thanks Eastsun, that is a lot better.

On Sun, Dec 20, 2009 at 12:14 AM, Eastsun <flushtime [at] 126 [dot] com> wrote:

How about this(scala 2.8 only):
Welcome to Scala version 2.8.0.r20117-b20091213020323 (Java HotSpot(TM)
Client VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import java.io._
import java.io._

scala> val data = Array[Byte](31, 0x80.toByte, 0, 3, 100, 101)
data: Array[Byte] = Array(31, -128, 0, 3, 100, 101)

scala> val is = new ByteArrayInputStream(data)
is: java.io.ByteArrayInputStream = java.io.ByteArrayInputStream@5ba50e

scala> val stream = Stream.continually(is.read).takeWhile(_ != -1
).map(_.toByte)
stream: scala.collection.immutable.Stream[Byte] = Stream(31, ?)

scala> stream.print
31, -128, 0, 3, 100, 101, empty
scala>




John Ky wrote:
>
> Hi Scala list,
>
> What is the idiomatic way to create a Stream from an InputStream?
>
> I managed the following:
>
>     val data = Array[Byte](31, 0x80.toByte, 0, 3, 100, 101)
>     val is = new ByteArrayInputStream(data)
>     def streamReader(): Stream[Byte] = {
>       is.read() match {
>         case -1 => Stream.empty
>         case value => Stream.cons(value.toByte, streamReader())
>       }
>     }
>     lazy val stream: Stream[Byte] = streamReader()
>     stream.print
>
> Cheers,
>
> -John
>
>


-----
My scala solutions for  http://projecteuler.net/ Project Euler  problems:
http://eastsun.javaeye.com/category/34059 Click here
--
View this message in context: http://old.nabble.com/Stream-from-InputStream-tp26854544p26854920.html
Sent from the Scala - User mailing list archive at Nabble.com.


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland