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 ByteBufBsonDocument method findInDocument.
<T> T findInDocument(final Finder<T> finder) {
ByteBuf duplicateByteBuf = byteBuf.duplicate();
BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(byteBuf.duplicate()));
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();
}
use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class TestInternalConnection method sendAndReceive.
@Override
public <T> T sendAndReceive(final CommandMessage message, final Decoder<T> decoder, final SessionContext sessionContext, final RequestContext requestContext) {
ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(this);
try {
message.encode(bsonOutput, sessionContext);
sendMessage(bsonOutput.getByteBuffers(), message.getId());
} finally {
bsonOutput.close();
}
ResponseBuffers responseBuffers = receiveMessage(message.getId());
try {
boolean commandOk = isCommandOk(new BsonBinaryReader(new ByteBufferBsonInput(responseBuffers.getBodyByteBuffer())));
responseBuffers.reset();
if (!commandOk) {
throw getCommandFailureException(getResponseDocument(responseBuffers, message, new BsonDocumentCodec()), description.getServerAddress());
}
return new ReplyMessage<T>(responseBuffers, decoder, message.getId()).getDocuments().get(0);
} finally {
responseBuffers.close();
}
}
use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class CryptConnection method commandAsync.
@Override
public <T> void commandAsync(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator, final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final SessionContext sessionContext, final ServerApi serverApi, final RequestContext requestContext, final boolean responseExpected, @Nullable final SplittablePayload payload, @Nullable final FieldNameValidator payloadFieldNameValidator, final SingleResultCallback<T> callback) {
if (serverIsLessThanVersionFourDotTwo(wrapped.getDescription())) {
callback.onResult(null, new MongoClientException("Auto-encryption requires a minimum MongoDB version of 4.2"));
return;
}
try {
BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
BsonBinaryWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(getDescription().getMaxDocumentSize()), bsonOutput, getFieldNameValidator(payload, commandFieldNameValidator, payloadFieldNameValidator));
BsonWriter writer = payload == null ? bsonBinaryWriter : new SplittablePayloadBsonWriter(bsonBinaryWriter, bsonOutput, createSplittablePayloadMessageSettings(), payload, MAX_SPLITTABLE_DOCUMENT_SIZE);
getEncoder(command).encode(writer, command, EncoderContext.builder().build());
crypt.encrypt(database, new RawBsonDocument(bsonOutput.getInternalBuffer(), 0, bsonOutput.getSize())).flatMap((Function<RawBsonDocument, Mono<RawBsonDocument>>) encryptedCommand -> Mono.create(sink -> wrapped.commandAsync(database, encryptedCommand, commandFieldNameValidator, readPreference, new RawBsonDocumentCodec(), sessionContext, serverApi, requestContext, responseExpected, null, null, sinkToCallback(sink)))).flatMap(crypt::decrypt).map(decryptedResponse -> commandResultDecoder.decode(new BsonBinaryReader(decryptedResponse.getByteBuffer().asNIO()), DecoderContext.builder().build())).subscribe(decryptedResult -> callback.onResult(decryptedResult, null), e -> callback.onResult(null, e));
} catch (Throwable t) {
callback.onResult(null, t);
}
}
use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class DocumentCodecTest method testCodeWithScopeEncoding.
@Test
public void testCodeWithScopeEncoding() throws IOException {
DocumentCodec documentCodec = new DocumentCodec();
Document doc = new Document();
doc.put("theCode", new CodeWithScope("javaScript code", new Document("fieldNameOfScope", "valueOfScope")));
documentCodec.encode(writer, doc, EncoderContext.builder().build());
Document decodedDocument = documentCodec.decode(new BsonBinaryReader(createInputBuffer()), DecoderContext.builder().build());
assertEquals(doc, decodedDocument);
}
Aggregations