Search in sources :

Example 16 with ByteBufferBsonInput

use of org.bson.io.ByteBufferBsonInput 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 17 with ByteBufferBsonInput

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

the class BsonBinaryWriterTest method testWriteRead.

@Test
public //CHECKSTYLE:OFF
void testWriteRead() throws IOException {
    ObjectId oid1 = new ObjectId();
    writer.writeStartDocument();
    {
        writer.writeBoolean("b1", true);
        writer.writeBoolean("b2", false);
        writer.writeStartArray("a1");
        {
            writer.writeString("danke");
            writer.writeString("");
        }
        writer.writeEndArray();
        writer.writeStartDocument("d1");
        {
            writer.writeDouble("do", 60);
            writer.writeInt32("i32", 40);
            writer.writeInt64("i64", Long.MAX_VALUE);
        }
        writer.writeEndDocument();
        writer.writeJavaScriptWithScope("js1", "print x");
        writer.writeStartDocument();
        {
            writer.writeInt32("x", 1);
        }
        writer.writeEndDocument();
        writer.writeObjectId("oid1", oid1);
    }
    writer.writeEndDocument();
    assertEquals(139, buffer.getPosition());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    buffer.pipe(baos);
    ByteBufferBsonInput basicInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(baos.toByteArray())));
    BsonBinaryReader reader = new BsonBinaryReader(basicInputBuffer);
    try {
        assertEquals(BsonType.DOCUMENT, reader.readBsonType());
        reader.readStartDocument();
        {
            assertEquals(BsonType.BOOLEAN, reader.readBsonType());
            assertEquals("b1", reader.readName());
            assertEquals(true, reader.readBoolean());
            assertEquals(BsonType.BOOLEAN, reader.readBsonType());
            assertEquals("b2", reader.readName());
            assertEquals(false, reader.readBoolean());
            assertEquals(BsonType.ARRAY, reader.readBsonType());
            assertEquals("a1", reader.readName());
            reader.readStartArray();
            {
                assertEquals(BsonType.STRING, reader.readBsonType());
                assertEquals("danke", reader.readString());
                assertEquals(BsonType.STRING, reader.readBsonType());
                assertEquals("", reader.readString());
            }
            assertEquals(BsonType.END_OF_DOCUMENT, reader.readBsonType());
            reader.readEndArray();
            assertEquals("d1", reader.readName());
            reader.readStartDocument();
            {
                assertEquals(BsonType.DOUBLE, reader.readBsonType());
                assertEquals("do", reader.readName());
                assertEquals(60, reader.readDouble(), 0);
                assertEquals(BsonType.INT32, reader.readBsonType());
                assertEquals("i32", reader.readName());
                assertEquals(40, reader.readInt32());
                assertEquals(BsonType.INT64, reader.readBsonType());
                assertEquals("i64", reader.readName());
                assertEquals(Long.MAX_VALUE, reader.readInt64());
            }
            assertEquals(BsonType.END_OF_DOCUMENT, reader.readBsonType());
            reader.readEndDocument();
            assertEquals(BsonType.JAVASCRIPT_WITH_SCOPE, reader.readBsonType());
            assertEquals("js1", reader.readName());
            assertEquals("print x", reader.readJavaScriptWithScope());
            reader.readStartDocument();
            {
                assertEquals(BsonType.INT32, reader.readBsonType());
                assertEquals("x", reader.readName());
                assertEquals(1, reader.readInt32());
            }
            assertEquals(BsonType.END_OF_DOCUMENT, reader.readBsonType());
            reader.readEndDocument();
            assertEquals(BsonType.OBJECT_ID, reader.readBsonType());
            assertEquals("oid1", reader.readName());
            assertEquals(oid1, reader.readObjectId());
            assertEquals(BsonType.END_OF_DOCUMENT, reader.readBsonType());
            reader.readEndDocument();
        }
    } finally {
        reader.close();
    }
}
Also used : ObjectId(org.bson.types.ObjectId) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 18 with ByteBufferBsonInput

use of org.bson.io.ByteBufferBsonInput 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 ByteBufferBsonInput

use of org.bson.io.ByteBufferBsonInput 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 ByteBufferBsonInput

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

the class DocumentCodecTest method createInputBuffer.

// TODO: factor into common base class;
private BsonInput createInputBuffer() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    buffer.pipe(baos);
    return new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(baos.toByteArray())));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufNIO(org.bson.ByteBufNIO) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Aggregations

ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)24 Test (org.junit.Test)11 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)10 ByteBufNIO (org.bson.ByteBufNIO)9 BsonBinaryReader (org.bson.BsonBinaryReader)7 ByteBuffer (java.nio.ByteBuffer)5 ByteBuf (org.bson.ByteBuf)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 StringWriter (java.io.StringWriter)1 Document (org.bson.Document)1 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)1 JsonWriter (org.bson.json.JsonWriter)1 ObjectId (org.bson.types.ObjectId)1