Search in sources :

Example 31 with CompositeByteBuf

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);
            }
        }
    });
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ChannelFuture(io.netty.channel.ChannelFuture) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(org.bson.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) MongoSocketOpenException(com.mongodb.MongoSocketOpenException) MongoClientException(com.mongodb.MongoClientException) MongoSocketException(com.mongodb.MongoSocketException) MongoException(com.mongodb.MongoException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MongoInternalException(com.mongodb.MongoInternalException) ReadTimeoutException(io.netty.handler.timeout.ReadTimeoutException) MongoInterruptedException(com.mongodb.MongoInterruptedException) IOException(java.io.IOException) MongoSocketReadTimeoutException(com.mongodb.MongoSocketReadTimeoutException)

Example 32 with CompositeByteBuf

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;
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 33 with CompositeByteBuf

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);
        }
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) BytesRefIterator(org.apache.lucene.util.BytesRefIterator) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf) BytesRef(org.apache.lucene.util.BytesRef)

Example 34 with CompositeByteBuf

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);
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Example 35 with CompositeByteBuf

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));
    }
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

CompositeByteBuf (io.netty.buffer.CompositeByteBuf)86 ByteBuf (io.netty.buffer.ByteBuf)65 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 Test (org.junit.Test)8 ByteBuffer (java.nio.ByteBuffer)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Channel (io.netty.channel.Channel)5 ChannelFutureListener (io.netty.channel.ChannelFutureListener)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 Test (org.junit.jupiter.api.Test)4 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)3 CodecException (io.netty.handler.codec.CodecException)3 SelfSignedCertificate (io.netty.handler.ssl.util.SelfSignedCertificate)3 InetSocketAddress (java.net.InetSocketAddress)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3