Scala: InputStream to Array[Byte]

后端 未结 11 851
臣服心动
臣服心动 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条回答
  •  梦毁少年i
    2021-02-02 06:57

    def inputStreamToByteArray(is: InputStream): Array[Byte] = {
        val buf = ListBuffer[Byte]()
        var b = is.read()
        while (b != -1) {
            buf.append(b.byteValue)
            b = is.read()
        }
        buf.toArray
    }
    

提交回复
热议问题