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:chapter10.WebSocketServerHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // /*  w w  w. ja va 2 s  .c  o  m*/
    if (res.status().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);
        buf.release();
        setContentLength(res, res.content().readableBytes());
    }

    // ?Keep-Alive
    ChannelFuture f = ctx.channel().writeAndFlush(res);
    if (!isKeepAlive(req) || res.status().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:cn.dennishucd.nettyhttpserver.HttpServerHandler.java

License:Apache License

@SuppressWarnings("deprecation")
@Override// ww  w .  ja va 2 s.  co  m
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    if (msg instanceof HttpRequest) {
        request = (HttpRequest) msg;

        if (HttpUtil.is100ContinueExpected(request)) {
            ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }

        if (!request.uri().equalsIgnoreCase(URL)) {
            sendError(ctx, NOT_FOUND);

            return;
        }
    }

    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        ByteBuf buf = content.content();
        UserToken ut = CTools.JSONStr2Object(buf.toString(io.netty.util.CharsetUtil.UTF_8), UserToken.class);

        logger.info("request body: " + buf.toString(io.netty.util.CharsetUtil.UTF_8));
        buf.release();

        boolean keepAlive = HttpUtil.isKeepAlive(request);
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK,
                Unpooled.wrappedBuffer(CONTENT.getBytes()));
        response.headers().set(CONTENT_TYPE, "application/json");
        response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());

        logger.info("response body: " + CONTENT);

        if (!keepAlive) {
            ctx.write(response).addListener(ChannelFutureListener.CLOSE);
        } else {
            response.headers().set(CONNECTION, KEEP_ALIVE);
            ctx.write(response);
        }

    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:code.google.nfs.rpc.netty.serialize.NettyProtocolDecoder.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    RecyclableArrayList out = RecyclableArrayList.newInstance();
    try {/*from   ww  w .  j a  v a  2  s .  com*/
        if (msg instanceof ByteBuf) {
            ByteBuf data = (ByteBuf) msg;
            if (cumulation == null) {
                cumulation = data;
                try {
                    callDecode(ctx, cumulation, out);
                } finally {
                    if (cumulation != null && !cumulation.isReadable()) {
                        cumulation.release();
                        cumulation = null;
                    }
                }
            } else {
                try {
                    if (cumulation.writerIndex() > cumulation.maxCapacity() - data.readableBytes()) {
                        ByteBuf oldCumulation = cumulation;
                        cumulation = ctx.alloc().buffer(oldCumulation.readableBytes() + data.readableBytes());
                        cumulation.writeBytes(oldCumulation);
                        oldCumulation.release();
                    }
                    cumulation.writeBytes(data);
                    callDecode(ctx, cumulation, out);
                } finally {
                    if (cumulation != null) {
                        if (!cumulation.isReadable()) {
                            cumulation.release();
                            cumulation = null;
                        } else {
                            cumulation.discardSomeReadBytes();
                        }
                    }
                    data.release();
                }
            }
        } else {
            out.add(msg);
        }
    } catch (DecoderException e) {
        throw e;
    } catch (Throwable t) {
        throw new DecoderException(t);
    } finally {
        if (out.isEmpty()) {
            decodeWasNull = true;
        }

        List<Object> results = new ArrayList<Object>();
        for (Object result : out) {
            results.add(result);
        }
        ctx.fireChannelRead(results);

        out.recycle();
    }
}

From source file:code.google.nfs.rpc.netty.serialize.NettyProtocolDecoder.java

License:Apache License

@Override
public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
    ByteBuf buf = internalBuffer();
    int readable = buf.readableBytes();
    if (buf.isReadable()) {
        ByteBuf bytes = buf.readBytes(readable);
        buf.release();
        ctx.fireChannelRead(bytes);//from  w  ww  .jav  a 2 s .  co m
    }
    cumulation = null;
    ctx.fireChannelReadComplete();
    handlerRemoved0(ctx);
}

