[JAVA-2125] BSONEncoder.encode output cannot be processed by BSONDecoder.decode Created: 27/Feb/16 Updated: 11/Sep/19 Resolved: 27/Feb/16 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | BSON |
| Affects Version/s: | 3.2.2 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major - P3 |
| Reporter: | Rick Houlihan | Assignee: | Unassigned |
| Resolution: | Done | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
Byte array received from client is decoded into a BSONObject using BSONDecoder. Byte array received follows: [42, 0, 0, 0, 2, 121, 111, 117, 0, 16, 0, 0, 0, 49, 50, 55, 46, 48, 46, 48, 46, 49, 58, 53, 53, 52, 56, 55, 0, 1, 111, 107, 0, 0, 0, 0, 0, 0, 0, -16, 63, 0, 5, 0, 0, 0, 0] BSONObject is parsed and then encoded into a byte array using BSONEncoder. Byte array produced follows: [42, 0, 0, 0, 2, 121, 111, 117, 0, 16, 0, 0, 0, 49, 50, 55, 46, 48, 46, 48, 46, 49, 58, 53, 53, 52, 56, 55, 0, 1, 111, 107, 0, 0, 0, 0, 0, 0, 0, -16, 63, 0] The array generated by BSONEncoder appears to be missing an integer and a null character at the end of the array. Trying to decode the output of BSONEncoder using BSONDecoder results in an exception being thrown. It seems the expected outcome if no properties of the deserialized BSONObject are altered in that the output of BSONEncoder would be the same as the input to BSONDecoder. |
| Comments |
| Comment by Rick Houlihan [ 27/Feb/16 ] | |||||||||||||||||||||||||||||||||||
|
Apologies, late night. Found the problem...bad code. Thanks for your help. | |||||||||||||||||||||||||||||||||||
| Comment by Jeffrey Yemin [ 27/Feb/16 ] | |||||||||||||||||||||||||||||||||||
|
Hi Rick, This is not a bug. The initial byte array has 5 extra bytes at the end, which you can see by examining the first four bytes, [42, 0, 0, 0], which represents the number of bytes of the document in little endian. So the document is 42 bytes long, and BasicBSONDecoder stops reading the byte array at the 42nd byte, which is a 0 representing the end of the document. Please see http://bsonspec.org for a description of the BSON format. You'll need to look at where that byte array is coming from and why it has superfluous bytes at the end. I was unable to reproduce the exception you indicated. Here's the test program I used:
which outputs:
Here's a description of the BSON so you can see what's going on:
|