use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project mongo-java-driver by mongodb.
the class NettyStream method writeAsync.
@Override
public void writeAsync(final List<ByteBuf> buffers, final AsyncCompletionHandler<Void> handler) {
CompositeByteBuf composite = PooledByteBufAllocator.DEFAULT.compositeBuffer();
for (ByteBuf cur : buffers) {
composite.addComponent(true, ((NettyByteBuf) cur).asByteBuf());
}
channel.writeAndFlush(composite).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
handler.failed(future.cause());
} else {
handler.completed(null);
}
}
});
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project vert.x by eclipse.
the class VertxHandler method safeBuffer.
/**
* Copy and release the {@code buf} when necessary.
*
* <p> This methods assuming the has full ownership of the buffer.
*
* <p> This method assumes that pooled buffers are allocated by {@code PooledByteBufAllocator}
*
* <p> The returned buffer will not need to be released and can be wrapped by a {@link io.vertx.core.buffer.Buffer}.
*
* @param buf the buffer
* @return a safe buffer to use
*/
public static ByteBuf safeBuffer(ByteBuf buf) {
if (buf != Unpooled.EMPTY_BUFFER && (buf.alloc() instanceof PooledByteBufAllocator || buf instanceof CompositeByteBuf)) {
try {
if (buf.isReadable()) {
ByteBuf buffer = VertxByteBufAllocator.DEFAULT.heapBuffer(buf.readableBytes());
buffer.writeBytes(buf, buf.readerIndex(), buf.readableBytes());
return buffer;
} else {
return Unpooled.EMPTY_BUFFER;
}
} finally {
buf.release();
}
}
return buf;
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project crate by crate.
the class Netty4Utils method toByteBuf.
/**
* Turns the given BytesReference into a ByteBuf. Note: the returned ByteBuf will reference the internal
* pages of the BytesReference. Don't free the bytes of reference before the ByteBuf goes out of scope.
*/
public static ByteBuf toByteBuf(final BytesReference reference) {
if (reference.length() == 0) {
return Unpooled.EMPTY_BUFFER;
}
if (reference instanceof ByteBufBytesReference) {
return ((ByteBufBytesReference) reference).toByteBuf();
} else {
final BytesRefIterator iterator = reference.iterator();
// usually we have one, two, or three components from the header, the message, and a buffer
final List<ByteBuf> buffers = new ArrayList<>(3);
try {
BytesRef slice;
while ((slice = iterator.next()) != null) {
buffers.add(Unpooled.wrappedBuffer(slice.bytes, slice.offset, slice.length));
}
final CompositeByteBuf composite = Unpooled.compositeBuffer(buffers.size());
composite.addComponents(true, buffers);
return composite;
} catch (IOException ex) {
throw new AssertionError("no IO happens here", ex);
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project spring-framework by spring-projects.
the class MetadataEncoder method encodeEntries.
@SuppressWarnings("deprecation")
private DataBuffer encodeEntries(List<MetadataEntry> entries) {
if (this.isComposite) {
CompositeByteBuf composite = this.allocator.compositeBuffer();
try {
if (this.route != null) {
io.rsocket.metadata.CompositeMetadataCodec.encodeAndAddMetadata(composite, this.allocator, WellKnownMimeType.MESSAGE_RSOCKET_ROUTING, encodeRoute());
}
entries.forEach(entry -> {
Object value = entry.value();
io.rsocket.metadata.CompositeMetadataCodec.encodeAndAddMetadata(composite, this.allocator, entry.mimeType().toString(), value instanceof ByteBuf ? (ByteBuf) value : PayloadUtils.asByteBuf(encodeEntry(entry)));
});
return asDataBuffer(composite);
} catch (Throwable ex) {
composite.release();
throw ex;
}
} else if (this.route != null) {
Assert.isTrue(entries.isEmpty(), "Composite metadata required for route and other entries");
String routingMimeType = WellKnownMimeType.MESSAGE_RSOCKET_ROUTING.getString();
return this.metadataMimeType.toString().equals(routingMimeType) ? asDataBuffer(encodeRoute()) : encodeEntry(this.route, this.metadataMimeType);
} else {
Assert.isTrue(entries.size() == 1, "Composite metadata required for multiple entries");
MetadataEntry entry = entries.get(0);
if (!this.metadataMimeType.equals(entry.mimeType())) {
throw new IllegalArgumentException("Connection configured for metadata mime type " + "'" + this.metadataMimeType + "', but actual is `" + entries + "`");
}
return encodeEntry(entry);
}
}
use of org.apache.flink.shaded.netty4.io.netty.buffer.CompositeByteBuf in project java-chassis by ServiceComb.
the class TcpConnection method writeInContext.
protected void writeInContext() {
CompositeByteBuf cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
for (; ; ) {
ByteBuf buf = writeQueue.poll();
if (buf == null) {
break;
}
writeQueueSize.decrementAndGet();
cbb.addComponent(true, buf);
if (cbb.numComponents() == cbb.maxNumComponents()) {
netSocket.write(Buffer.buffer(cbb));
cbb = ByteBufAllocator.DEFAULT.compositeBuffer();
}
}
if (cbb.isReadable()) {
netSocket.write(Buffer.buffer(cbb));
}
}
Aggregations