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:org.apache.qpid.jms.message.facade.test.JmsTestBytesMessageFacade.java

License:Apache License

@Override
public void clearBody() {
    if (bytesIn != null) {
        try {/*  w  w w.  j  a  v  a2 s .c o  m*/
            bytesIn.close();
        } catch (IOException e) {
        }
        bytesIn = null;
    }
    if (bytesOut != null) {
        try {
            bytesOut.close();
        } catch (IOException e) {
        }
        bytesOut = null;
    }

    content = Unpooled.EMPTY_BUFFER;
}

From source file:org.apache.qpid.jms.message.facade.test.JmsTestBytesMessageFacade.java

License:Apache License

@Override
public OutputStream getOutputStream() throws JMSException {
    if (bytesIn != null) {
        throw new IllegalStateException("Body is being read from, cannot perform a write.");
    }/*from w  w w . ja  v  a2s  . c om*/

    if (bytesOut == null) {
        bytesOut = new ByteBufOutputStream(Unpooled.buffer());
        content = Unpooled.EMPTY_BUFFER;
    }

    return bytesOut;
}

From source file:org.apache.qpid.jms.provider.amqp.message.AmqpJmsBytesMessageFacadeTest.java

License:Apache License

private OutputStream substituteMockOutputStream(AmqpJmsBytesMessageFacade bytesMessage)
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    ByteBufOutputStream mock = Mockito.mock(ByteBufOutputStream.class);
    Mockito.when(mock.buffer()).thenReturn(Unpooled.EMPTY_BUFFER);

    Field oshField = bytesMessage.getClass().getDeclaredField("bytesOut");
    oshField.setAccessible(true);// ww w.  ja  va2 s .  co  m
    oshField.set(bytesMessage, mock);

    return mock;
}

From source file:org.apache.zookeeper.server.NettyServerCnxn.java

License:Apache License

@Override
public void close() {
    closingChannel = true;//from ww  w . jav a 2s  .  com

    if (LOG.isDebugEnabled()) {
        LOG.debug("close called for sessionid:0x{}", Long.toHexString(sessionId));
    }
    setStale();

    // ZOOKEEPER-2743:
    // Always unregister connection upon close to prevent
    // connection bean leak under certain race conditions.
    factory.unregisterConnection(this);

    // if this is not in cnxns then it's already closed
    if (!factory.cnxns.remove(this)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("cnxns size:{}", factory.cnxns.size());
        }
        return;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("close in progress for sessionid:0x{}", Long.toHexString(sessionId));
    }

    factory.removeCnxnFromSessionMap(this);

    factory.removeCnxnFromIpMap(this, ((InetSocketAddress) channel.remoteAddress()).getAddress());

    if (zkServer != null) {
        zkServer.removeCnxn(this);
    }

    if (channel.isOpen()) {
        // Since we don't check on the futures created by write calls to the channel complete we need to make sure
        // that all writes have been completed before closing the channel or we risk data loss
        // See: http://lists.jboss.org/pipermail/netty-users/2009-August/001122.html
        channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) {
                future.channel().close().addListener(f -> releaseQueuedBuffer());
            }
        });
    } else {
        channel.eventLoop().execute(this::releaseQueuedBuffer);
    }
}

From source file:org.asynchttpclient.request.body.ChunkingTest.java

License:Open Source License

private void feed(FeedableBodyGenerator feedableBodyGenerator, InputStream is) throws Exception {
    try (InputStream inputStream = is) {
        byte[] buffer = new byte[512];
        for (int i; (i = inputStream.read(buffer)) > -1;) {
            byte[] chunk = new byte[i];
            System.arraycopy(buffer, 0, chunk, 0, i);
            feedableBodyGenerator.feed(Unpooled.wrappedBuffer(chunk), false);
        }//from   w ww .  java 2  s  .  c  o m
    }
    feedableBodyGenerator.feed(Unpooled.EMPTY_BUFFER, true);

}

From source file:org.atmosphere.nettosphere.ChunkedWriter.java

License:Apache License

