use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class LazyDBEncoderTest method testEncodingRegularDBObjectWithLazyDBEncoder.
@Test(expected = IllegalArgumentException.class)
public void testEncodingRegularDBObjectWithLazyDBEncoder() {
DBObject document = new BasicDBObject();
new LazyDBEncoder().writeObject(new BasicOutputBuffer(), document);
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class LazyDBEncoderTest method testEncodingObject.
@Test
public void testEncodingObject() {
byte[] bytes = new byte[] { 12, 0, 0, 0, 16, 97, 0, 1, 0, 0, 0, 0 };
DBObject document = new LazyDBObject(bytes, new LazyDBCallback(null));
OutputBuffer buffer = new BasicOutputBuffer();
new LazyDBEncoder().writeObject(buffer, document);
assertArrayEquals(bytes, buffer.toByteArray());
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BSONTest method checkEncodingAndDecoding.
private void checkEncodingAndDecoding(final BSONObject toEncodeAndDecode, final int expectedEncodedSize, final String expectedHash) throws IOException {
// check encoding
BSONEncoder bsonEncoder = new BasicBSONEncoder();
OutputBuffer buf = new BasicOutputBuffer();
bsonEncoder.set(buf);
bsonEncoder.putObject(toEncodeAndDecode);
assertEquals(expectedEncodedSize, buf.size());
assertEquals(expectedHash, hexMD5(buf.toByteArray()));
bsonEncoder.done();
// check decoding
BSONDecoder bsonDecoder = new BasicBSONDecoder();
BSONCallback callback = new BasicBSONCallback();
int numberOfBytesDecoded = bsonDecoder.decode(new ByteArrayInputStream(buf.toByteArray()), callback);
assertEquals(expectedEncodedSize, numberOfBytesDecoded);
assertEquals(callback.get(), toEncodeAndDecode);
// I believe this is an obscure way of checking the decoded object is the the one we expect
OutputBuffer buf2 = new BasicOutputBuffer();
bsonEncoder.set(buf2);
bsonEncoder.putObject((BSONObject) callback.get());
assertEquals(expectedEncodedSize, buf2.size());
assertEquals(expectedHash, hexMD5(buf2.toByteArray()));
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BSONTest method encodeDocumentToByteArray.
private byte[] encodeDocumentToByteArray(final BSONObject document) {
OutputBuffer outputBuffer = new BasicOutputBuffer();
BSONEncoder encoder = new BasicBSONEncoder();
encoder.set(outputBuffer);
encoder.putObject(document);
encoder.done();
return outputBuffer.toByteArray();
}
Aggregations