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.springframework.messaging.rsocket.LeakAwareNettyDataBufferFactory.java

License:Apache License

@Override
public NettyDataBuffer wrap(ByteBuf byteBuf) {
    NettyDataBuffer dataBuffer = super.wrap(byteBuf);
    if (byteBuf != Unpooled.EMPTY_BUFFER) {
        recordHint(dataBuffer);//ww  w  .  java 2s.  c o m
    }
    return dataBuffer;
}

From source file:org.teiid.transport.ObjectEncoder.java

License:Apache License

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    ByteBuf out = allocateBuffer(ctx, this.estimatedLength, this.preferDirect);
    int startIdx = out.writerIndex();
    ByteBufOutputStream bout = new ByteBufOutputStream(out);
    bout.write(LENGTH_PLACEHOLDER);/*from  w  ww.j  a v a2s. c  o m*/
    final CompactObjectOutputStream oout = new CompactObjectOutputStream(bout);
    try {
        oout.writeObject(msg);
        ExternalizeUtil.writeCollection(oout, oout.getReferences());
        oout.flush();
        oout.close();

        int endIdx = out.writerIndex();
        out.setInt(startIdx, endIdx - startIdx - 4);

        if (out.isReadable()) {
            ctx.write(out, promise);
            for (InputStream is : oout.getStreams()) {
                ctx.write(new AnonymousChunkedStream(new BufferedInputStream(is, CHUNK_SIZE)), promise);
            }
        } else {
            out.release();
            ctx.write(Unpooled.EMPTY_BUFFER, promise);
        }
        ctx.flush();
        out = null;
    } catch (Throwable t) {
        throw new FailedWriteException(msg, t);
    } finally {
        if (out != null) {
            out.release();
        }
    }
}

From source file:org.vertx.java.core.http.impl.AssembledFullHttpResponse.java

License:Open Source License

private static LastHttpContent toLastContent(ByteBuf buf, HttpHeaders trailingHeaders) {
    if (buf.isReadable()) {
        if (trailingHeaders == null) {
            return new DefaultLastHttpContent(buf);
        } else {/*from   ww w.  ja  va  2s .  c  o  m*/
            return new AssembledLastHttpContent(buf, trailingHeaders);
        }
    } else {
        if (trailingHeaders == null) {
            return LastHttpContent.EMPTY_LAST_CONTENT;
        } else {
            return new AssembledLastHttpContent(Unpooled.EMPTY_BUFFER, trailingHeaders);
        }
    }
}

From source file:org.vertx.java.core.http.impl.DefaultHttpClientRequest.java

License:Open Source License

private void connected(ClientConnection conn) {
    conn.setCurrentRequest(this);
    this.conn = conn;

    // If anything was written or the request ended before we got the connection, then
    // we need to write it now

    if (pendingMaxSize != -1) {
        conn.doSetWriteQueueMaxSize(pendingMaxSize);
    }/* www . j  a va 2 s . com*/

    if (pendingChunks != null) {
        ByteBuf pending = pendingChunks;
        pendingChunks = null;

        if (completed) {
            // we also need to write the head so optimize this and write all out in once
            writeHeadWithContent(pending, true);

            conn.endRequest();
        } else {
            writeHeadWithContent(pending, false);
        }
    } else {
        if (completed) {
            // we also need to write the head so optimize this and write all out in once
            writeHeadWithContent(Unpooled.EMPTY_BUFFER, true);
            conn.endRequest();
        } else {
            if (writeHead) {
                writeHead();
            }
        }
    }
}

From source file:org.vertx.java.core.http.impl.VertxHttpHandler.java

License:Open Source License

