michel-kraemer/bson4jackson — Issue #11: BSONParser reads past end of document
Author: jroper
Created: 2011-12-08 14:09:20+00:00
State: open

The BSONParser assumes that it is entitled to read to the end of the stream. While this is usually a valid assumption for reading JSON, BSON is designed to be used as part of chatty binary protocols, for example, it's used by MongoDB, where a single stream will have more than just a single BSON document going across it. That's why the first 4 bytes of the stream is the size of the document, so that appropriate buffering can be done while reading, without interferring with the rest of the stream.

The BSONParser ignores the size of the document, and reads past the end of the document (as part of its buffering), which results in it consuming things it should not consume. I've solved this by wrapping the input stream that goes into it with an input stream that reads the first 4 bytes, decodes them into the size, outputs them as the first 4 bytes, and returns EOF once that size has been read, but really I think the BSONParser should handle this itself.