From source file:com.addthis.hydra.data.tree.prop.DataCounting.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    preEncode();//from www . j  av a2 s  .  c om
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        Varint.writeUnsignedVarInt(ver, buffer);
        Varint.writeUnsignedVarInt(M.length, buffer);
        buffer.writeBytes(M);
        byte[] bytes = new byte[buffer.readableBytes()];
        buffer.readBytes(bytes);
        return bytes;
    } finally {
        buffer.release();
    }
}

From source file:com.addthis.hydra.data.tree.prop.DataCounting.java

License:Apache License

@Override
public void bytesDecode(byte[] b, long version) {
    ByteBuf buffer = Unpooled.wrappedBuffer(b);
    try {//from   www  . ja v a2 s .  c om
        ver = Varint.readUnsignedVarInt(buffer);
        M = new byte[Varint.readUnsignedVarInt(buffer)];
        buffer.readBytes(M);
    } finally {
        buffer.release();
    }
    postDecode();
}

From source file:com.addthis.hydra.data.tree.prop.DataKeyTop.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] bytes = null;
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
    try {/*  w  w w  .ja  v a  2  s . com*/
        byte[] topBytes = top.bytesEncode(version);
        Varint.writeUnsignedVarInt(topBytes.length, buf);
        buf.writeBytes(topBytes);
        Varint.writeUnsignedVarInt(size, buf);
        bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
    } finally {
        buf.release();
    }
    return bytes;
}

From source file:com.addthis.hydra.data.tree.prop.DataKeyTop.java

License:Apache License

@Override
public void bytesDecode(byte[] b, long version) {
    top = new ConcurrentKeyTopper();
    ByteBuf buf = Unpooled.wrappedBuffer(b);
    try {/*from   w  w  w.  ja v a2 s. c o m*/
        int topBytesLength = Varint.readUnsignedVarInt(buf);
        if (topBytesLength > 0) {
            byte[] topBytes = new byte[topBytesLength];
            buf.readBytes(topBytes);
            top.bytesDecode(topBytes, version);
        } else {
            top.init();
        }
        size = Varint.readUnsignedVarInt(buf);
    } finally {
        buf.release();
    }
}

From source file:com.addthis.hydra.data.tree.prop.DataMap.java

License:Apache License

@Override
public byte[] bytesEncode(long version) {
    byte[] encodedBytes = null;
    ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer();
    try {//  ww  w  . jav a2  s.  c o m
        synchronized (map) {
            preEncode();
            Varint.writeUnsignedVarInt(keys.length, buf);
            for (String key : keys) {
                writeString(buf, key);
            }
            for (String val : vals) {
                writeString(buf, val);
            }
            Varint.writeUnsignedVarInt(size, buf);
        }
        encodedBytes = new byte[buf.readableBytes()];
        buf.readBytes(encodedBytes);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    } finally {
        buf.release();
    }
    return encodedBytes;
}

From source file:com.addthis.hydra.data.tree.prop.DataMap.java

License:Apache License

@Override
public void bytesDecode(byte[] b, long version) {
    ByteBuf buf = Unpooled.wrappedBuffer(b);
    try {//from   w  w w .ja v  a2 s.c  o  m
        int length = Varint.readUnsignedVarInt(buf);
        keys = new String[length];
        vals = new String[length];
        try {
            for (int i = 0; i < length; i++) {
                keys[i] = readString(buf);
            }
            for (int i = 0; i < length; i++) {
                vals[i] = readString(buf);
            }
            if (buf.readableBytes() > 0) {
                size = Varint.readUnsignedVarInt(buf);
            } else {
                if (!IGNORE_DESERIALIZATION_ERROR) {
                    throw new RuntimeException("Tried to deserialize a corrupted DataMap attachment. "
                            + "set the system property hydra.tree.data.map=true to ignore (Map Attachment will be empty on old nodes)");
                }
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    } finally {
        buf.release();
    }
    postDecode();
}