Example usage for io.netty.buffer ByteBuf release

List of usage examples for io.netty.buffer ByteBuf release

Introduction

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

Prototype

boolean release();

Source Link

Document

Decreases the reference count by 1 and deallocates this object if the reference count reaches at 0 .

Usage

From source file:com.linecorp.armeria.common.stream.StreamMessageDuplicatorTest.java

License:Apache License

@Test
public void raiseExceptionInOnNext() {
    final DefaultStreamMessage<ByteBuf> publisher = new DefaultStreamMessage<>();
    final ByteBufDuplicator duplicator = new ByteBufDuplicator(publisher);

    final ByteBuf buf = newUnpooledBuffer();
    publisher.write(buf);/*ww w .ja v  a  2s.com*/
    assertThat(buf.refCnt()).isOne();

    // Release the buf after writing to the publisher which must not happen!
    buf.release();

    final ByteBufSubscriber subscriber = new ByteBufSubscriber();
    duplicator.duplicateStream().subscribe(subscriber, ImmediateEventExecutor.INSTANCE);
    assertThatThrownBy(() -> subscriber.completionFuture().get())
            .hasCauseInstanceOf(IllegalReferenceCountException.class);
}

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();
        throw Status.INTERNAL
                .withDescription(/*w w  w.j  a v a2s. c om*/
                        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.ArmeriaMessageFramer.java

License:Apache License

private ByteBuf writeCompressed(ByteBuf message) throws IOException {
    final CompositeByteBuf compressed = alloc.compositeBuffer();
    try (OutputStream compressingStream = compressor.compress(new ByteBufOutputStream(compressed))) {
        compressingStream.write(ByteBufUtil.getBytes(message));
    } finally {/*from  www .  ja va2s.c o  m*/
        message.release();
    }

    return write(compressed, true);
}

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

License:Apache License

private ByteBuf write(ByteBuf message, boolean compressed) {
    try {/*w  w w.  j  av a  2 s.c  o  m*/
        final int messageLength = message.readableBytes();
        if (maxOutboundMessageSize >= 0 && messageLength > maxOutboundMessageSize) {
            throw Status.RESOURCE_EXHAUSTED
                    .withDescription(
                            String.format("message too large %d > %d", messageLength, maxOutboundMessageSize))
                    .asRuntimeException();
        }
        final ByteBuf buf = alloc.buffer(HEADER_LENGTH + messageLength);
        buf.writeByte(compressed ? COMPRESSED : UNCOMPRESSED);
        buf.writeInt(messageLength);
        buf.writeBytes(message);
        return buf;
    } finally {
        message.release();
    }
}

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

License:Apache License

private ByteBuf serializeProto(Message message) throws IOException {
    if (GrpcSerializationFormats.isProto(serializationFormat)) {
        final ByteBuf buf = alloc.buffer(message.getSerializedSize());
        boolean success = false;
        try {//w  w  w  .ja va2 s . co  m
            message.writeTo(CodedOutputStream.newInstance(buf.nioBuffer(0, buf.writableBytes())));
            buf.writerIndex(buf.capacity());
            success = true;
        } finally {
            if (!success) {
                buf.release();
            }
        }
        return buf;
    }

    if (GrpcSerializationFormats.isJson(serializationFormat)) {
        final ByteBuf buf = alloc.buffer();
        boolean success = false;
        try (ByteBufOutputStream os = new ByteBufOutputStream(buf)) {
            jsonMarshaller.writeValue(message, os);
            success = true;
        } finally {
            if (!success) {
                buf.release();
            }
        }
        return buf;
    }
    throw new IllegalStateException("Unknown serialization format: " + serializationFormat);
}

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

License:Apache License

@Test
public void serializeRequest() throws Exception {
    ByteBuf serialized = marshaller.serializeRequest(GrpcTestUtil.REQUEST_MESSAGE);
    assertThat(ByteBufUtil.getBytes(serialized)).containsExactly(GrpcTestUtil.REQUEST_MESSAGE.toByteArray());
    serialized.release();
}

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

License:Apache License

@Test
public void deserializeRequest_wrappedByteBuf() throws Exception {
    marshaller = new GrpcMessageMarshaller<>(ByteBufAllocator.DEFAULT, GrpcSerializationFormats.PROTO,
            TestServiceGrpc.getUnaryCallMethod(),
            MessageMarshaller.builder().register(SimpleRequest.getDefaultInstance())
                    .register(SimpleResponse.getDefaultInstance()).build(),
            true);//from ww w  .j  a va  2s. co m
    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.REQUEST_MESSAGE.getSerializedSize());
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.writeBytes(GrpcTestUtil.REQUEST_MESSAGE.toByteArray());
    SimpleRequest request = marshaller.deserializeRequest(new ByteBufOrStream(buf));
    assertThat(request).isEqualTo(GrpcTestUtil.REQUEST_MESSAGE);
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.release();
}

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

License:Apache License

@Test
public void serializeResponse() throws Exception {
    ByteBuf serialized = marshaller.serializeResponse(GrpcTestUtil.RESPONSE_MESSAGE);
    assertThat(ByteBufUtil.getBytes(serialized)).containsExactly(GrpcTestUtil.RESPONSE_MESSAGE.toByteArray());
    serialized.release();
}

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

License:Apache License

@Test
public void deserializeResponse_wrappedByteBuf() throws Exception {
    marshaller = new GrpcMessageMarshaller<>(ByteBufAllocator.DEFAULT, GrpcSerializationFormats.PROTO,
            TestServiceGrpc.getUnaryCallMethod(),
            MessageMarshaller.builder().register(SimpleRequest.getDefaultInstance())
                    .register(SimpleResponse.getDefaultInstance()).build(),
            true);//from  w  ww  . java 2s .  c  o m
    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.RESPONSE_MESSAGE.getSerializedSize());
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.writeBytes(GrpcTestUtil.RESPONSE_MESSAGE.toByteArray());
    SimpleResponse response = marshaller.deserializeResponse(new ByteBufOrStream(buf));
    assertThat(response).isEqualTo(GrpcTestUtil.RESPONSE_MESSAGE);
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.release();
}

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

License:Apache License

public static byte[] uncompressedFrame(ByteBuf proto) {
    ByteBuf buf = Unpooled.buffer();/*from  w  ww  .j a  va2  s  . c o  m*/
    buf.writeByte(0);
    buf.writeInt(proto.readableBytes());
    buf.writeBytes(proto);
    proto.release();
    byte[] result = ByteBufUtil.getBytes(buf);
    buf.release();
    return result;
}