Search in sources :

Example 26 with BasicOutputBuffer

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);
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) Test(org.junit.Test)

Example 27 with BasicOutputBuffer

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());
}
Also used : OutputBuffer(org.bson.io.OutputBuffer) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) Test(org.junit.Test)

Example 28 with BasicOutputBuffer

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()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 29 with BasicOutputBuffer

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();
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Aggregations

BasicOutputBuffer (org.bson.io.BasicOutputBuffer)29 Test (org.junit.Test)12 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)10 BsonBinaryWriter (org.bson.BsonBinaryWriter)6 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)5 OutputBuffer (org.bson.io.OutputBuffer)5 ByteBufNIO (org.bson.ByteBufNIO)4 BsonBinaryReader (org.bson.BsonBinaryReader)3 Before (org.junit.Before)2 BsonParser (de.undercouch.bson4jackson.BsonParser)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteBuffer (java.nio.ByteBuffer)1 BSONEncoder (org.bson.BSONEncoder)1 BasicBSONEncoder (org.bson.BasicBSONEncoder)1 BsonDocument (org.bson.BsonDocument)1 Document (org.bson.Document)1 RawBsonDocument (org.bson.RawBsonDocument)1 JsonReader (org.bson.json.JsonReader)1