Iterator to inputstream

2,329 views
Skip to first unread message

Ayoub Benali

unread,
Mar 13, 2015, 5:58:42 AM3/13/15
to scala...@googlegroups.com
Hello,

I want to convert an Iterator[String] to a java.io.InputStream but I didn't find an east way to do it.
The main challenge is I cannot store all the data in memory to say, creating the inputstream from the array of byte or something like that, it needs to be lazy.

Currently I am saving the iterator as temporary file and then opening an inputstream on the file.

Does anybody know a better way to do that ?
If the scala API can't do that kind of transformation, is there a library that would allow to do that ?

Thanks,
Ayoub.

Dennis Haupt

unread,
Mar 13, 2015, 6:15:27 AM3/13/15
to Ayoub Benali, scala...@googlegroups.com
all you need to do is to delegate the read-call on inputstream to your iterator and override "marksupported" with false
anything i overlooked?
 
Gesendet: Freitag, 13. März 2015 um 10:58 Uhr
Von: "Ayoub Benali" <benali.a...@gmail.com>
An: scala...@googlegroups.com
Betreff: [scala-user] Iterator to inputstream
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ayoub Benali

unread,
Mar 13, 2015, 6:44:02 AM3/13/15
to Dennis Haupt, scala...@googlegroups.com
I am using a library that is expecting an InputStream as parameter so I cannot hack it a way.
Otherwise I guess I didn't understand what you mean.

Are you talking about implementing the InputStream Interface and using the iterator under the hood ?

Dennis Haupt

unread,
Mar 13, 2015, 8:04:01 AM3/13/15
to Ayoub Benali, scala...@googlegroups.com
yes, you can just create a simple wrapper yourself. all you need is to impement 2 methods
 
Gesendet: Freitag, 13. März 2015 um 11:43 Uhr
Von: "Ayoub Benali" <benali.a...@gmail.com>
An: "Dennis Haupt" <h-s...@gmx.de>
Cc: scala...@googlegroups.com
Betreff: Re: [scala-user] Iterator to inputstream

Oliver Ruebenacker

unread,
Mar 13, 2015, 8:17:15 AM3/13/15
to Dennis Haupt, Ayoub Benali, scala-user

     Hello,

  In your InputStream extension, you read a String from the Iterator and then convert it to a Array[Byte] using either the default encoding/charset or one of your choice (e.g. UTF-8). Then you need to remember how many Bytes you already read from the current Array[Byte] and get the next one if the previous one is exhausted. Make sure the InputStream does not block when the Iterator is exhausted.

  You can create a valid InputStream by only implementing the single-byte read method, but if you also implement the multi-byte read method, performance will be much better.

     Best, Oliver
--
Oliver Ruebenacker
Solutions Architect at Altisource Labs
Be always grateful, but never satisfied.

Ayoub Benali

unread,
Mar 18, 2015, 12:24:35 PM3/18/15
to Oliver Ruebenacker, Dennis Haupt, scala-user
thank you for your help, it worked :)

Dennis Haupt

unread,
Mar 18, 2015, 12:48:45 PM3/18/15
to Ayoub Benali, Oliver Ruebenacker, Dennis Haupt, scala-user
of course it does, we're experts :D
we could even draw invisible red lines in the form of a kitten 

Matt Fulgo

unread,
May 18, 2016, 2:17:07 PM5/18/16
to scala-user, benali.a...@gmail.com, cur...@gmail.com, h-s...@gmx.de
Since this thread came up in my search, but it left me without a great answer... The best way I found to do this was to use a SequenceInputStream:
 
import scala.collection.JavaConverters._
import java.io.{ByteArrayInputStream, InputStream, SequenceInputStream}

val strings: Iterator[String] = ???
val stream: InputStream = new SequenceInputStream({
  val i = strings map { s => new ByteArrayInputStream(s.getBytes("UTF-8")) }
  i.asJavaEnumeration
})
 
 

Betreff: [scala-user] Iterator to inputstream
Hello,

I want to convert an Iterator[String] to a java.io.InputStream but I didn't find an east way to do it.
The main challenge is I cannot store all the data in memory to say, creating the inputstream from the array of byte or something like that, it needs to be lazy.

Currently I am saving the iterator as temporary file and then opening an inputstream on the file.

Does anybody know a better way to do that ?
If the scala API can't do that kind of transformation, is there a library that would allow to do that ?

Thanks,
Ayoub.

 

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Oliver Ruebenacker
Solutions Architect at Altisource Labs
Be always grateful, but never satisfied.
Reply all
Reply to author
Forward
0 new messages