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:com.github.spapageo.jannel.channel.ChannelBufferUtils.java

License:Open Source License

/**
 * Reads a data segment from the byte buffer
 * @param byteBuffer the bytes to read from
 * @return the data segment/*from   w ww  . j  a va2  s . c o  m*/
 */
@Nonnull
public static ByteBuf readOctetStringToBytes(ByteBuf byteBuffer) {
    if (byteBuffer.readableBytes() < 4)
        throw new NotEnoughDataDecoderException("Not enough bytes to read the octet string size");

    int dataSize = byteBuffer.readInt();

    if (dataSize == -1)
        return Unpooled.EMPTY_BUFFER;

    checkOctetStringSize(byteBuffer, dataSize);

    return byteBuffer.readBytes(dataSize);
}

From source file:com.gxkj.demo.netty.proxy.HexDumpProxyBackendHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ctx.read();
    ctx.write(Unpooled.EMPTY_BUFFER);
}

From source file:com.gxkj.demo.netty.socksproxy.RelayHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ctx.writeAndFlush(Unpooled.EMPTY_BUFFER);
}

From source file:com.gxkj.demo.netty.socksproxy.SocksServerUtils.java

License:Apache License

/**
 * Closes the specified channel after all queued write requests are flushed.
 *//*from ww w .  j  a v a  2s .co m*/
public static void closeOnFlush(Channel ch) {
    /**
     * ?
     */
    if (ch.isActive()) {
        ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.intuit.karate.netty.FeatureServerHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) {
    long startTime = System.currentTimeMillis();
    backend.getContext().logger.debug("handling method: {}, uri: {}", msg.method(), msg.uri());
    FullHttpResponse nettyResponse;/*from w  w  w  . ja  v  a2  s  .c  o  m*/
    if (msg.uri().startsWith(STOP_URI)) {
        backend.getContext().logger.info("stop uri invoked, shutting down");
        ByteBuf responseBuf = Unpooled.copiedBuffer("stopped", CharsetUtil.UTF_8);
        nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, responseBuf);
        stopFunction.run();
    } else {
        StringUtils.Pair url = HttpUtils.parseUriIntoUrlBaseAndPath(msg.uri());
        HttpRequest request = new HttpRequest();
        if (url.left == null) {
            String requestScheme = ssl ? "https" : "http";
            String host = msg.headers().get(HttpUtils.HEADER_HOST);
            request.setUrlBase(requestScheme + "://" + host);
        } else {
            request.setUrlBase(url.left);
        }
        request.setUri(url.right);
        request.setMethod(msg.method().name());
        msg.headers().forEach(h -> request.addHeader(h.getKey(), h.getValue()));
        QueryStringDecoder decoder = new QueryStringDecoder(url.right);
        decoder.parameters().forEach((k, v) -> request.putParam(k, v));
        HttpContent httpContent = (HttpContent) msg;
        ByteBuf content = httpContent.content();
        if (content.isReadable()) {
            byte[] bytes = new byte[content.readableBytes()];
            content.readBytes(bytes);
            request.setBody(bytes);
        }
        HttpResponse response = backend.buildResponse(request, startTime);
        HttpResponseStatus httpResponseStatus = HttpResponseStatus.valueOf(response.getStatus());
        byte[] responseBody = response.getBody();
        if (responseBody != null) {
            ByteBuf responseBuf = Unpooled.copiedBuffer(responseBody);
            nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, httpResponseStatus, responseBuf);
        } else {
            nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, httpResponseStatus);
        }
        MultiValuedMap karateHeaders = response.getHeaders();
        if (karateHeaders != null) {
            HttpHeaders nettyHeaders = nettyResponse.headers();
            karateHeaders.forEach((k, v) -> nettyHeaders.add(k, v));
        }
    }
    ctx.write(nettyResponse);
    ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroupTest.java

License:Apache License

