use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project pravega by pravega.
the class DirectMemoryCache method get.
@Override
public BufferView get(int address) {
Exceptions.checkNotClosed(this.closed.get(), this);
List<ByteBuf> readBuffers = new ArrayList<>();
while (address != CacheLayout.NO_ADDRESS) {
// Locate the Buffer-Block for the current address.
int bufferId = this.layout.getBufferId(address);
int blockId = this.layout.getBlockId(address);
DirectMemoryBuffer b = this.buffers[bufferId];
// Fetch the read data into our buffer collection and then set the address to the next in the chain.
address = b.read(blockId, readBuffers);
}
if (readBuffers.isEmpty()) {
// Couldn't read anything, so this address must not point to anything.
return null;
} else {
// Compose the result and return it.
ByteBuf first = readBuffers.get(0);
ByteBuf result = readBuffers.size() == 1 ? first : new CompositeByteBuf(first.alloc(), false, readBuffers.size(), Lists.reverse(readBuffers));
this.metrics.get(result.readableBytes());
return new NonReleaseableByteBufWrapper(result);
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf 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);
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project grpc-java by grpc.
the class NettyHandlerTestBase method captureWrite.
protected final ByteBuf captureWrite(ChannelHandlerContext ctx) {
ArgumentCaptor<ByteBuf> captor = ArgumentCaptor.forClass(ByteBuf.class);
verify(ctx, atLeastOnce()).write(captor.capture(), any(ChannelPromise.class));
CompositeByteBuf composite = Unpooled.compositeBuffer();
for (ByteBuf buf : captor.getAllValues()) {
composite.addComponent(buf);
composite.writerIndex(composite.writerIndex() + buf.readableBytes());
}
return composite;
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project grpc-java by grpc.
the class AltsProtocolNegotiatorTest method protectShouldRoundtrip.
@Test
// List cast
@SuppressWarnings("unchecked")
public void protectShouldRoundtrip() throws Exception {
doHandshake();
// Write the message 1 character at a time. The message should be buffered
// and not interfere with the handshake.
final AtomicInteger writeCount = new AtomicInteger();
String message = "hello";
for (int ix = 0; ix < message.length(); ++ix) {
ByteBuf in = Unpooled.copiedBuffer(message, ix, 1, UTF_8);
// go/futurereturn-lsc
@SuppressWarnings("unused") Future<?> possiblyIgnoredError = channel.write(in).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
writeCount.incrementAndGet();
}
}
});
}
channel.flush();
// Capture the protected data written to the wire.
assertEquals(1, channel.outboundMessages().size());
ByteBuf protectedData = channel.readOutbound();
assertEquals(message.length(), writeCount.get());
// Read the protected message at the server and verify it matches the original message.
TsiFrameProtector serverProtector = serverHandshaker.createFrameProtector(channel.alloc());
List<ByteBuf> unprotected = new ArrayList<>();
serverProtector.unprotect(protectedData, (List<Object>) (List<?>) unprotected, channel.alloc());
// We try our best to remove the HTTP2 handler as soon as possible, but just by constructing it
// a settings frame is written (and an HTTP2 preface). This is hard coded into Netty, so we
// have to remove it here. See {@code Http2ConnectionHandler.PrefaceDecode.sendPreface}.
int settingsFrameLength = 9;
CompositeByteBuf unprotectedAll = new CompositeByteBuf(channel.alloc(), false, unprotected.size() + 1, unprotected);
ByteBuf unprotectedData = unprotectedAll.slice(settingsFrameLength, message.length());
assertEquals(message, unprotectedData.toString(UTF_8));
// Protect the same message at the server.
final AtomicReference<ByteBuf> newlyProtectedData = new AtomicReference<>();
serverProtector.protectFlush(Collections.singletonList(unprotectedData), new Consumer<ByteBuf>() {
@Override
public void accept(ByteBuf buf) {
newlyProtectedData.set(buf);
}
}, channel.alloc());
// Read the protected message at the client and verify that it matches the original message.
channel.writeInbound(newlyProtectedData.get());
assertEquals(1, channel.inboundMessages().size());
assertEquals(message, channel.<ByteBuf>readInbound().toString(UTF_8));
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project ambry by linkedin.
the class WriteCallback method writeToChannel.
/**
* Writes {@code writeCount} number of random chunks to the given {@link ByteBufferAsyncWritableChannel}.
* @param writeCount the number of chunks to write.
*/
public void writeToChannel(int writeCount) {
for (int i = 0; i < writeCount; i++) {
WriteCallback writeCallback = new WriteCallback(i);
byte[] data = new byte[100];
random.nextBytes(data);
Future<Long> future = null;
if (useNettyByteBuf) {
ByteBuf chunk = null;
if (!useCompositeByteBuf) {
chunk = ByteBufAllocator.DEFAULT.heapBuffer(data.length);
chunk.writeBytes(data);
} else {
CompositeByteBuf composite = ByteBufAllocator.DEFAULT.compositeHeapBuffer(100);
ByteBuf c = ByteBufAllocator.DEFAULT.heapBuffer(50);
c.writeBytes(data, 0, 50);
composite.addComponent(true, c);
c = ByteBufAllocator.DEFAULT.heapBuffer(50);
c.writeBytes(data, 50, 50);
composite.addComponent(true, c);
chunk = composite;
}
final ByteBuf finalByteBuf = chunk;
future = channel.write(finalByteBuf, (result, exception) -> {
finalByteBuf.release();
writeCallback.onCompletion(result, exception);
});
} else {
ByteBuffer chunk = ByteBuffer.wrap(data);
future = channel.write(chunk, writeCallback);
}
writes.add(new WriteData(data, future, writeCallback));
}
}
Aggregations