[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:

 
 
       byte[] bytes = new byte[]
                {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};
 
        BasicBSONDecoder decoder = new BasicBSONDecoder();
        BSONObject obj = decoder.readObject(bytes);
 
        System.out.println(obj);
 
        BasicBSONEncoder encoder = new BasicBSONEncoder();
 
        byte[] encoded = encoder.encode(obj);
 
        obj = decoder.readObject(bytes);
 
        System.out.println( obj);

which outputs:

{ "you" : "127.0.0.1:55487" , "ok" : 1.0}
{ "you" : "127.0.0.1:55487" , "ok" : 1.0}

Here's a description of the BSON so you can see what's going on:

        42, 0, 0, 0,                      document size: 42 
            2,                                type of first field: string
                121, 111, 117, 0,        cstring field name: "you"
                16, 0, 0, 0,              length of string: 16 
                49, 50, 55, 46, 48, 46, 48, 46, 49, 58, 53, 53, 52, 56, 55,    string: "127.0.0.1:55487" 
                0,                           null terminator of string
            1,                               type of second field: double
                111, 107, 0,               cstring field name: "ok" 
                0, 0, 0, 0, 0, 0, -16, 63,  double: 1.0 
            0,                               null terminator of document 
            5, 0, 0, 0, 0;               superfluous bytes

Generated at Thu Feb 08 08:56:23 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.