Example usage for io.netty.buffer CompositeByteBuf release

List of usage examples for io.netty.buffer CompositeByteBuf release

Introduction

In this page you can find the example usage for io.netty.buffer CompositeByteBuf release.

Prototype

@Override
    public boolean release() 

Source Link

Usage

From source file:com.linecorp.armeria.client.encoding.ZlibStreamDecoder.java

License:Apache License

private byte[] fetchDecoderOutput() {
    CompositeByteBuf decoded = Unpooled.compositeBuffer();
    for (;;) {/*from w w w  .ja  va 2 s. c  o  m*/
        ByteBuf buf = decoder.readInbound();
        if (buf == null) {
            break;
        }
        if (!buf.isReadable()) {
            buf.release();
            continue;
        }
        decoded.addComponent(true, buf);
    }
    byte[] ret = ByteBufUtil.getBytes(decoded);
    decoded.release();
    return ret;
}

From source file:io.grpc.alts.internal.BufUnwrapperTest.java

License:Apache License

@Test
public void readableNioBuffers_worksWithComposite() {
    CompositeByteBuf buf = alloc.compositeBuffer();
    buf.addComponent(true, alloc.buffer(1).writeByte('a'));
    try (BufUnwrapper unwrapper = new BufUnwrapper()) {
        ByteBuffer[] internalBufs = unwrapper.readableNioBuffers(buf);
        Truth.assertThat(internalBufs).hasLength(1);

        assertEquals('a', internalBufs[0].get(0));
    } finally {/*from w w w .  ja  v  a2s .  c o  m*/
        buf.release();
    }
}

From source file:io.grpc.alts.internal.BufUnwrapperTest.java

License:Apache License

@Test
public void writableNioBuffers_worksWithComposite() {
    CompositeByteBuf buf = alloc.compositeBuffer();
    buf.addComponent(alloc.buffer(1));/*w  w  w  .j av a 2s  .c om*/
    buf.capacity(1);
    try (BufUnwrapper unwrapper = new BufUnwrapper()) {
        ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf);
        Truth.assertThat(internalBufs).hasLength(1);

        internalBufs[0].put((byte) 'a');

        buf.writerIndex(1);
        assertEquals('a', buf.readByte());
    } finally {
        buf.release();
    }
}

From source file:org.asynchttpclient.netty.util.ByteBufUtils.java

License:Open Source License

public static String byteBuf2String(Charset charset, ByteBuf... bufs) throws CharacterCodingException {
    if (charset == UTF_8 || charset == US_ASCII) {
        return Utf8ByteBufCharsetDecoder.decodeUtf8(bufs);
    } else {//from  ww w.ja v  a2  s .  c om
        CompositeByteBuf composite = Unpooled.compositeBuffer(bufs.length);

        try {
            for (ByteBuf buf : bufs) {
                buf.retain();
                composite.addComponent(buf);
            }

            return composite.toString(charset);

        } finally {
            composite.release();
        }
    }
}

From source file:org.springframework.messaging.rsocket.MetadataEncoder.java

License:Apache License

/**
 * Encode the collected metadata entries to a {@code DataBuffer}.
 * @see PayloadUtils#createPayload(DataBuffer, DataBuffer)
 *///from  w ww .  j a  va  2s  .  c o m
public DataBuffer encode() {
    if (this.isComposite) {
        CompositeByteBuf composite = this.allocator.compositeBuffer();
        try {
            if (this.route != null) {
                CompositeMetadataFlyweight.encodeAndAddMetadata(composite, this.allocator,
                        WellKnownMimeType.MESSAGE_RSOCKET_ROUTING, encodeRoute());
            }
            this.metadata.forEach((value, mimeType) -> {
                ByteBuf metadata = (value instanceof ByteBuf ? (ByteBuf) value
                        : PayloadUtils.asByteBuf(encodeEntry(value, mimeType)));
                CompositeMetadataFlyweight.encodeAndAddMetadata(composite, this.allocator, mimeType.toString(),
                        metadata);
            });
            return asDataBuffer(composite);
        } catch (Throwable ex) {
            composite.release();
            throw ex;
        }
    } else if (this.route != null) {
        Assert.isTrue(this.metadata.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(this.metadata.size() == 1, "Composite metadata required for multiple entries");
        Map.Entry<Object, MimeType> entry = this.metadata.entrySet().iterator().next();
        if (!this.metadataMimeType.equals(entry.getValue())) {
            throw new IllegalArgumentException("Connection configured for metadata mime type " + "'"
                    + this.metadataMimeType + "', but actual is `" + this.metadata + "`");
        }
        return encodeEntry(entry.getKey(), entry.getValue());
    }
}

From source file:org.wso2.carbon.gateway.internal.mediation.camel.CarbonMessageTypeConverter.java

License:Open Source License

@SuppressWarnings("unchecked")

public <T> T convertTo(Class<T> type, Exchange exchange, Object value) {
    if (value instanceof CarbonMessage) {
        //Retrieving the Pipe from the carbon message
        Pipe pipe = ((CarbonMessage) value).getPipe();
        //Input stream used for building the desired message
        ByteBufInputStream byteBufInputStream = null;
        //Create a composite buffer from content chunks in the pipe
        CompositeByteBuf contentBuf = aggregateChunks(pipe);
        //Check whether we have any content to be processed
        if (contentBuf.capacity() != 0) {
            try {
                if (type.isAssignableFrom(Document.class)) {
                    //Convert the input stream into xml dom element
                    return (T) toDocument(contentBuf, exchange);
                } else if (type.isAssignableFrom(DOMSource.class)) {
                    return (T) toDOMSource(contentBuf, exchange);
                } else if (type.isAssignableFrom(SAXSource.class)) {
                    return (T) toSAXSource(contentBuf, exchange);
                } else if (type.isAssignableFrom(StAXSource.class)) {
                    return (T) toStAXSource(contentBuf, exchange);
                } else if (type.isAssignableFrom(StreamSource.class)) {
                    return (T) toStreamSource(contentBuf, exchange);
                } else if (type.isAssignableFrom(InputStream.class)) {
                    return (T) toInputStream(contentBuf, exchange);
                } else if (type.isAssignableFrom(String.class)) {
                    return (T) toString(contentBuf, exchange);
                }//from www .ja  v a2 s.  c  o m
            } catch (UnsupportedEncodingException e) {
                log.error("Error occurred during type conversion", e);
            } finally {
                //Release the buffer if all the content has been consumed
                if (contentBuf.readerIndex() == contentBuf.writerIndex()) {
                    contentBuf.release();
                }
            }
        }

    }
    return null;
}

From source file:ratpack.stream.bytebuf.ByteBufStreams.java

License:Apache License

/**
 * Reduces the stream to a single composite byte buf.
 *
 * @param publisher the stream/*from  w  w  w. ja  va 2 s . c o m*/
 * @param alloc the buffer allocator
 * @return the reduced composite buffer
 */
public static Promise<CompositeByteBuf> compose(Publisher<? extends ByteBuf> publisher,
        ByteBufAllocator alloc) {
    return Promise.flatten(() -> {
        CompositeByteBuf seed = alloc.compositeBuffer();
        return Streams.reduce(publisher, seed, (c, b) -> c.addComponent(true, b)).onError(e -> {
            seed.release();
            throw Exceptions.toException(e);
        });
    });
}