简体   繁体   English

BSON解码器异常

[英]BSON Decoder exception

Since Mongo uses BSON, I am using the BSONDecoder from Java API to get the BSON document from the Mongo query and print the string output. 由于Mongo使用BSON,因此我使用Java API中的BSONDecoder从Mongo查询中获取BSON文档并打印字符串输出。 In the following a byte[] array stores the bytes of the MongoDB document (when I print the hex values they are the same as in Wireshark) 在下面的byte []数组中存储MongoDB文档的字节(当我打印十六进制值时,它们与Wireshark中的相同)

  byte[] array = byteBuffer.array();
  BasicBSONDecoder decoder = new BasicBSONDecoder();
  BSONObject bsonObject = decoder.readObject(array);
  System.out.println(bsonObject.toString());

I get the following error: 我收到以下错误:

  org.bson.BSONException: should be impossible

Caused by: java.io.IOException: unexpected EOF at org.bson.BasicBSONDecoder$BSONInput._need(BasicBSONDecoder.java:327) at org.bson.BasicBSONDecoder$BSONInput.read(BasicBSONDecoder.java:364) at org.bson.BasicBSONDecoder.decodeElement(BasicBSONDecoder.java:118) at org.bson.BasicBSONDecoder._decode(BasicBSONDecoder.java:79) at org.bson.BasicBSONDecoder.decode(BasicBSONDecoder.java:57) at org.bson.BasicBSONDecoder.readObject(BasicBSONDecoder.java:42) at org.bson.BasicBSONDecoder.readObject(BasicBSONDecoder.java:32) ... 4 more 引起原因:java.io.IOException:org.bson.BasicBSONDecoder $ BSONInput.read(BasicBSONDecoder.java:364)上的org.bson.BasicBSONDecoder $ BSONInput._need(BasicBSONDecoder.java:327)出现意外EOF。位于org.bson.BasicBSONDecoder._decode(BasicBSONDecoder.java:79)的org.bson.BasicBSONDecoder.decode(BasicBSONDecoder.java:57)的BasicBSONDecoder.decodeElement(BasicBSONDecoder.java:118)在org.bson.BasicBSON。 .java:42),位于org.bson.BasicBSONDecoder.readObject(BasicBSONDecoder.java:32)...还有4个

Looking on the implementation https://github.com/mongodb/mongo-java-driver/blob/master/src/main/org/bson/LazyBSONDecoder.java it looks that it is caught in 查看实现https://github.com/mongodb/mongo-java-driver/blob/master/src/main/org/bson/LazyBSONDecoder.java ,看起来它陷入了

        throw new BSONException( "should be impossible" , ioe );

The above takes place in query to the database (by query I mean that byte[] array contains all the bytes after the document length). 上面是在查询数据库时发生的(通过查询,我的意思是byte []数组包含文档长度之后的所有字节)。 The query itself contains a string "ismaster" or in hex is "x10 ismaster x00 x01 x00 x00 x00 x00". 查询本身包含字符串“ ismaster”或十六进制为“ x10 ismaster x00 x01 x00 x00 x00 x00”。 I suspect it is the BSON format of {isMaster: 1}, but I still do not understand why it fails. 我怀疑它是{isMaster:1}的BSON格式,但是我仍然不明白为什么它会失败。

You say: 你说:

byte[] array contains all the bytes after the document length byte []数组包含文档长度之后的所有字节

If you are stripping off the first part of the BSON that's returned, you are not passing a valid BSON document to the parser/decoder. 如果要剥离返回的BSON的第一部分,则不会将有效的BSON文档传递给解析器/解码器。

See BSON spec for details, but in a nut-shell, the first four bytes are the total size of the binary document in little endian format. 有关详细信息,请参见BSON规范 。但是, 总的来说 ,前四个字节是小尾数格式的二进制文档的总大小。

You are getting an exception in the code that is basically trying to read an expected number of bytes. 您在代码中遇到了一个异常, 异常基本上是试图读取期望的字节数。 It read the first int32 as length and then tried to parse the rest of it as BSON elements (and got an exception when it didn't find a valid type in the next byte). 它读取第一个int32作为长度,然后尝试将其其余部分解析为BSON元素(并在下一个字节中找不到有效类型时得到了异常)。 Pass it everything you get back from the query, including document size and it will work correctly. 将您从查询中获得的所有信息(包括文档大小)传递给它,它将正常运行。

This works just fine: 这很好用:

byte[] array = new BigInteger("130000001069734d6173746572000100000000", 16).toByteArray();
BasicBSONDecoder decoder = new BasicBSONDecoder();
BSONObject bsonObject = decoder.readObject(array);
System.out.println(bsonObject.toString());

And produces this output: 并产生以下输出:

{ "isMaster" : 1} {“ isMaster”:1}

There is something wrong with the bytes in your byteBuffer. 您的byteBuffer中的字节有问题。 Note that you must include the whole document (including the first 4 bytes which are the size). 请注意,您必须包括整个文档(包括前4个字节)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM