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();
}
}
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());
}
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();
}
}
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();
}
}
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();
}
Aggregations