如何将 Java 字节数组转换为 Scala 字节数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6237666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert a Java byte array into a Scala byte array?
提问by mtsz
I am new to Scala and work currently on a project involving both Java and a Scala modules. Now I'd like to call a Scala method from Java using a parameter of type byte[].
我是 Scala 的新手,目前正在从事一个涉及 Java 和 Scala 模块的项目。现在我想使用 byte[] 类型的参数从 Java 调用 Scala 方法。
The Scala method has the signature: def foo(data: Array[Byte])
Scala 方法具有以下签名: def foo(data: Array[Byte])
The Java call looks like this: foo(x), where x has the type byte[].
Java 调用如下所示:foo(x),其中 x 的类型为byte[]。
The IDE tells me its not possible:
IDE 告诉我这是不可能的:
The method foo(Array) in the type Bar is not applicable for the arguments (byte[])
As an additional constraint it is not preferred to change the Scala method. On the Java side I tried using Byte[], but this didn't solve the problem. There must exist some conversion?
作为额外的约束,最好不要更改 Scala 方法。在 Java 方面,我尝试使用Byte[],但这并没有解决问题。一定存在某种转换吗?
采纳答案by mtsz
As others pointed out, there is no problem in conversion. My IDE is behaving erroneous, and showing imaginary errors which compile without problems. At this moment the call of the receive Method in the main-method in following code is marked with the error:
正如其他人指出的那样,转换没有问题。我的 IDE 出现错误,并显示编译时没有问题的假想错误。此时调用如下代码中main-method中的receive方法会报错:
The method receive(Array) from the type ScalaByteReceiver refers to the missing type Array
But this code, which exemplifies my question, compiles fine and yields the expected result:
但是这段代码,它举例说明了我的问题,编译得很好并产生了预期的结果:
Java:
爪哇:
package stackOverflow;
public class JavaByteSender {
public static void main(String... args) {
new ScalaByteReceiver().receive(new byte[4]);
}
}
Scala:
斯卡拉:
package stackOverflow
import stackOverflow._
class ScalaByteReceiver{
def receive(bytes: Array[Byte]) {
println(bytes.length);
// prints 4
}
}
So Java and Scala understand each other nicely.
所以 Java 和 Scala 很好地相互理解。
回答by JaimeJorge
I tried to reproduce your error but it ran as expected. Running with scala 2.9.0 and sbt
我试图重现您的错误,但它按预期运行。使用 Scala 2.9.0 和 sbt 运行
java code:
代码:
package stackOverflow;
public class ByteContainer {
private byte[] bytes;
public ByteContainer(byte[] bytes){
this.bytes = bytes;
}
public byte[] getBytes() {
return bytes;
}
public void setBytes(byte[] bytes) {
this.bytes = bytes;
}
}
scala code:
斯卡拉代码:
package stackOverflow
import stackOverflow._
class ScalaByte{
val bytes:Array[Byte] = "this is my test".getBytes()
}
object ByteUser extends App{
val b = new ByteContainer((new ScalaByte()).bytes)
val s = b.getBytes()
println(s)
}
output: [B@6ef38f6f
输出:[B@6ef38f6f
This compiles and runs. Is this not what you were asking about? feel free to comment.
这将编译并运行。这不是你问的吗?随时发表评论。