private static DnsRecord newBadAddressRecord(String name, boolean ipV4) {
    return new DefaultDnsRawRecord(name, ipV4 ? A : AAAA, 60, Unpooled.EMPTY_BUFFER);
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsTextEndpointGroupTest.java

License:Apache License

private static DnsRecord newTooShortTxtRecord(String hostname) {
    return new DefaultDnsRawRecord(hostname, TXT, 60, Unpooled.EMPTY_BUFFER);
}

From source file:com.linecorp.armeria.internal.http.Http1ObjectEncoder.java

License:Apache License

private HttpObject convertServerHeaders(int streamId, HttpHeaders headers, boolean endStream)
        throws Http2Exception {

    // Leading headers will always have :status, trailers will never have it.
    final HttpStatus status = headers.status();
    if (status == null) {
        return convertTrailingHeaders(streamId, headers);
    }/*from   w  w w .ja  v  a2  s.c  om*/

    // Convert leading headers.
    final HttpResponse res;
    final boolean informational = status.codeClass() == HttpStatusClass.INFORMATIONAL;

    if (endStream || informational) {

        res = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status.toNettyStatus(), Unpooled.EMPTY_BUFFER,
                false);

        final io.netty.handler.codec.http.HttpHeaders outHeaders = res.headers();
        convert(streamId, headers, outHeaders, false);

        if (informational) {
            // 1xx responses does not have the 'content-length' header.
            outHeaders.remove(HttpHeaderNames.CONTENT_LENGTH);
        } else if (!headers.contains(HttpHeaderNames.CONTENT_LENGTH)) {
            // NB: Set the 'content-length' only when not set rather than always setting to 0.
            //     It's because a response to a HEAD request can have empty content while having
            //     non-zero 'content-length' header.
            //     However, this also opens the possibility of sending a non-zero 'content-length'
            //     header even when it really has to be zero. e.g. a response to a non-HEAD request
            outHeaders.setInt(HttpHeaderNames.CONTENT_LENGTH, 0);
        }
    } else {
        res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status.toNettyStatus(), false);
        // Perform conversion.
        convert(streamId, headers, res.headers(), false);
        setTransferEncoding(res);
    }

    return res;
}

From source file:com.linecorp.armeria.internal.http.Http1ObjectEncoder.java

License:Apache License

private LastHttpContent convertTrailingHeaders(int streamId, HttpHeaders headers) throws Http2Exception {
    final LastHttpContent lastContent;
    if (headers.isEmpty()) {
        lastContent = LastHttpContent.EMPTY_LAST_CONTENT;
    } else {/*from   w ww  .ja v a2  s. c  o  m*/
        lastContent = new DefaultLastHttpContent(Unpooled.EMPTY_BUFFER, false);
        convert(streamId, headers, lastContent.trailingHeaders(), true);
    }
    return lastContent;
}

From source file:com.linecorp.armeria.internal.http.Http1ObjectEncoder.java

License:Apache License

@Override
protected ChannelFuture doWriteReset(ChannelHandlerContext ctx, int id, int streamId, Http2Error error) {
    // NB: this.minClosedId can be overwritten more than once when 3+ pipelined requests are received
    //     and they are handled by different threads simultaneously.
    //     e.g. when the 3rd request triggers a reset and then the 2nd one triggers another.
    minClosedId = Math.min(minClosedId, id);
    for (int i = minClosedId; i <= maxIdWithPendingWrites; i++) {
        final PendingWrites pendingWrites = this.pendingWrites.remove(i);
        for (;;) {
            final Entry<HttpObject, ChannelPromise> e = pendingWrites.poll();
            if (e == null) {
                break;
            }//w ww .  j  a  v a  2s.c  om
            e.getValue().tryFailure(ClosedSessionException.get());
        }
    }

    final ChannelFuture f = ctx.write(Unpooled.EMPTY_BUFFER);
    if (currentId >= minClosedId) {
        f.addListener(ChannelFutureListener.CLOSE);
    }

    return f;
}