@Override
protected Object safeObject(Object msg, ByteBufAllocator allocator) throws Exception {
    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        ByteBuf buf = content.content();
        if (buf != Unpooled.EMPTY_BUFFER && buf.isDirect()) {
            ByteBuf newBuf = safeBuffer(content, allocator);
            if (msg instanceof LastHttpContent) {
                LastHttpContent last = (LastHttpContent) msg;
                return new AssembledLastHttpContent(newBuf, last.trailingHeaders());
            } else {
                return new DefaultHttpContent(newBuf);
            }//from  w  ww. ja  v a2  s  . co  m
        }
    } else if (msg instanceof WebSocketFrame) {
        ByteBuf payload = safeBuffer((WebSocketFrame) msg, allocator);
        boolean isFinal = ((WebSocketFrame) msg).isFinalFragment();
        org.vertx.java.core.http.WebSocketFrame.FrameType frameType;
        if (msg instanceof BinaryWebSocketFrame) {
            frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.BINARY;
        } else if (msg instanceof CloseWebSocketFrame) {
            frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.CLOSE;
        } else if (msg instanceof PingWebSocketFrame) {
            frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.PING;
        } else if (msg instanceof PongWebSocketFrame) {
            frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.PONG;
        } else if (msg instanceof TextWebSocketFrame) {
            frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.TEXT;
        } else if (msg instanceof ContinuationWebSocketFrame) {
            frameType = org.vertx.java.core.http.WebSocketFrame.FrameType.CONTINUATION;
        } else {
            throw new IllegalStateException("Unsupported websocket msg " + msg);
        }
        return new DefaultWebSocketFrame(frameType, payload, isFinal);
    }
    return msg;
}

From source file:org.waarp.gateway.kernel.commonfile.CommonFileChunkedInput.java

License:Open Source License

@Override
public ByteBuf readChunk(ByteBufAllocator arg0) throws Exception {
    // Document// ww  w. j  av a2  s  . c o  m
    DataBlock block;
    try {
        block = this.document.readDataBlock();
    } catch (FileEndOfTransferException e) {
        lastChunkAlready = true;
        return Unpooled.EMPTY_BUFFER;
    } catch (FileTransferException e) {
        throw new HttpIncorrectRetrieveException(e);
    }
    lastChunkAlready = block.isEOF();
    offset += block.getByteCount();
    return block.getBlock();
}

From source file:org.waarp.gateway.kernel.rest.RestArgument.java

License:Open Source License

/**
 * Set values according to the request URI
 * /*w  w w  . jav  a2  s.c om*/
 * @param request
 */
