Example usage for io.netty.buffer Unpooled compositeBuffer

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

Introduction

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

Prototype

public static CompositeByteBuf compositeBuffer() 

Source Link

Document

Returns a new big-endian composite buffer with no components.

Usage

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

public HTTPParser() {
    this.buf = Unpooled.compositeBuffer();
}

From source file:io.servicecomb.common.rest.AbstractRestInvocation.java

License:Apache License

@SuppressWarnings("deprecation")
protected void sendResponse(Response response) throws Exception {
    if (response.getHeaders().getHeaderMap() != null) {
        for (Entry<String, List<Object>> entry : response.getHeaders().getHeaderMap().entrySet()) {
            for (Object value : entry.getValue()) {
                if (!entry.getKey().equalsIgnoreCase(HttpHeaders.CONTENT_LENGTH)) {
                    responseEx.addHeader(entry.getKey(), String.valueOf(value));
                }//  ww w.ja v  a2 s .com
            }
        }
    }
    responseEx.setStatus(response.getStatusCode(), response.getReasonPhrase());
    responseEx.setContentType(produceProcessor.getName());

    Object body = response.getResult();
    if (response.isFailed()) {
        body = ((InvocationException) body).getErrorData();
    }

    try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
        produceProcessor.encodeResponse(output, body);

        responseEx.setBodyBuffer(output.getBuffer());
        for (HttpServerFilter filter : httpServerFilters) {
            filter.beforeSendResponse(invocation, responseEx);
        }

        responseEx.flushBuffer();
    }
}

From source file:io.vertx.core.http.impl.HttpClientRequestImpl.java

License:Open Source License

private void write(ByteBuf buff, boolean end) {
    if (buff == null && !end) {
        // nothing to write to the connection just return
        return;/*from   ww w .j  a va  2 s.  c om*/
    }

    if (end) {
        if (buff != null && !chunked && !contentLengthSet()) {
            headers().set(CONTENT_LENGTH, String.valueOf(buff.writerIndex()));
        }
    } else {
        if (!chunked && !contentLengthSet()) {
            throw new IllegalStateException(
                    "You must set the Content-Length header to be the total size of the message "
                            + "body BEFORE sending any data if you are not using HTTP chunked encoding.");
        }
    }

    if (buff != null) {
        written += buff.readableBytes();
        if (followRedirects > 0) {
            if (cachedChunks == null) {
                cachedChunks = Unpooled.compositeBuffer();
            }
            cachedChunks.addComponent(buff).writerIndex(cachedChunks.writerIndex() + buff.writerIndex());
        }
    }

    if (stream == null) {
        if (buff != null) {
            if (pendingChunks == null) {
                pendingChunks = buff;
            } else {
                CompositeByteBuf pending;
                if (pendingChunks instanceof CompositeByteBuf) {
                    pending = (CompositeByteBuf) pendingChunks;
                } else {
                    pending = Unpooled.compositeBuffer();
                    pending.addComponent(pendingChunks).writerIndex(pendingChunks.writerIndex());
                    pendingChunks = pending;
                }
                pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex());
            }
        }
        connect(null);
    } else {
        if (!headWritten) {
            writeHeadWithContent(buff, end);
        } else {
            stream.writeBuffer(buff, end);
        }
        if (end) {
            stream.connection().reportBytesWritten(written);
            if (respHandler != null) {
                stream.endRequest();
            }
        }
    }

    if (end) {
        completed = true;
        if (completionHandler != null) {
            completionHandler.handle(null);
        }
    }
}

From source file:org.apache.servicecomb.common.rest.filter.inner.ServerRestArgsFilter.java

License:Apache License

@Override
public CompletableFuture<Void> beforeSendResponseAsync(Invocation invocation,
        HttpServletResponseEx responseEx) {
    Response response = (Response) responseEx.getAttribute(RestConst.INVOCATION_HANDLER_RESPONSE);
    ProduceProcessor produceProcessor = (ProduceProcessor) responseEx
            .getAttribute(RestConst.INVOCATION_HANDLER_PROCESSOR);
    Object body = response.getResult();
    if (response.isFailed()) {
        body = ((InvocationException) body).getErrorData();
    }/*  ww  w.ja  v  a 2 s .c  om*/

    if (Part.class.isInstance(body)) {
        return responseEx.sendPart((Part) body);
    }

    responseEx.setContentType(produceProcessor.getName() + "; charset=utf-8");

    CompletableFuture<Void> future = new CompletableFuture<>();
    try (BufferOutputStream output = new BufferOutputStream(Unpooled.compositeBuffer())) {
        produceProcessor.encodeResponse(output, body);

        responseEx.setBodyBuffer(output.getBuffer());
        future.complete(null);
    } catch (Throwable e) {
        future.completeExceptionally(ExceptionFactory.convertProducerException(e));
    }
    return future;
}

