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:com.linecorp.armeria.unsafe.grpc.GrpcUnsafeBufferUtil.java

License:Apache License

/**
 * Releases the {@link ByteBuf} backing the provided {@link Message}.
 *///w ww.j  a  v a  2s. com
public static void releaseBuffer(Object message, RequestContext ctx) {
    IdentityHashMap<Object, ByteBuf> buffers = ctx.attr(BUFFERS).get();
    checkState(buffers != null, "Releasing buffer even though storeBuffer has not been called.");
    ByteBuf removed = buffers.remove(message);
    if (removed == null) {
        throw new IllegalArgumentException("The provided message does not have a stored buffer.");
    }
    removed.release();
}

From source file:com.linkedin.pinot.transport.perf.ScatterGatherPerfClient.java

License:Apache License

private static void releaseByteBuf(CompositeFuture<ServerInstance, ByteBuf> future) throws Exception {
    Map<ServerInstance, ByteBuf> bMap = future.get();
    if (null != bMap) {
        for (Entry<ServerInstance, ByteBuf> bEntry : bMap.entrySet()) {
            ByteBuf b = bEntry.getValue();
            if (null != b) {
                b.release();
            }//from w w  w .  j  av  a  2  s.co m
        }
    }
}

From source file:com.lxz.talk.websocketx.server.WebSocketServerHandler.java

License:Apache License

private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
    // Generate an error page if response getStatus code is not OK (200).
    if (res.getStatus().code() != 200) {
        ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
        res.content().writeBytes(buf);/*from w  w w  .  j  a  va 2 s  .co  m*/
        buf.release();
        HttpHeaders.setContentLength(res, res.content().readableBytes());
    }

    // Send the response and close the connection if necessary.
    ChannelFuture f = ctx.channel().writeAndFlush(res);

    if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
        f.addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.magnet.yak.load.XMPPHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    System.out.println("CHANNEL READ CALLED");
    ByteBuf m = (ByteBuf) msg; // (1)
    try {//from   w  w  w  . j  a  va 2  s.co m
        long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L;
        System.out.println(new Date(currentTimeMillis));
        ctx.close();
    } finally {
        m.release();
    }
}

From source file:com.mastfrog.tinymavenproxy.FileFinder.java

License:Open Source License

public synchronized void put(final Path path, final ByteBuf content, final DateTime lastModified) {
    // This method is currently unused, but if we enhance the server to accept
    // uploads, we will likely need code a lot like this
    if (content.readableBytes() == 0) {
        return;/*from www .j a  va  2 s  . c o m*/
    }
    final ByteBuf buf = content.duplicate();
    threadPool.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            final File target = new File(config.dir, path.toString().replace('/', File.separatorChar));
            buf.retain();
            if (!target.exists()) {
                if (!target.getParentFile().exists()) {
                    if (!target.getParentFile().mkdirs()) {
                        throw new IOException("Could not create " + target.getParentFile());
                    }
                }
                if (!target.createNewFile()) {
                    throw new IOException("Could not create " + target);
                }
            }
            try (ByteBufInputStream in = new ByteBufInputStream(buf)) {
                try (OutputStream out = new BufferedOutputStream(new FileOutputStream(target))) {
                    Streams.copy(in, out, 1024);
                }
            } catch (IOException ioe) {
                if (target.exists()) {
                    target.delete();
                }
                throw ioe;
            } finally {
                buf.release();
            }
            threadPool.submit(new Runnable() {

                @Override
                public void run() {
                    if (lastModified != null) {
                        target.setLastModified(lastModified.getMillis());
                    }
                }
            });
            return null;
        }
    });
}

From source file:com.mikesilversides.mod1.ServerTest.EchoClientHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    //ctx.write(msg);
    ByteBuf in = (ByteBuf) msg;
    System.out.println("returned msg = " + in.toString(io.netty.util.CharsetUtil.UTF_8));
    in.release();
}

