Search in sources :

Example 16 with BasicOutputBuffer

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

the class BsonBinaryWriterTest method testPipeNestedDocument.

@Test
public void testPipeNestedDocument() {
    // {
    //    "value" : { "a" : true},
    //    "b"     : 2
    // }
    writer.writeStartDocument();
    writer.writeStartDocument("value");
    writer.writeBoolean("a", true);
    writer.writeEndDocument();
    writer.writeInt32("b", 2);
    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))));
    reader1.readStartDocument();
    reader1.readName();
    //pipe {'a':true} to writer
    newWriter.pipe(reader1);
    //continue reading from the same reader
    assertEquals(BsonType.INT32, reader1.readBsonType());
    assertEquals("b", reader1.readName());
    assertEquals(2, reader1.readInt32());
    BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
    //checking what writer piped
    reader2.readStartDocument();
    assertEquals(BsonType.BOOLEAN, reader2.readBsonType());
    assertEquals("a", reader2.readName());
    assertEquals(true, reader2.readBoolean());
    reader2.readEndDocument();
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 17 with BasicOutputBuffer

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

the class BsonBinaryWriterTest method testPipe.

//CHECKSTYLE:ON
@Test
public void testPipe() {
    writer.writeStartDocument();
    writer.writeBoolean("a", true);
    writer.writeEndDocument();
    byte[] bytes = buffer.toByteArray();
    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);
        } finally {
            reader.close();
        }
    } finally {
        newWriter.close();
    }
    assertArrayEquals(bytes, newBuffer.toByteArray());
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 18 with BasicOutputBuffer

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

the class BsonBinaryWriterTest method testPipeDocumentIntoTopLevel.

@Test
public void testPipeDocumentIntoTopLevel() {
    writer.writeStartDocument();
    writer.writeString("str", "value");
    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.pipe(reader1);
    BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
    //checking what writer piped
    reader2.readStartDocument();
    assertEquals("value", reader2.readString("str"));
    reader2.readEndDocument();
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 19 with BasicOutputBuffer

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

the class CodecTestUtil method prepareReaderWithObjectToBeDecoded.

static <T> BsonBinaryReader prepareReaderWithObjectToBeDecoded(final T objectToDecode, final Codec<T> codec) {
    BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
    BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
    byte[] documentAsByteArrayForReader;
    try {
        codec.encode(writer, objectToDecode, EncoderContext.builder().build());
        documentAsByteArrayForReader = outputBuffer.toByteArray();
    } finally {
        writer.close();
    }
    return new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(documentAsByteArrayForReader))));
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 20 with BasicOutputBuffer

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

the class DocumentCodecTest method setUp.

@Before
public void setUp() throws Exception {
    buffer = new BasicOutputBuffer();
    writer = new BsonBinaryWriter(buffer);
}
Also used : BsonBinaryWriter(org.bson.BsonBinaryWriter) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) Before(org.junit.Before)

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