From source file:org.eclipse.milo.opcua.sdk.client.DataTypeDictionaryReader.java

License:Open Source License

CompletableFuture<ByteString> readDataTypeDictionaryBytes(NodeId nodeId, int fragmentSize) {
    CompositeByteBuf fragmentBuffer = Unpooled.compositeBuffer();

    CompletableFuture<ByteBuf> future = readFragments(nodeId, fragmentBuffer, fragmentSize, 0);

    return future.thenApply(buffer -> {
        // trim any junk at the end. some servers have a bug
        // that cause a null byte to be appended to the end,
        // which makes it invalid XML.
        int length = buffer.readableBytes();

        for (int i = buffer.writerIndex() - 1; i >= 0; i--) {
            byte lastByte = buffer.getByte(i);

            boolean empty = (lastByte == 0 || Character.isWhitespace(lastByte)
                    || Character.isSpaceChar(lastByte));

            if (!empty)
                break;
            else//from w w w  .ja va2s . co  m
                length -= 1;
        }

        byte[] bs = new byte[length];
        buffer.readBytes(bs, 0, length);

        if (logger.isDebugEnabled()) {
            String xmlString = new String(bs);
            logger.debug("Dictionary XML: {}", xmlString);
        }

        return ByteString.of(bs);
    });
}

From source file:org.opendaylight.controller.netconf.nettyutil.handler.NetconfChunkAggregator.java

License:Open Source License

private void initChunk() {
    chunk = Unpooled.compositeBuffer();
}

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

License:Open Source License

private DefaultHttpClientRequest write(ByteBuf buff, boolean end) {
    int readableBytes = buff.readableBytes();
    if (readableBytes == 0 && !end) {
        // nothing to write to the connection just return
        return this;
    }//  ww w .ja va  2 s . c  o m

    if (end) {
        completed = true;
    }

    written += buff.readableBytes();

    if (!end && !raw && !chunked && !contentLengthSet()) {
        throw new IllegalStateException(
                "You must set the Content-Length header to be the total size of the message "
                        + "body BEFORE sending any data if you are not using HTTP chunked encoding.");
    }

    if (conn == null) {
        if (pendingChunks == null) {
            pendingChunks = buff;
        } else {
            CompositeByteBuf pending;
            if (pendingChunks instanceof CompositeByteBuf) {
                pending = (CompositeByteBuf) pendingChunks;
            } else {
                pending = Unpooled.compositeBuffer();
                pending.addComponent(pendingChunks).writerIndex(pendingChunks.writerIndex());
                pendingChunks = pending;
            }
            pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex());
        }
        connect();
    } else {
        if (!headWritten) {
            writeHeadWithContent(buff, end);
        } else {
            if (end) {
                writeEndChunk(buff);
            } else {
                sendChunk(buff);
            }
        }
        if (end) {
            conn.endRequest();
        }
    }
    return this;
}

From source file:tachyon.network.protocol.databuffer.DataNettyBufferTest.java

License:Apache License

@Test
public void singleNioBufferCheckFailedTest() {
    mThrown.expect(IllegalArgumentException.class);
    mThrown.expectMessage("Number of nioBuffers of this bytebuf is 2 (1 expected).");
    releaseBuffer(); // not using the default ByteBuf given in Before()
    // creating a CompositeByteBuf with 2 NIO buffers
    mBuffer = Unpooled.compositeBuffer();
    ((CompositeByteBuf) mBuffer).addComponent(Unpooled.buffer(LENGTH));
    ((CompositeByteBuf) mBuffer).addComponent(Unpooled.buffer(LENGTH));
    new DataNettyBuffer(mBuffer, LENGTH);
}