Example usage for io.netty.buffer ByteBuf release

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

Introduction

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

Prototype

boolean release();

Source Link

Document

Decreases the reference count by 1 and deallocates this object if the reference count reaches at 0 .

Usage

From source file:io.crate.protocols.postgres.types.PGTypesTest.java

License:Apache License

private Object writeAndReadAsText(Entry entry, PGType pgType) {
    ByteBuf buffer = Unpooled.buffer();
    try {/*from  w ww .  j  a  v a  2  s . c om*/
        pgType.writeAsText(buffer, entry.value);
        int length = buffer.readInt();
        return pgType.readTextValue(buffer, length);
    } finally {
        buffer.release();
    }
}

From source file:io.datty.msgpack.MessageIO.java

License:Apache License

/**
 * Release complex object with ByteBuf values
 * /*from  ww  w  .  j ava  2s  .com*/
 * @param value
 */

public static void release(Object value) {
    if (value == null) {
        return;
    }
    if (value instanceof Map) {
        Map<Object, Object> map = (Map<Object, Object>) value;
        for (Object v : map.values()) {
            if (v != null && v instanceof ByteBuf) {
                ByteBuf bb = (ByteBuf) v;
                bb.release();
            }
        }
    }
    if (value instanceof List) {
        List<Object> list = (List<Object>) value;
        for (Object v : list) {
            if (v != null && v instanceof ByteBuf) {
                ByteBuf bb = (ByteBuf) v;
                bb.release();
            }
        }
    }
}

From source file:io.enforcer.deathstar.ws.WebSocketServerHandler.java

License:Apache License

/**
 * Sends http response to clients//  w  w  w.j a  v  a  2s. c  o m
 *
 * @param ctx handler context
 * @param req http request
 * @param res http response
 */
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    if (res.status().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);
        buf.release();
        HttpHeaderUtil.setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!HttpHeaderUtil.isKeepAlive(req) || res.status().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:io.gatling.http.client.body.FormUrlEncodedRequestBodyTest.java

License:Apache License

private void formUrlEncoding(Charset charset) throws Exception {
    String key = "key";
    String value = "";
    List<Param> params = new ArrayList<>();
    params.add(new Param(key, value));
    ByteBuf bb = (ByteBuf) new FormUrlEncodedRequestBody(params, TEXT_PLAIN.toString(), charset)
            .build(false, ByteBufAllocator.DEFAULT).getContent();
    try {/*from www. ja va 2s .  c  om*/
        String ahcString = ByteBufUtils.byteBuf2String(US_ASCII, bb);
        String jdkString = key + "=" + URLEncoder.encode(value, charset.name());
        assertEquals(ahcString, jdkString);
    } finally {
        bb.release();
    }
}

From source file:io.gatling.http.client.body.multipart.impl.MultipartChunkedInput.java

License:Apache License

@Override
public ByteBuf readChunk(ByteBufAllocator alloc) throws Exception {

    if (endOfInput) {
        return null;
    }// w  w  w. j  a va2 s .co  m

    ByteBuf buffer = alloc.buffer(chunkSize);
    ChunkedInputState state = copyInto(buffer);
    progress += buffer.writerIndex();
    switch (state) {
    case STOP:
        endOfInput = true;
        return buffer;
    case SUSPEND:
        // this will suspend the stream in ChunkedWriteHandler
        buffer.release();
        return null;
    case CONTINUE:
        return buffer;
    default:
        throw new IllegalStateException("Unknown state: " + state);
    }
}

From source file:io.gatling.http.client.test.listener.CompleteResponseListener.java

License:Apache License

private void releaseChunks() {
    if (chunks != null) {
        for (ByteBuf chunk : chunks) {
            chunk.release();
        }/*from  ww w  .ja  v  a  2s. c  o m*/
    }
}

From source file:io.gatling.http.client.test.listener.ResponseAsStringListener.java

License:Apache License

protected String responseBody() {

    if (isNonEmpty(chunks)) {
        Charset charset = withDefault(
                extractContentTypeCharsetAttribute(headers.get(HttpHeaderNames.CONTENT_TYPE)), UTF_8);

        ByteBuf composite = Unpooled.wrappedBuffer(chunks.toArray(ByteBufUtils.EMPTY_BYTEBUF_ARRAY));
        try {/*  w w  w .  j  a v a  2 s  .  com*/
            return composite.toString(charset);
        } finally {
            composite.release();
        }
    }

    return null;
}

From source file:io.gatling.netty.util.ahc.ByteBufUtils.java

License:Apache License

static String byteBuf2String0(Charset charset, ByteBuf... bufs) {
    if (bufs.length == 1) {
        return decodeString(charset, bufs[0]);
    }//  w  w  w.j  a va 2s. c o m
    ByteBuf composite = composite(bufs);
    try {
        return decodeString(charset, composite);
    } finally {
        composite.release();
    }
}

From source file:io.gatling.netty.util.ahc.ByteBufUtils.java

License:Apache License

static char[] byteBuf2Chars0(Charset charset, ByteBuf... bufs) {
    if (bufs.length == 1) {
        return decodeChars(charset, bufs[0]);
    }//ww w .jav a 2 s  . com
    ByteBuf composite = composite(bufs);
    try {
        return decodeChars(charset, composite);
    } finally {
        composite.release();
    }
}

From source file:io.gatling.netty.util.ahc.ByteBufUtils.java

License:Apache License

private static CharBuffer decode(ByteBuf src, Charset charset) {
    int readerIndex = src.readerIndex();
    int len = src.readableBytes();
    final CharsetDecoder decoder = CharsetUtil.decoder(charset);
    CharBuffer dst = pooledCharBuffer(len, decoder);
    if (src.nioBufferCount() == 1) {
        // Use internalNioBuffer(...) to reduce object creation.
        // BEWARE: NOT THREAD-SAFE
        decode(decoder, src.internalNioBuffer(readerIndex, len), dst);
    } else {/*from w ww . jav a  2 s.c o m*/
        // We use a heap buffer as CharsetDecoder is most likely able to use a fast-path if src and dst buffers
        // are both backed by a byte array.
        ByteBuf buffer = src.alloc().heapBuffer(len);
        try {
            buffer.writeBytes(src, readerIndex, len);
            // Use internalNioBuffer(...) to reduce object creation.
            decode(decoder, buffer.internalNioBuffer(buffer.readerIndex(), len), dst);
        } finally {
            // Release the temporary buffer again.
            buffer.release();
        }
    }
    dst.flip();
    return dst;
}