Example usage for io.netty.buffer Unpooled EMPTY_BUFFER

List of usage examples for io.netty.buffer Unpooled EMPTY_BUFFER

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled EMPTY_BUFFER.

Prototype

ByteBuf EMPTY_BUFFER

To view the source code for io.netty.buffer Unpooled EMPTY_BUFFER.

Click Source Link

Document

A buffer whose capacity is 0 .

Usage

From source file:de.jackwhite20.apex.util.ChannelUtil.java

License:Open Source License

public static void closeOnFlush(Channel ch) {

    if (ch != null && ch.isActive()) {
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }//w  ww  .  j av  a 2s.c o  m
}

From source file:de.jackwhite20.comix.handler.DownstreamHandler.java

License:Open Source License

@Override
public void channelInactive(ChannelHandlerContext ctx) {
    if (upstreamChannel != null) {
        if (upstreamChannel.isActive()) {
            upstreamChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
        }//from   w w  w .  j av  a2s.  c o  m

        upstreamBytesOut = 0;
        downstreamBytesIn = 0;
    }
}

From source file:de.jackwhite20.comix.handler.DownstreamHandler.java

License:Open Source License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    Channel ch = ctx.channel();/*from  w w w  .  ja va  2 s  .  c  om*/
    if (ch.isActive()) {
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:de.jackwhite20.comix.handler.HandshakeHandler.java

License:Open Source License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    Channel ch = ctx.channel();/* w  w w  .  j  a va  2  s  .c  om*/
    if (ch.isActive()) {
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:de.jackwhite20.comix.handler.UpstreamHandler.java

License:Open Source License

@Override
public void channelInactive(ChannelHandlerContext ctx) {
    if (downstreamChannel != null) {
        if (downstreamChannel.isActive()) {
            downstreamChannel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
        }//from   ww w  . ja va2  s.  c om

        if (client != null)
            Comix.getInstance().removeClient(client);

        upstreamBytesIn = 0;
        downstreamBytesOut = 0;

        Comix.getLogger()
                .info("["
                        + ((client != null) ? client.getName()
                                : Util.formatSocketAddress(upstreamChannel.remoteAddress()))
                        + "] -> UpstreamHandler has disconnected");
    }
}

From source file:divconq.api.internal.UploadStream.java

License:Open Source License

@Override
public ByteBuf getChunk(int length) throws IOException {
    if (this.in == null || length == 0)
        return Unpooled.EMPTY_BUFFER;

    // indicate that we are keeping busy and not hung
    this.ops.touch();

    //System.out.println("Get last activity after touch: " + this.ops.getLastActivity());

    int read = 0;

    ByteBuffer byteBuffer = ByteBuffer.allocate(length);

    while (read < length) {
        int readnow = this.in.read(byteBuffer);

        if (readnow == -1) {
            this.in.close();
            this.in = null;
            break;
        } else {/*from  w  ww  . ja v a2 s.c  o m*/
            read += readnow;
        }
    }

    if (read == 0)
        return Unpooled.EMPTY_BUFFER;

    byteBuffer.flip();
    ByteBuf buffer = Unpooled.wrappedBuffer(byteBuffer);
    buffer.readerIndex(0);
    buffer.writerIndex(read);
    return buffer;
}

From source file:divconq.net.ByteToMessageDecoder.java

License:Apache License

protected ByteBuf internalBuffer() {
    if (this.cumulation != null) {
        return this.cumulation;
    } else {/*  w  ww .  ja  v a  2  s  .c o  m*/
        return Unpooled.EMPTY_BUFFER;
    }
}

From source file:divconq.net.ByteToMessageDecoder.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    RecyclableArrayList out = RecyclableArrayList.newInstance();
    try {//from  ww w.  j av  a 2 s.  c  om
        if (this.cumulation != null) {
            callDecode(ctx, this.cumulation, out);
            decodeLast(ctx, this.cumulation, out);
        } else {
            decodeLast(ctx, Unpooled.EMPTY_BUFFER, out);
        }
    } catch (DecoderException e) {
        throw e;
    } catch (Exception e) {
        throw new DecoderException(e);
    } finally {
        try {
            if (this.cumulation != null) {
                this.cumulation.release();
                this.cumulation = null;
            }
            int size = out.size();
            for (int i = 0; i < size; i++) {
                ctx.fireChannelRead(out.get(i));
            }
            if (size > 0) {
                // Something was read, call fireChannelReadComplete()
                ctx.fireChannelReadComplete();
            }
            ctx.fireChannelInactive();
        } finally {
            // recycle in all cases
            out.recycle();
        }
    }
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

public ChannelFuture close(final ChannelPromise future) {
    final ChannelHandlerContext ctx = this.ctx;
    ctx.executor().execute(new Runnable() {
        @Override// w  w  w.java  2 s  . co  m
        public void run() {
            engine.closeOutbound();
            try {
                write(ctx, Unpooled.EMPTY_BUFFER, future);
                flush(ctx);
            } catch (Exception e) {
                if (!future.tryFailure(e)) {
                    logger.warn("flush() raised a masked exception.", e);
                }
            }
        }
    });

    return future;
}

From source file:divconq.net.ssl.SslHandler.java

License:Apache License

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
    // Do not encrypt the first write request if this handler is
    // created with startTLS flag turned on.
    if (startTls && !sentFirstMessage) {
        sentFirstMessage = true;//w  w w.  ja  v a  2  s.c  o  m
        pendingUnencryptedWrites.removeAndWriteAll();
        ctx.flush();
        return;
    }
    if (pendingUnencryptedWrites.isEmpty()) {
        pendingUnencryptedWrites.add(Unpooled.EMPTY_BUFFER, ctx.voidPromise());
    }
    if (!handshakePromise.isDone()) {
        flushedBeforeHandshakeDone = true;
    }
    wrap(ctx, false);
    ctx.flush();
}