将Scq中的Sec [Byte]。如何将其转换为java byte []或Input Stream?将Scala中的Seq [Byte]转换为byte []/InputStream
0
A
回答
0
您可以使用copyToArray
复制Seq
的内容。
val myseq: Seq[Byte] = ???
val myarray = new Array[Byte](myseq.size)
myseq.copyToArray(myarray)
注意,这将通过序列重复两次,这可能是不可取的,不可能的,还是蛮好的,这取决于您的使用。
4
不会
val a: Seq[Byte] = List()
a.toArray
做的工作?
0
一个明智的选择:
val byteSeq: Seq[Byte] = ???
val byteArray: Array[Byte] = bSeq.toArray
val inputStream = java.io.ByteArrayInputStream(byteArray)
一个不太明智的选择:
object HelloWorld {
implicit class ByteSequenceInputStream(val byteSeq: Seq[Byte]) extends java.io.InputStream {
private var pos = 0
val size = byteSeq.size
override def read(): Int = pos match {
case `size` => -1 // backticks match against the value in the variable
case _ => {
val result = byteSeq(pos).toInt
pos = pos + 1
result
}
}
}
val testByteSeq: Seq[Byte] = List(1, 2, 3, 4, 5).map(_.toByte)
def testConversion(in: java.io.InputStream): Unit = {
var done = false
while (! done) {
val result = in.read()
println(result)
done = result == -1
}
}
def main(args: Array[String]): Unit = {
testConversion(testByteSeq)
}
}
相关问题
- 1. 如何将byte []转换为InputStream?
- 2. 的byte []的InputStream转换成字符串
- 3. 转换IEnumerable的<byte[]>为byte []
- 4. Byte [] InputStream或OutputStream
- 5. C# - 将uint转换为byte []
- 6. 将byte []转换为UInt16。
- 7. 在Java中,如何将InputStream转换为字节数组(byte [])?
- 8. BYTE转换为DWORD
- 9. 使用Tritonus AudioSystem:将PCM byte []数组转换为mp3 byte []数组
- 10. 将InputStream转换为byte []的最有效方法?
- 11. 我需要将org.apache.spark.rdd.RDD [Array [Byte]]转换为Spark Streaming中的Array [Byte]
- 12. 将字符串转换为byte []和byte []转换为JAVA中的字符串
- 13. 将图像byte []转换为文件
- 14. 如何将设置变量转换为byte/file/inputstream/string?
- 15. 将byte []转换为Java中的Object File
- 16. 将int []转换为C#中的byte []
- 17. pdfbox将pdf转换为图像byte []
- 18. 如何将int转换为byte []并重新设置byte []的值
- 19. 在java中将byte []转换为long []
- 20. 在.NET中将int转换为byte [4]
- 21. 在Android中将int转换为byte []
- 22. 在VB.Net中将MemoryStream转换为Byte Array
- 23. 从floatbuffer转换为byte []
- 24. 从`const char *'转换为`byte'
- 25. 将Zip文件转换为byte []和byte []以压缩文件
- 26. StackExchange.Redis将RedisValue转换为byte []通过“as byte []”返回null
- 27. 将byte * {aka unsigned char *}'转换为byte [32] {aka unsigned char [32]}
- 28. 如何将类型'system.linq.iqueryable <byte[]>'转换为'byte []'
- 29. byte []转换为灰度BitmapImage
- 30. 将jp2的byte []转换为jpg文件
明智的选择合作。谢谢 ! – Arpit