Search in sources :

Example 6 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.

the class BsonBinaryWriterTest method testPipeDocumentIntoScopeDocument.

@Test
public void testPipeDocumentIntoScopeDocument() {
    writer.writeStartDocument();
    writer.writeInt32("i", 0);
    writer.writeEndDocument();
    byte[] bytes = buffer.toByteArray();
    BasicOutputBuffer newBuffer = new BasicOutputBuffer();
    BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
    BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
    newWriter.writeStartDocument();
    newWriter.writeJavaScriptWithScope("js", "i++");
    newWriter.pipe(reader1);
    newWriter.writeEndDocument();
    BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
    //checking what writer piped
    reader2.readStartDocument();
    reader2.readJavaScriptWithScope("js");
    reader2.readStartDocument();
    assertEquals(0, reader2.readInt32("i"));
    reader2.readEndDocument();
    reader2.readEndDocument();
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 7 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.

the class BsonBinaryWriterTest method setup.

@Before
public void setup() {
    buffer = new BasicOutputBuffer();
    writer = new BsonBinaryWriter(new BsonWriterSettings(100), new BsonBinaryWriterSettings(1024), buffer);
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) Before(org.junit.Before)

Example 8 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.

the class BsonBinaryWriterTest method testPipeOfDocumentWithInvalidSize.

@Test
public void testPipeOfDocumentWithInvalidSize() {
    // minimum document size is 5;
    byte[] bytes = { 4, 0, 0, 0 };
    BasicOutputBuffer newBuffer = new BasicOutputBuffer();
    BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
    try {
        BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
        try {
            newWriter.pipe(reader);
            fail("Pipe is expected to fail with document size is < 5");
        } catch (BsonSerializationException e) {
        // expected
        } finally {
            reader.close();
        }
    } finally {
        newWriter.close();
    }
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 9 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.

the class DBEncoderAdapter method encode.

// TODO: this can be optimized to reduce copying of buffers.  For that we'd need an InputBuffer that could iterate
//       over an array of ByteBuffer instances from a PooledByteBufferOutputBuffer
@Override
public void encode(final BsonWriter writer, final DBObject document, final EncoderContext encoderContext) {
    BasicOutputBuffer buffer = new BasicOutputBuffer();
    try {
        encoder.writeObject(buffer, document);
        BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(buffer.toByteArray()))));
        try {
            writer.pipe(reader);
        } finally {
            reader.close();
        }
    } finally {
        buffer.close();
    }
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 10 with BasicOutputBuffer

use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.

the class BasicBSONEncoder method encode.

@Override
public byte[] encode(final BSONObject document) {
    OutputBuffer outputBuffer = new BasicOutputBuffer();
    set(outputBuffer);
    putObject(document);
    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