private ByteBuf writeHeaders(AtmosphereResponse response) throws UnsupportedEncodingException {
    if (writeHeader && !headerWritten.getAndSet(true) && response != null) {
        return Unpooled.wrappedBuffer(
                Unpooled.wrappedBuffer(constructStatusAndHeaders(response, -1).getBytes("UTF-8")));
    }//from  w ww  . ja  va2  s  .co m
    return Unpooled.EMPTY_BUFFER;
}

From source file:org.atmosphere.nettosphere.ChunkedWriter.java

License:Apache License

private ByteBuf writeHeadersForHttp(AtmosphereResponse response) throws UnsupportedEncodingException {
    if (writeHeader && !headerWritten.getAndSet(true) && response != null) {
        return Unpooled.wrappedBuffer(constructStatusAndHeaders(response, -1).getBytes("UTF-8"));
    }//from  w w  w  .j  ava  2  s .  c o m
    return Unpooled.EMPTY_BUFFER;
}

From source file:org.atmosphere.nettosphere.StreamWriter.java

License:Apache License

private void setUpBuffers() {
    if (chainedBodyBuffer == null) {
        chainedBodyBuffer = Unpooled.EMPTY_BUFFER;
    }
}

From source file:org.atmosphere.nettosphere.StreamWriter.java

License:Apache License

@Override
public void close(AtmosphereResponse response) throws IOException {
    // Once we initiate the flush, we discard anything coming after for whatever reason.
    if (!doneProcessing.getAndSet(true) && channel.isOpen()) {
        logger.trace("About to flush to {} for {}", channel, response.uuid());

        ByteBuf statusAndHeadersBuffer = writeHeader ? Unpooled.wrappedBuffer(
                constructStatusAndHeaders(response, chainedBodyBuffer.readableBytes()).getBytes("UTF-8"))
                : Unpooled.EMPTY_BUFFER;
        ByteBuf drain = Unpooled.wrappedBuffer(statusAndHeadersBuffer, chainedBodyBuffer);
        channel.writeAndFlush(drain).addListener(new ChannelFutureListener() {
            @Override//  w  w w  . java  2  s.co m
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                chainedBodyBuffer = null;
                if (!keepAlive) {
                    channel.close().awaitUninterruptibly();
                }
            }
        });
    } else {
        throw Utils.ioExceptionForChannel(channel, response.uuid());
    }
}

From source file:org.dcache.xrootd.stream.ChunkedResponseWriteHandler.java

License:Open Source License

private boolean doFlush(final ChannelHandlerContext ctx) throws Exception {
    final Channel channel = ctx.channel();
    if (!channel.isActive()) {
        discard(null);//from   w w  w. j  a v a 2  s . co m
        return false;
    }
    boolean flushed = false;
    while (channel.isWritable()) {
        if (currentWrite == null) {
            currentWrite = queue.poll();
        }

        if (currentWrite == null) {
            break;
        }
        final PendingWrite currentWrite = this.currentWrite;
        final ChunkedResponse pendingMessage = currentWrite.msg;

        boolean endOfInput;
        Object message = null;
        try {
            message = pendingMessage.nextChunk(ctx.alloc());
            endOfInput = pendingMessage.isEndOfInput();
        } catch (final Throwable t) {
            this.currentWrite = null;

            if (message != null) {
                ReferenceCountUtil.release(message);
            }

            currentWrite.fail(t);
            break;
        }

        if (message == null) {
            // If message is null write an empty ByteBuf.
            // See https://github.com/netty/netty/issues/1671
            message = Unpooled.EMPTY_BUFFER;
        }

        final int amount = amount(message);
        ChannelFuture f = ctx.write(message);
        if (endOfInput) {
            this.currentWrite = null;

            // Register a listener which will close the input once the write is complete.
            // This is needed because the Chunk may have some resource bound that can not
            // be closed before its not written.
            //
            // See https://github.com/netty/netty/issues/303
            f.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        currentWrite.fail(future.cause());
                    } else {
                        currentWrite.progress(amount);
                        currentWrite.success();
                    }
                }
            });
        } else {
            f.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        currentWrite.fail(future.cause());
                    } else {
                        currentWrite.progress(amount);
                    }
                }
            });
        }

        // Always need to flush
        ctx.flush();
        flushed = true;

        if (!channel.isActive()) {
            discard(new ClosedChannelException());
            break;
        }
    }
    return flushed;

}