Scala: InputStream to Array[Byte]

后端 未结 11 869
臣服心动
臣服心动 2021-02-02 06:01

With Scala, what is the best way to read from an InputStream to a bytearray?

I can see that you can convert an InputStream to char array

Source.fromInput         


        
11条回答
  •  春和景丽
    2021-02-02 06:52

    Here's an approach using scalaz-stream:

    import scalaz.concurrent.Task
    import scalaz.stream._
    import scodec.bits.ByteVector
    
    def allBytesR(is: InputStream): Process[Task, ByteVector] =
      io.chunkR(is).evalMap(_(4096)).reduce(_ ++ _).lastOr(ByteVector.empty)
    

提交回复
热议问题