Java readInt方法返回LittleEndian而不是Scala中Int的BigEndian值

问题描述 投票:1回答:1

我正在使用Kryo反序列化最初在Spark中序列化的类。 Kryo以BigEndian格式编写所有原语,但是当我尝试反序列化另一台机器上的值时,返回的值就好像它是LittleEndian一样。

Kryo的基础方法:

public int readInt () throws KryoException {
    require(4); // Does a basic positionality check that passes in this case
    byte[] buffer = this.buffer;
    int p = this.position;
    this.position = p + 4;
    return buffer[p] & 0xFF //
        | (buffer[p + 1] & 0xFF) << 8 //
        | (buffer[p + 2] & 0xFF) << 16 //
        | (buffer[p + 3] & 0xFF) << 24;
}

这将返回值0x70000000。但是当我的程序(在Scala中)使用Kryo的readByte方法时:

public byte readByte () throws KryoException {
    if (position == limit) require(1);
    return buffer[position++];
}

并单独读取字节,如下所示:

  val a = input.readByte()
  val b = input.readByte()
  val c = input.readByte()
  val d = input.readByte()
  val x = (a & 0xFF) << 24 | (b & 0xFF) << 16 | (c & 0xFF) << 8 | d & 0xFF

然后我得x为0x70。我不明白这里发生了什么。它是Scala和Java之间的某种转换问题,还是与Kryo和underling字节数组有关?

java scala endianness kryo
1个回答
2
投票

你写的代码:

  val a = input.readByte()
  val b = input.readByte()
  val c = input.readByte()
  val d = input.readByte()
  val x = (a & 0xFF) << 24 | (b & 0xFF) << 16 | (c & 0xFF) << 8 | d & 0xFF

是以错误的方式将字节转换为int。如果你仔细检查readInt()方法,你会看到你已经切换了订单。

  val x = (a & 0xFF) | (b & 0xFF) << 8 | (c & 0xFF) << 16 | d & 0xFF << 24;

写这个是正确的方法。

© www.soinside.com 2019 - 2024. All rights reserved.