From source file:com.mikesilversides.mod1.ServerTest.StubClientHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    //ctx.write(msg);
    ByteBuf in = (ByteBuf) msg;
    final Byte inByte = in.readByte();
    System.out.println("received from stub1 byte = " + inByte);
    in.release();

    if (startSent) {
        if (inByte == 2) { // 2=ack
            System.out.println("received ack from stub1");
            startSent = false;//from  ww  w  .j  a va 2s .  c o m
        } else {
            System.out.println("unexpected reply received, closing connection");
            ctx.close();
            stubClientPlayer.addChatMessage(
                    new ChatComponentText("unexpected stub1 reply received, connection closed"));
        }
    } else {
        //stubClientPlayer.addChatMessage(new ChatComponentText("stub1 sent byte = "+inByte));

        // This code creates a new task which will be executed by the server during the next tick,
        //  for example see MinecraftServer.updateTimeLightAndEntities(), just under section
        //      this.theProfiler.startSection("jobs");
        //  In this case, the task is to call messageHandlerOnServer.processMessage(message, sendingPlayer)
        //final WorldServer playerWorldServer = sendingPlayer.getServerForPlayer();
        //playerWorldServer.addScheduledTask(new Runnable() {
        MinecraftServer.getServer().addScheduledTask(new Runnable() {
            public void run() {
                processMessage(inByte);
            }
        });
    }
}

From source file:com.minetats.mw.NamedPipe.java

License:Apache License

@Override
public ByteBuf readChunk(ByteBufAllocator bba) throws Exception {
    chunks++;//  w w  w  . j  ava 2  s .co m
    ByteBuf buf = bba.heapBuffer(chunkSize);
    boolean release = false;
    int read = 0;
    try {
        do {
            buf.writerIndex(buf.writerIndex() + read);
            read = file.read(buf.array(), buf.arrayOffset() + read, chunkSize - read);
        } while (read > 0);
        int index = buf.writerIndex() - 1;
        if (buf.getByte(index) == '\n' && buf.getByte(index - 1) == '\n') {
            endOfInput = true;
            System.out.println("endOfInput=" + endOfInput + ", read " + chunks + " chunks");
        }
        return buf;
    } finally {
        if (release) {
            buf.release();
        }
    }
}

From source file:com.mobius.software.android.iotbroker.mqtt.net.MQDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    ByteBuf nextHeader = null;
    do {//from   www .j  a v  a  2s .com
        if (buf.readableBytes() > 1)
            nextHeader = MQParser.next(buf);

        if (nextHeader != null) {
            buf.readBytes(nextHeader, nextHeader.capacity());
            try {
                MQMessage header = MQParser.decode(nextHeader);
                out.add(header);
            } catch (Exception e) {
                buf.resetReaderIndex();
                ctx.channel().pipeline().remove(this);
                throw e;
            } finally {
                nextHeader.release();
            }
        }
    } while (buf.readableBytes() > 1 && nextHeader != null);
}

From source file:com.mobius.software.mqtt.performance.controller.net.Decoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) {
    ByteBuf nextHeader = null;
    do {//from w w  w  . j av  a2 s . c o  m
        if (buf.readableBytes() > 1) {
            try {
                nextHeader = MQParser.next(buf);
            } catch (MalformedMessageException | IndexOutOfBoundsException ex) {
                buf.resetReaderIndex();
                if (nextHeader != null)
                    nextHeader = null;
            }
        }

        if (nextHeader != null) {
            buf.readBytes(nextHeader, nextHeader.capacity());
            try {
                MQMessage header = MQParser.decode(nextHeader);
                out.add(header);
            } catch (Exception e) {
                buf.resetReaderIndex();
                ctx.channel().pipeline().remove(this);
                throw e;
            } finally {
                nextHeader.release();
            }
        }
    } while (buf.readableBytes() > 1 && nextHeader != null);
}