为什么 FlatBufferBuilder.dataBuffer return 与 java.nio.ByteBuffer.wrap 不同?
Why does FlatBufferBuilder.dataBuffer return a different buffer than java.nio.ByteBuffer.wrap?
我正在尝试通过网络从 windows 上用 c# 编写的程序向 android 使用平面缓冲区编写的 java 应用程序发送浮点数。
根据 android 应用程序中接收到的字节,使用 java.nio.ByteBuffer.wrap 构建缓冲区。从这个缓冲区中,Example 对象被反序列化。然而,这会导致每个浮点数都设置为 0。
在下面的示例中显示了此行为。
为什么 "example2" return 的值是 0.0
而不是 20.0
?
这是我使用的模式:
// Example IDL file for our monster's schema.
namespace MyGame.Sample;
table Example {
myFloat:float;
}
root_type Example;
这是显示行为的 java 代码:
//The float
float myFloat = 20.0f;
FlatBufferBuilder builder = new FlatBufferBuilder(0);
//Build the example
Example.startExample(builder);
Example.addMyFloat(builder, myFloat);
int exampleInt = Example.endExample(builder);
builder.finish(exampleInt);
java.nio.ByteBuffer buf = builder.dataBuffer();
Example example = Example.getRootAsExample(buf);
System.out.println("1: Myfloat: " + example.myFloat());
byte[] bytesBuf = buf.array();
Example example2 = Example.getRootAsExample(java.nio.ByteBuffer.wrap(bytesBuf));
System.out.println("2: Myfloat: " + example2.myFloat());
输出:
System.out: 1: Myfloat: 20.0
System.out: 2: Myfloat: 0.0
array() 获取原始 underlyimg 缓冲区,其中包含偏移量可能不是 0 的 FlatBuffer。相反,FlatBufferBuilder 中有一个方法(我现在忘记了它的名字)可以为您提供缓冲区。
我正在尝试通过网络从 windows 上用 c# 编写的程序向 android 使用平面缓冲区编写的 java 应用程序发送浮点数。
根据 android 应用程序中接收到的字节,使用 java.nio.ByteBuffer.wrap 构建缓冲区。从这个缓冲区中,Example 对象被反序列化。然而,这会导致每个浮点数都设置为 0。
在下面的示例中显示了此行为。
为什么 "example2" return 的值是 0.0
而不是 20.0
?
这是我使用的模式:
// Example IDL file for our monster's schema.
namespace MyGame.Sample;
table Example {
myFloat:float;
}
root_type Example;
这是显示行为的 java 代码:
//The float
float myFloat = 20.0f;
FlatBufferBuilder builder = new FlatBufferBuilder(0);
//Build the example
Example.startExample(builder);
Example.addMyFloat(builder, myFloat);
int exampleInt = Example.endExample(builder);
builder.finish(exampleInt);
java.nio.ByteBuffer buf = builder.dataBuffer();
Example example = Example.getRootAsExample(buf);
System.out.println("1: Myfloat: " + example.myFloat());
byte[] bytesBuf = buf.array();
Example example2 = Example.getRootAsExample(java.nio.ByteBuffer.wrap(bytesBuf));
System.out.println("2: Myfloat: " + example2.myFloat());
输出:
System.out: 1: Myfloat: 20.0
System.out: 2: Myfloat: 0.0
array() 获取原始 underlyimg 缓冲区,其中包含偏移量可能不是 0 的 FlatBuffer。相反,FlatBufferBuilder 中有一个方法(我现在忘记了它的名字)可以为您提供缓冲区。