Search in sources :

Example 11 with ByteBufferBsonInput

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

the class ReplyMessageTest method shouldThrowExceptionIfOpCodeIsIncorrect.

@Test(expected = MongoInternalException.class)
public void shouldThrowExceptionIfOpCodeIsIncorrect() {
    int badOpCode = 2;
    ByteBuffer headerByteBuffer = ByteBuffer.allocate(36);
    headerByteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    headerByteBuffer.putInt(36);
    headerByteBuffer.putInt(2456);
    headerByteBuffer.putInt(5);
    headerByteBuffer.putInt(badOpCode);
    headerByteBuffer.putInt(0);
    headerByteBuffer.putLong(0);
    headerByteBuffer.putInt(0);
    headerByteBuffer.putInt(0);
    headerByteBuffer.flip();
    ByteBufferBsonInput headerInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(headerByteBuffer));
    ReplyHeader replyHeader = new ReplyHeader(headerInputBuffer, ConnectionDescription.getDefaultMaxMessageSize());
    new ReplyMessage<Document>(replyHeader, 5);
}
Also used : ByteBufNIO(org.bson.ByteBufNIO) ByteBuffer(java.nio.ByteBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 12 with ByteBufferBsonInput

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

the class ByteBufBsonDocument method toBsonDocument.

private BsonDocument toBsonDocument() {
    ByteBuf duplicateByteBuf = byteBuf.duplicate();
    BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(duplicateByteBuf));
    try {
        return new BsonDocumentCodec().decode(bsonReader, DecoderContext.builder().build());
    } finally {
        duplicateByteBuf.release();
        bsonReader.close();
    }
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 13 with ByteBufferBsonInput

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

the class ByteBufBsonDocument method findInDocument.

private <T> T findInDocument(final Finder<T> finder) {
    ByteBuf duplicateByteBuf = byteBuf.duplicate();
    BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(duplicateByteBuf));
    try {
        bsonReader.readStartDocument();
        while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            T found = finder.find(bsonReader);
            if (found != null) {
                return found;
            }
        }
        bsonReader.readEndDocument();
    } finally {
        duplicateByteBuf.release();
        bsonReader.close();
    }
    return finder.notFound();
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 14 with ByteBufferBsonInput

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

the class BsonBinaryWriterTest method testMarkAndReset.

// CHECKSTYLE:OFF
@Test
public void testMarkAndReset() throws IOException {
    writer.writeStartDocument();
    writer.writeStartArray("a");
    {
        writer.writeStartDocument();
        writer.writeInt32("i", 1);
        writer.writeEndDocument();
    }
    writer.mark();
    {
        writer.writeStartDocument();
        writer.writeInt32("i", 2);
        writer.writeEndDocument();
    }
    writer.reset();
    {
        writer.writeStartDocument();
        writer.writeInt32("i", 3);
        writer.writeEndDocument();
    }
    writer.writeEndArray();
    writer.writeEndDocument();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    buffer.pipe(baos);
    ByteBufferBsonInput basicInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(baos.toByteArray())));
    BsonBinaryReader reader = new BsonBinaryReader(basicInputBuffer);
    try {
        reader.readStartDocument();
        reader.readName("a");
        reader.readStartArray();
        {
            reader.readStartDocument();
            assertEquals(1, reader.readInt32("i"));
            reader.readEndDocument();
        }
        {
            reader.readStartDocument();
            assertEquals(3, reader.readInt32("i"));
            reader.readEndDocument();
        }
        reader.readEndArray();
        reader.readEndDocument();
    } finally {
        reader.close();
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) Test(org.junit.Test)

Example 15 with ByteBufferBsonInput

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

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