use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class InternalStreamConnection method sendAndReceiveAsync.
@Override
public <T> void sendAndReceiveAsync(final CommandMessage message, final Decoder<T> decoder, final SessionContext sessionContext, final RequestContext requestContext, final SingleResultCallback<T> callback) {
notNull("stream is open", stream, callback);
if (isClosed()) {
callback.onResult(null, new MongoSocketClosedException("Can not read from a closed socket", getServerAddress()));
return;
}
ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(this);
ByteBufferBsonOutput compressedBsonOutput = new ByteBufferBsonOutput(this);
try {
message.encode(bsonOutput, sessionContext);
CommandEventSender commandEventSender = createCommandEventSender(message, bsonOutput, requestContext);
commandEventSender.sendStartedEvent();
if (sendCompressor == null || SECURITY_SENSITIVE_COMMANDS.contains(message.getCommandDocument(bsonOutput).getFirstKey())) {
sendCommandMessageAsync(message.getId(), decoder, sessionContext, callback, bsonOutput, commandEventSender, message.isResponseExpected());
} else {
List<ByteBuf> byteBuffers = bsonOutput.getByteBuffers();
try {
CompressedMessage compressedMessage = new CompressedMessage(message.getOpCode(), byteBuffers, sendCompressor, getMessageSettings(description));
compressedMessage.encode(compressedBsonOutput, sessionContext);
} finally {
releaseAllBuffers(byteBuffers);
bsonOutput.close();
}
sendCommandMessageAsync(message.getId(), decoder, sessionContext, callback, compressedBsonOutput, commandEventSender, message.isResponseExpected());
}
} catch (Throwable t) {
bsonOutput.close();
compressedBsonOutput.close();
callback.onResult(null, t);
}
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class AsynchronousChannelStream method readAsync.
private void readAsync(final int numBytes, final int additionalTimeout, final AsyncCompletionHandler<ByteBuf> handler) {
ByteBuf buffer = bufferProvider.getBuffer(numBytes);
int timeout = settings.getReadTimeout(MILLISECONDS);
if (timeout > 0 && additionalTimeout > 0) {
timeout += additionalTimeout;
}
channel.read(buffer.asNIO(), timeout, MILLISECONDS, null, new BasicCompletionHandler(buffer, handler));
}
use of org.bson.ByteBuf 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.ByteBuf 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.ByteBuf in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method createList.
static List<ByteBufBsonDocument> createList(final ByteBufferBsonOutput bsonOutput, final int startPosition) {
List<ByteBuf> duplicateByteBuffers = bsonOutput.getByteBuffers();
CompositeByteBuf outputByteBuf = new CompositeByteBuf(duplicateByteBuffers);
outputByteBuf.position(startPosition);
List<ByteBufBsonDocument> documents = new ArrayList<ByteBufBsonDocument>();
int curDocumentStartPosition = startPosition;
while (outputByteBuf.hasRemaining()) {
int documentSizeInBytes = outputByteBuf.getInt();
ByteBuf slice = outputByteBuf.duplicate();
slice.position(curDocumentStartPosition);
slice.limit(curDocumentStartPosition + documentSizeInBytes);
documents.add(new ByteBufBsonDocument(slice));
curDocumentStartPosition += documentSizeInBytes;
outputByteBuf.position(outputByteBuf.position() + documentSizeInBytes - 4);
}
for (ByteBuf byteBuffer : duplicateByteBuffers) {
byteBuffer.release();
}
return documents;
}
Aggregations