Search in sources :

Example 6 with BsonBinaryReader

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

the class ByteBufBsonDocument method toJson.

@Override
public String toJson(final JsonWriterSettings settings) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(stringWriter, settings);
    ByteBuf duplicate = byteBuf.duplicate();
    BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(duplicate));
    try {
        jsonWriter.pipe(reader);
        return stringWriter.toString();
    } finally {
        duplicate.release();
        reader.close();
    }
}
Also used : StringWriter(java.io.StringWriter) BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) JsonWriter(org.bson.json.JsonWriter) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 7 with BsonBinaryReader

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

the class MessageHelper method decodeCommand.

public static BsonDocument decodeCommand(final BsonInput bsonInput) {
    // length
    bsonInput.readInt32();
    //requestId
    bsonInput.readInt32();
    //responseTo
    bsonInput.readInt32();
    // opcode
    bsonInput.readInt32();
    // flags
    bsonInput.readInt32();
    //collectionName
    bsonInput.readCString();
    // numToSkip
    bsonInput.readInt32();
    // numToReturn
    bsonInput.readInt32();
    BsonBinaryReader reader = new BsonBinaryReader(bsonInput);
    return new BsonDocumentCodec().decode(reader, DecoderContext.builder().build());
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 8 with BsonBinaryReader

use of org.bson.BsonBinaryReader 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 9 with BsonBinaryReader

use of org.bson.BsonBinaryReader 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 10 with BsonBinaryReader

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

Aggregations

BsonBinaryReader (org.bson.BsonBinaryReader)14 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)12 Document (org.bson.Document)7 Test (org.junit.Test)6 BsonInput (org.bson.io.BsonInput)5 ByteBuf (org.bson.ByteBuf)3 ByteBufNIO (org.bson.ByteBufNIO)3 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)3 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)3 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 StringWriter (java.io.StringWriter)1 Arrays.asList (java.util.Arrays.asList)1 Date (java.util.Date)1 List (java.util.List)1 BsonObjectId (org.bson.BsonObjectId)1 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)1 JsonWriter (org.bson.json.JsonWriter)1 Binary (org.bson.types.Binary)1 Code (org.bson.types.Code)1 CodeWithScope (org.bson.types.CodeWithScope)1