Scala:InputStream到数组[Byte] [英] Scala: InputStream to Array[Byte]
本文介绍了Scala:InputStream到数组[Byte]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Scala,从InputStream读取到字节数组的最佳方法是什么?
With Scala, what is the best way to read from an InputStream to a bytearray?
我看到您可以将InputStream转换为char数组
I can see that you can convert an InputStream to char array
Source.fromInputStream(is).toArray()
推荐答案
怎么样:
Stream.continually(is.read).takeWhile(_ != -1).map(_.toByte).toArray
更新:
Use LazyList instead of Stream
(因为不推荐使用2.13.x)
Update:
Use LazyList instead of Stream
(since 2.13.x deprecated)
LazyList.continually(is.read).takeWhile(_ != -1).map(_.toByte).toArray
这篇关于Scala:InputStream到数组[Byte]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文