如何将Java字节数组转换为Scala字节数组?

时间:2021-01-04 07:30:36

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 [],但这并没有解决问题。必须存在一些转换?

2 个解决方案

#1


4  

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方法中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:

Java的:

package *;

public class JavaByteSender {    
    public static void main(String... args) {
    new ScalaByteReceiver().receive(new byte[4]);
    }
}

Scala:

斯卡拉:

package *

import *._

class ScalaByteReceiver{

  def receive(bytes: Array[Byte]) {    
    println(bytes.length);
    // prints 4
  }
}

So Java and Scala understand each other nicely.

所以Java和Scala很好地相互理解。

#2


1  

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:

java代码:

package *;

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 *

import *._

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.

这编译并运行。这不是你问的问题吗?随意发表评论。

#1


4  

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方法中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:

Java的:

package *;

public class JavaByteSender {    
    public static void main(String... args) {
    new ScalaByteReceiver().receive(new byte[4]);
    }
}

Scala:

斯卡拉:

package *

import *._

class ScalaByteReceiver{

  def receive(bytes: Array[Byte]) {    
    println(bytes.length);
    // prints 4
  }
}

So Java and Scala understand each other nicely.

所以Java和Scala很好地相互理解。

#2


1  

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:

java代码:

package *;

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 *

import *._

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.

这编译并运行。这不是你问的问题吗?随意发表评论。