public void setRequest(HttpRequest request) {
    arguments.put(REST_ROOT_FIELD.ARG_HASBODY.field, (request instanceof FullHttpRequest
            && ((FullHttpRequest) request).content() != Unpooled.EMPTY_BUFFER));
    arguments.put(REST_ROOT_FIELD.ARG_METHOD.field, request.method().name());
    QueryStringDecoder decoderQuery = new QueryStringDecoder(request.uri());
    String path = decoderQuery.path();
    arguments.put(REST_ROOT_FIELD.ARG_PATH.field, path);
    // compute path main uri
    String basepath = path;
    int pos = basepath.indexOf('/');
    if (pos >= 0) {
        if (pos == 0) {
            int pos2 = basepath.indexOf('/', 1);
            if (pos2 < 0) {
                basepath = basepath.substring(1);
            } else {
                basepath = basepath.substring(1, pos2);
            }
        } else {
            basepath = basepath.substring(0, pos);
        }
    }
    arguments.put(REST_ROOT_FIELD.ARG_BASEPATH.field, basepath);
    // compute sub path args
    if (pos == 0) {
        pos = path.indexOf('/', 1);
    }
    if (pos >= 0) {
        int pos2 = path.indexOf('/', pos + 1);
        if (pos2 > 0) {
            ArrayNode array = arguments.putArray(REST_ROOT_FIELD.ARGS_SUBPATH.field);
            while (pos2 > 0) {
                array.add(path.substring(pos + 1, pos2));
                pos = pos2;
                pos2 = path.indexOf('/', pos + 1);
            }
        }
        pos2 = path.indexOf('?', pos + 1);
        if (pos2 > 0 && pos2 > pos + 1) {
            ArrayNode array = (ArrayNode) arguments.get(REST_ROOT_FIELD.ARGS_SUBPATH.field);
            if (array == null) {
                array = arguments.putArray(REST_ROOT_FIELD.ARGS_SUBPATH.field);
            }
            array.add(path.substring(pos + 1, pos2));
        } else {
            String last = path.substring(pos + 1);
            if (!last.isEmpty()) {
                ArrayNode array = (ArrayNode) arguments.get(REST_ROOT_FIELD.ARGS_SUBPATH.field);
                if (array == null) {
                    array = arguments.putArray(REST_ROOT_FIELD.ARGS_SUBPATH.field);
                }
                array.add(last);
            }
        }
    }
    Map<String, List<String>> map = decoderQuery.parameters();
    ObjectNode node = arguments.putObject(REST_GROUP.ARGS_URI.group);
    for (String key : map.keySet()) {
        if (key.equalsIgnoreCase(REST_ROOT_FIELD.ARG_X_AUTH_KEY.field)) {
            arguments.put(REST_ROOT_FIELD.ARG_X_AUTH_KEY.field, map.get(key).get(0));
            continue;
        }
        if (key.equalsIgnoreCase(REST_ROOT_FIELD.ARG_X_AUTH_USER.field)) {
            arguments.put(REST_ROOT_FIELD.ARG_X_AUTH_USER.field, map.get(key).get(0));
            continue;
        }
        if (key.equalsIgnoreCase(REST_ROOT_FIELD.ARG_X_AUTH_TIMESTAMP.field)) {
            arguments.put(REST_ROOT_FIELD.ARG_X_AUTH_TIMESTAMP.field, map.get(key).get(0));
            continue;
        }
        List<String> list = map.get(key);
        if (list.size() > 1) {
            ArrayNode array = node.putArray(key);
            for (String val : map.get(key)) {
                array.add(val);
            }
        } else if (list.isEmpty()) {
            node.putNull(key);
        } else {
            //1
            node.put(key, list.get(0));
        }
    }
}

From source file:org.waarp.openr66.protocol.localhandler.packet.AbstractLocalPacket.java

License:Open Source License

/**
 * @param lcr/*  w ww. ja  va 2  s .com*/
 *            the LocalChannelReference in use
 * @return the ByteBuf as LocalPacket
 * @throws OpenR66ProtocolPacketException
 */
public ByteBuf getLocalPacket(LocalChannelReference lcr) throws OpenR66ProtocolPacketException {
    final ByteBuf buf = Unpooled.buffer(4 * 3 + 1);// 3 header
    // lengths+type
    if (header == null) {
        createHeader(lcr);
    }
    final ByteBuf newHeader = header != null ? header : Unpooled.EMPTY_BUFFER;
    final int headerLength = 4 * 2 + 1 + newHeader.readableBytes();
    if (middle == null) {
        createMiddle(lcr);
    }
    final ByteBuf newMiddle = middle != null ? middle : Unpooled.EMPTY_BUFFER;
    final int middleLength = newMiddle.readableBytes();
    if (end == null) {
        createEnd(lcr);
    }
    final ByteBuf newEnd = end != null ? end : Unpooled.EMPTY_BUFFER;
    final int endLength = newEnd.readableBytes();
    buf.writeInt(headerLength);
    buf.writeInt(middleLength);
    buf.writeInt(endLength);
    buf.writeByte(getType());
    final ByteBuf ByteBuf = Unpooled.wrappedBuffer(buf, newHeader, newMiddle, newEnd);
    return ByteBuf;
}

From source file:org.waarp.openr66.protocol.localhandler.packet.BlockRequestPacket.java

License:Open Source License

@Override
public void createEnd(LocalChannelReference lcr) throws OpenR66ProtocolPacketException {
    end = Unpooled.EMPTY_BUFFER;
}

From source file:org.waarp.openr66.protocol.localhandler.packet.BlockRequestPacket.java

License:Open Source License

@Override
public void createMiddle(LocalChannelReference lcr) throws OpenR66ProtocolPacketException {
    middle = Unpooled.EMPTY_BUFFER;
}