无法从 javascript 中的 protobuf 消息读取字节 [英] Can't read bytes from protobuf message in javascript
本文介绍了无法从 javascript 中的 protobuf 消息读取字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 javascript 对 protobuf 消息中的 bytes
进行编码/解码.
如图所示,字符串工作正常,但是在消息定义中使用 bytes
时,我可以看到数据包含在解码的字节缓冲区中(printDebug 显示了这一点),但我无法提取字符串:
I need to encode/decode bytes
in a protobuf message using javascript.
Strings work fine as shown, but when using bytes
in the message definition I can see the data is contained in the decoded bytebuffer (printDebug shows this), but I haven't been able to extract the string:
test.proto:
test.proto:
syntax = "proto3";
package Testing;
message Record {
string data = 1;
}
message Bytes {
bytes data = 1;
}
TestProto.js:
TestProto.js:
var ProtoBuf = require("protobufjs");
var ByteBuffer = require("bytebuffer");
var builder = ProtoBuf.loadProtoFile('test.proto');
var Testing = builder.build('Testing');
var test = new Testing.Record();
test.data = 'Hello World';
var encoded = test.encode();
encoded.printDebug();
var unencoded = Testing.Record.decode(encoded);
console.log(unencoded.data);
// prints 'Hello World'
console.log('\n\n');
var bytes = new Testing.Bytes();
var bb = new ByteBuffer();
bb.writeIString('Testing 1,2,3');
bb.flip();
bytes.data = bb;
var bytesEncoded = bytes.encode();
bytesEncoded.printDebug();
var bytesUnencoded = Testing.Bytes.decode(bytesEncoded);
bytesUnencoded.data.printDebug();
console.log(bytesUnencoded.data.readIString());
控制台输出:
$ node TestProto.js
ByteBufferNB(offset=0,markedOffset=-1,limit=13,capacity=16)
-------------------------------------------------------------------
<0A 0B 48 65 6C 6C 6F 20 57 6F 72 6C 64>00 00 00 ..Hello.World...
Hello World
ByteBufferNB(offset=0,markedOffset=-1,limit=19,capacity=32)
-------------------------------------------------------------------
<0A 11 00 00 00 0D 54 65 73 74 69 6E 67 20 31 2C ......Testing.1,
32 2C 33>00 00 02 00 00 01 00 00 00 01 00 00 00 2,3.............
ByteBufferNB(offset=2,markedOffset=-1,limit=19,capacity=32)
-------------------------------------------------------------------
0A 11<00 00 00 0D 54 65 73 74 69 6E 67 20 31 2C ......Testing.1,
32 2C 33>00 00 02 00 00 01 00 00 00 01 00 00 00 2,3.............
C:\node_modules\protobufjs\node_modules\bytebuffer\dist\ByteBufferNB.js:1874
throw RangeError("Index out of bounds: "+offset+" + "+temp+" <= "+this.buffer.length);
^
RangeError: Index out of bounds: 6 + 218103808 <= 32
at RangeError (native)
at module.exports.ByteBufferPrototype.readIString (C:\node_modules\protobufjs\node_modules\bytebuffer\dist\ByteBufferNB.js:1874:19)
at Object.<anonymous> (C:\TestProto.js:30:33)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3
推荐答案
在 readIString
解决问题之前,通过 bytesUnencoded.data 显式设置为 Big Endian.
Explicitly setting by bytesUnencoded.data to Big Endian before readIString
fixed the problem.
bytesUnencoded.data.BE();
console.log(bytesUnencoded.data.readIString());
这篇关于无法从 javascript 中的 protobuf 消息读取字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文