Example usage for io.netty.buffer ByteBufInputStream ByteBufInputStream

List of usage examples for io.netty.buffer ByteBufInputStream ByteBufInputStream

Introduction

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

Prototype

public ByteBufInputStream(ByteBuf buffer, boolean releaseOnClose) 

Source Link

Document

Creates a new stream which reads data from the specified buffer starting at the current readerIndex and ending at the current writerIndex .

Usage

From source file:com.linecorp.armeria.internal.grpc.ArmeriaMessageDeframer.java

License:Apache License

private ByteBufOrStream getCompressedBody(ByteBuf buf) {
    if (decompressor == Codec.Identity.NONE) {
        buf.release();/*  w w w  .  j  a va 2s  .  c  o m*/
        throw Status.INTERNAL
                .withDescription(
                        DEBUG_STRING + ": Can't decode compressed frame as compression not configured.")
                .asRuntimeException();
    }

    try {
        // Enforce the maxMessageSizeBytes limit on the returned stream.
        final InputStream unlimitedStream = decompressor.decompress(new ByteBufInputStream(buf, true));
        return new ByteBufOrStream(
                new SizeEnforcingInputStream(unlimitedStream, maxMessageSizeBytes, DEBUG_STRING));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshaller.java

License:Apache License

public I deserializeRequest(ByteBufOrStream message) throws IOException {
    InputStream messageStream = message.stream();
    if (message.buf() != null) {
        try {//from ww w .j  a v  a  2 s .c om
            switch (requestType) {
            case PROTOBUF:
                final PrototypeMarshaller<I> marshaller = (PrototypeMarshaller<I>) method
                        .getRequestMarshaller();
                // PrototypeMarshaller<I>.getMessagePrototype will always parse to I
                @SuppressWarnings("unchecked")
                final I msg = (I) deserializeProto(message.buf(), (Message) marshaller.getMessagePrototype());
                return msg;
            default:
                // Fallback to using the method's stream marshaller.
                messageStream = new ByteBufInputStream(message.buf().retain(), true);
                break;
            }
        } finally {
            if (!unsafeWrapDeserializedBuffer) {
                message.buf().release();
            }
        }
    }
    try (InputStream msg = messageStream) {
        return method.parseRequest(msg);
    }
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshaller.java

License:Apache License

public O deserializeResponse(ByteBufOrStream message) throws IOException {
    InputStream messageStream = message.stream();
    if (message.buf() != null) {
        try {/* w  w w.j  av  a2s  .  c o  m*/
            switch (responseType) {
            case PROTOBUF:
                final PrototypeMarshaller<O> marshaller = (PrototypeMarshaller<O>) method
                        .getResponseMarshaller();
                // PrototypeMarshaller<I>.getMessagePrototype will always parse to I
                @SuppressWarnings("unchecked")
                final O msg = (O) deserializeProto(message.buf(), (Message) marshaller.getMessagePrototype());
                return msg;
            default:
                // Fallback to using the method's stream marshaller.
                messageStream = new ByteBufInputStream(message.buf().retain(), true);
                break;
            }
        } finally {
            if (!unsafeWrapDeserializedBuffer) {
                message.buf().release();
            }
        }
    }
    try (InputStream msg = messageStream) {
        return method.parseResponse(msg);
    }
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshaller.java

License:Apache License

private Message deserializeProto(ByteBuf buf, Message prototype) throws IOException {
    if (GrpcSerializationFormats.isProto(serializationFormat)) {
        final CodedInputStream stream;
        if (unsafeWrapDeserializedBuffer) {
            stream = UnsafeByteOperations.unsafeWrap(buf.nioBuffer()).newCodedInput();
            stream.enableAliasing(true);
        } else {/*from ww w  . ja v a2  s . c  om*/
            stream = CodedInputStream.newInstance(buf.nioBuffer());
        }
        try {
            final Message msg = prototype.getParserForType().parseFrom(stream);
            try {
                stream.checkLastTagWas(0);
            } catch (InvalidProtocolBufferException e) {
                e.setUnfinishedMessage(msg);
                throw e;
            }
            return msg;
        } catch (InvalidProtocolBufferException e) {
            throw Status.INTERNAL.withDescription("Invalid protobuf byte sequence").withCause(e)
                    .asRuntimeException();
        }
    }

    if (GrpcSerializationFormats.isJson(serializationFormat)) {
        final Message.Builder builder = prototype.newBuilderForType();
        try (ByteBufInputStream is = new ByteBufInputStream(buf, /* releaseOnClose */ false)) {
            jsonMarshaller.mergeValue(is, builder);
        }
        return builder.build();
    }
    throw new IllegalStateException("Unknown serialization format: " + serializationFormat);
}

From source file:com.linecorp.armeria.internal.grpc.GrpcTestUtil.java

License:Apache License

public static byte[] compressedFrame(ByteBuf uncompressed) {
    ByteBuf compressed = Unpooled.buffer();
    try (ByteBufInputStream is = new ByteBufInputStream(uncompressed, true);
            GZIPOutputStream os = new GZIPOutputStream(new ByteBufOutputStream(compressed))) {
        ByteStreams.copy(is, os);/*  w  w w.  ja  va  2s.c  o  m*/
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    ByteBuf buf = Unpooled.buffer();
    buf.writeByte(1);
    buf.writeInt(compressed.readableBytes());
    buf.writeBytes(compressed);
    compressed.release();
    byte[] result = ByteBufUtil.getBytes(buf);
    buf.release();
    return result;
}

From source file:com.linecorp.armeria.server.grpc.ArmeriaServerCallTest.java

License:Apache License

@Test
public void messageReadAfterClose_stream() {
    call.close(Status.ABORTED, new Metadata());

    // messageRead is always called from the event loop.
    eventLoop.submit(() -> {//from   w w w. j  a  v  a 2s  . c  o m
        call.messageRead(new ByteBufOrStream(new ByteBufInputStream(GrpcTestUtil.requestByteBuf(), true)));

        verify(listener, never()).onMessage(any());
    }).syncUninterruptibly();
}

From source file:io.apptik.comm.jus.netty.NettyHttpClientHandler.java

License:Apache License

@Override
public void messageReceived(ChannelHandlerContext ctx, HttpObject msg) {
    if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        System.err.println("STATUS: " + response.status());
        System.err.println("VERSION: " + response.protocolVersion());
        System.err.println();/*from   w  w w  . j  av a 2 s. c  o  m*/

        builder.setStatusCode(response.status().code());

        if (!response.headers().isEmpty()) {
            for (CharSequence name : response.headers().names()) {
                for (CharSequence value : response.headers().getAll(name)) {
                    System.err.println("HEADER: " + name + " = " + value);
                    if (name.toString().equalsIgnoreCase("content-length")) {
                        len = Integer.parseInt(value.toString());
                        System.err.println("LEN: " + len);
                    }
                    builder.setHeader(name.toString(), value.toString());
                }
            }
            System.err.println();
        }

        if (HttpHeaderUtil.isTransferEncodingChunked(response)) {
            System.err.println("CHUNKED CONTENT {");
        } else {
            System.err.println("CONTENT {");
        }
    }

    if (msg instanceof HttpContent) {
        HttpContent content = (HttpContent) msg;
        if (!content.content().hasArray()) {
            try {
                builder.setBody(AbstractHttpStack
                        .getContentBytes(new ByteBufInputStream(content.content(), len), byteArrayPool, len));
            } catch (IOException e) {
                e.printStackTrace();
                exceptionCaught(ctx, e);
            }
        } else {
            builder.setBody(content.content().array());
        }

        content.content().resetReaderIndex();
        System.err.print(content.content().toString(CharsetUtil.UTF_8));
        System.err.flush();

        if (content instanceof LastHttpContent) {
            System.err.println("} END OF CONTENT");
            result = builder.build();
            resultReceived = true;
            ctx.close();
            synchronized (this) {
                this.notifyAll();
            }
        }
    }
}

From source file:org.apache.bookkeeper.mledger.offload.jcloud.impl.DataBlockHeaderImpl.java

License:Apache License

/**
 * Get the content of the data block header as InputStream.
 * Read out in format://from ww w.j  ava2s.  c  om
 *   [ magic_word -- int ][ block_len -- int ][ first_entry_id  -- long] [padding zeros]
 */
@Override
public InputStream toStream() {
    ByteBuf out = PooledByteBufAllocator.DEFAULT.buffer(HEADER_MAX_SIZE, HEADER_MAX_SIZE);
    out.writeInt(MAGIC_WORD).writeLong(headerLength).writeLong(blockLength).writeLong(firstEntryId)
            .writeBytes(PADDING);

    // true means the input stream will release the ByteBuf on close
    return new ByteBufInputStream(out, true);
}

From source file:org.apache.bookkeeper.mledger.offload.jcloud.impl.OffloadIndexBlockImpl.java

License:Apache License

/**
 * Get the content of the index block as InputStream.
 * Read out in format://from ww  w  . ja v a 2 s  . c  o m
 *   | index_magic_header | index_block_len | data_object_len | data_header_len |
 *   | index_entry_count  | segment_metadata_len | segment metadata | index entries... |
 */
@Override
public OffloadIndexBlock.IndexInputStream toStream() throws IOException {
    int indexEntryCount = this.indexEntries.size();
    byte[] ledgerMetadataByte = buildLedgerMetadataFormat(this.segmentMetadata);
    int segmentMetadataLength = ledgerMetadataByte.length;

    int indexBlockLength = 4 /* magic header */
            + 4 /* index block length */
            + 8 /* data object length */
            + 8 /* data header length */
            + 4 /* index entry count */
            + 4 /* segment metadata length */
            + segmentMetadataLength
            + indexEntryCount * (8 + 4 + 8); /* messageEntryId + blockPartId + blockOffset */

    ByteBuf out = PooledByteBufAllocator.DEFAULT.buffer(indexBlockLength, indexBlockLength);

    out.writeInt(INDEX_MAGIC_WORD).writeInt(indexBlockLength).writeLong(dataObjectLength)
            .writeLong(dataHeaderLength).writeInt(indexEntryCount).writeInt(segmentMetadataLength);
    // write metadata
    out.writeBytes(ledgerMetadataByte);

    // write entries
    this.indexEntries.entrySet().forEach(entry -> out.writeLong(entry.getValue().getEntryId())
            .writeInt(entry.getValue().getPartId()).writeLong(entry.getValue().getOffset()));

    return new OffloadIndexBlock.IndexInputStream(new ByteBufInputStream(out, true), indexBlockLength);
}

From source file:org.apache.bookkeeper.stream.storage.impl.routing.RoutingHeaderProxyInterceptor.java

License:Apache License

private <ReqT, RespT> ReqT interceptMessage(MethodDescriptor<ReqT, RespT> method, ReqT message) {
    InputStream is = method.getRequestMarshaller().stream(message);
    int bytes;//from w ww  . j  ava2 s.co  m
    try {
        bytes = is.available();
    } catch (IOException e) {
        log.warn("Encountered exceptions in getting available bytes of message", e);
        throw new RuntimeException("Encountered exception in intercepting message", e);
    }
    ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
    try {
        buffer.writeBytes(is, bytes);
    } catch (IOException e) {
        log.warn("Encountered exceptions in transferring bytes to the buffer", e);
        buffer.release();
        throw new RuntimeException("Encountered exceptions in transferring bytes to the buffer", e);
    }
    return method.getRequestMarshaller().parse(new ByteBufInputStream(buffer, true));
}