由于 Mongo 使用 BSON,我使用 Java API 中的 BSONDecoder 从 Mongo 查询中获取 BSON 文档并打印字符串输出。在下面的 byte[] 数组中存储了 MongoDB 文档的字节(当我打印十六进制值时,它们与 Wireshark 中的相同)
byte[] array = byteBuffer.array();
BasicBSONDecoder decoder = new BasicBSONDecoder();
BSONObject bsonObject = decoder.readObject(array);
System.out.println(bsonObject.toString());
我收到以下错误:
org.bson.BSONException: should be impossible
原因:java.io.IOException:org.bson 的 org.bson.BasicBSONDecoder$BSONInput.read(BasicBSONDecoder.java:364) 的 org.bson.BasicBSONDecoder$BSONInput._need(BasicBSONDecoder.java:327) 出现意外 EOF。 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) 在 org.bson.BasicBSONDecoder.readObject(BasicBSONDecoder.java:32) ... 4 更多
查看实现 https://github.com/mongodb/mongo-java-driver/blob/master/src/main/org/bson/LazyBSONDecoder.java看起来它被困在
throw new BSONException( "should be impossible" , ioe );
以上发生在对数据库的查询中(通过查询,我的意思是 byte[] 数组包含文档长度之后的所有字节)。查询本身包含字符串“ismaster”或十六进制为“x10 ismaster x00 x01 x00 x00 x00 x00”。我怀疑是 {isMaster: 1} 的 BSON 格式,但我还是不明白为什么会失败。