use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class SnappyCompressor method copy.
private void copy(final List<ByteBuf> source, final byte[] in) {
int offset = 0;
for (ByteBuf cur : source) {
int remaining = cur.remaining();
cur.get(in, offset, remaining);
offset += remaining;
}
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class KeyManagementService method streamRead.
private void streamRead(final Stream stream, final MongoKeyDecryptor keyDecryptor, final MonoSink<Void> sink) {
int bytesNeeded = keyDecryptor.bytesNeeded();
if (bytesNeeded > 0) {
AsynchronousChannelStream asyncStream = (AsynchronousChannelStream) stream;
final ByteBuf buffer = asyncStream.getBuffer(bytesNeeded);
asyncStream.getChannel().read(buffer.asNIO(), asyncStream.getSettings().getReadTimeout(MILLISECONDS), MILLISECONDS, null, new CompletionHandler<Integer, Void>() {
@Override
public void completed(final Integer integer, final Void aVoid) {
buffer.flip();
try {
keyDecryptor.feed(buffer.asNIO());
buffer.release();
streamRead(stream, keyDecryptor, sink);
} catch (Throwable t) {
sink.error(t);
}
}
@Override
public void failed(final Throwable t, final Void aVoid) {
buffer.release();
stream.close();
sink.error(t);
}
});
} else {
stream.close();
sink.success();
}
}
use of org.bson.ByteBuf in project mongo-java-driver by mongodb.
the class NettyStream method readAsync.
/**
* @param numBytes Must be equal to {@link #pendingReader}{@code .numBytes} when called by a Netty channel handler.
* @param handler Must be equal to {@link #pendingReader}{@code .handler} when called by a Netty channel handler.
* @param readTimeoutMillis Must be equal to {@link #NO_SCHEDULE_TIME} when called by a Netty channel handler.
* Timeouts may be scheduled only by the public read methods. Taking into account that concurrent pending
* readers are not allowed, there must not be a situation when threads attempt to schedule a timeout
* before the previous one is either cancelled or completed.
*/
private void readAsync(final int numBytes, final AsyncCompletionHandler<ByteBuf> handler, final long readTimeoutMillis) {
ByteBuf buffer = null;
Throwable exceptionResult = null;
synchronized (this) {
exceptionResult = pendingException;
if (exceptionResult == null) {
if (!hasBytesAvailable(numBytes)) {
if (pendingReader == null) {
// called by a public read method
pendingReader = new PendingReader(numBytes, handler, scheduleReadTimeout(readTimeoutTask, readTimeoutMillis));
}
} else {
CompositeByteBuf composite = allocator.compositeBuffer(pendingInboundBuffers.size());
int bytesNeeded = numBytes;
for (Iterator<io.netty.buffer.ByteBuf> iter = pendingInboundBuffers.iterator(); iter.hasNext(); ) {
io.netty.buffer.ByteBuf next = iter.next();
int bytesNeededFromCurrentBuffer = Math.min(next.readableBytes(), bytesNeeded);
if (bytesNeededFromCurrentBuffer == next.readableBytes()) {
composite.addComponent(next);
iter.remove();
} else {
next.retain();
composite.addComponent(next.readSlice(bytesNeededFromCurrentBuffer));
}
composite.writerIndex(composite.writerIndex() + bytesNeededFromCurrentBuffer);
bytesNeeded -= bytesNeededFromCurrentBuffer;
if (bytesNeeded == 0) {
break;
}
}
buffer = new NettyByteBuf(composite).flip();
}
}
if (// the read operation has completed
!(exceptionResult == null && buffer == null) && pendingReader != null) {
// we need to clear the pending reader
cancel(pendingReader.timeout);
this.pendingReader = null;
}
}
if (exceptionResult != null) {
handler.failed(exceptionResult);
}
if (buffer != null) {
handler.completed(buffer);
}
}
Aggregations