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.turo.pushy.apns.server.ValidatingPushNotificationHandlerTest.java

License:Open Source License

@Test
public void testHandleNotificationWithEmptyPayload() {
    final ByteBuf emptyPayload = UnpooledByteBufAllocator.DEFAULT.buffer();

    try {//from  www .  ja  v  a2  s  .  c om
        this.testWithExpectedRejection(
                "Push notifications with a device token for the wrong topic should be rejected.",
                this.getHandler(DEVICE_TOKENS_BY_TOPIC, Collections.<String, Date>emptyMap()), this.headers,
                emptyPayload, RejectionReason.PAYLOAD_EMPTY);
    } finally {
        emptyPayload.release();
    }
}

From source file:com.turo.pushy.apns.server.ValidatingPushNotificationHandlerTest.java

License:Open Source License

@Test
public void testHandleNotificationWithOversizedPayload() {
    final int size = 4096 * 2;
    final ByteBuf largePayload = UnpooledByteBufAllocator.DEFAULT.buffer(size);

    try {/*from  www. j  ava  2 s  .co  m*/
        final byte[] payloadBytes = new byte[size];
        new Random().nextBytes(payloadBytes);

        largePayload.writeBytes(payloadBytes);

        this.testWithExpectedRejection(
                "Push notifications with a device token for the wrong topic should be rejected.",
                this.getHandler(DEVICE_TOKENS_BY_TOPIC, Collections.<String, Date>emptyMap()), this.headers,
                largePayload, RejectionReason.PAYLOAD_TOO_LARGE);
    } finally {
        largePayload.release();
    }
}

From source file:com.uber.tchannel.codecs.CodecUtils.java

License:Open Source License

public static ByteBuf writeArgsCopy(ByteBufAllocator allocator, ByteBuf header, List<ByteBuf> args) {
    ByteBuf payload = allocator.buffer(header.readableBytes(), TFrame.MAX_FRAME_PAYLOAD_LENGTH);
    payload.writeBytes(header);/*  w  w w  .  j a  v a2 s  . co m*/
    header.release();
    int writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - payload.readableBytes();

    while (!args.isEmpty()) {
        ByteBuf arg = args.get(0);
        writeArgCopy(allocator, payload, arg, writableBytes);
        writableBytes = TFrame.MAX_FRAME_PAYLOAD_LENGTH - payload.readableBytes();
        if (writableBytes <= TFrame.FRAME_SIZE_LENGTH) {
            break;
        }

        if (arg.readableBytes() == 0) {
            args.remove(0);
        }
    }

    return payload;
}

From source file:com.uber.tchannel.codecs.MessageCodec.java

License:Open Source License

public static Request decodeCallRequest(List<CallFrame> frames) {

    if (frames.isEmpty()) {
        return null;
    }/*from  ww  w .j a  v  a2s . c o m*/

    CallRequestFrame first = (CallRequestFrame) frames.get(0);
    ArgScheme scheme = ArgScheme.toScheme(first.getHeaders().get(TransportHeaders.ARG_SCHEME_KEY));
    if (!ArgScheme.isSupported(scheme)) {
        return null;
    }

    List<ByteBuf> args = new ArrayList<>();
    for (CallFrame frame : frames) {
        CodecUtils.readArgs(args, frame.getPayload());
        frame.release();
    }

    if (args.size() != 3) {
        for (ByteBuf arg : args) {
            arg.release();
        }

        throw new UnsupportedOperationException("The arg count is not should be 3 instead of " + args.size());
    }

    return Request.build(scheme, first.getId(), first.getTTL(), first.getService(), first.getHeaders(),
            args.get(0), args.get(1), args.get(2));
}

From source file:com.uber.tchannel.codecs.MessageCodec.java

License:Open Source License

public static Response decodeCallResponse(List<CallFrame> frames) {

    if (frames.isEmpty()) {
        return null;
    }//w  w w  .  java  2  s  .c om

    CallResponseFrame first = (CallResponseFrame) frames.get(0);
    ArgScheme scheme = ArgScheme.toScheme(first.getHeaders().get(TransportHeaders.ARG_SCHEME_KEY));
    if (!ArgScheme.isSupported(scheme)) {
        return null;
    }

    List<ByteBuf> args = new ArrayList<>();
    for (CallFrame frame : frames) {
        CodecUtils.readArgs(args, frame.getPayload());
        frame.release();
    }

    if (args.size() != 3) {
        for (ByteBuf arg : args) {
            arg.release();
        }

        throw new UnsupportedOperationException("The arg count is not should be 3 instead of " + args.size());
    }

    args.get(0).release();
    return Response.build(scheme, first.getId(), first.getResponseCode(), first.getHeaders(), args.get(1),
            args.get(2));
}

From source file:com.uber.tchannel.messages.ThriftSerializerTest.java

License:Open Source License

@Test
public void testEncodeDecodeEndpoint() throws Exception {
    String endpoint = "SomeService::someMethod";
    ByteBuf endpointBuf = this.serializer.encodeEndpoint(endpoint);
    String decodedEndpoint = this.serializer.decodeEndpoint(endpointBuf);
    assertEquals(endpoint, decodedEndpoint);
    endpointBuf.release();
}

From source file:com.uber.tchannel.messages.ThriftSerializerTest.java

License:Open Source License

@Test
public void testEncodeDecodeHeaders() throws Exception {
    Map<String, String> headers = new HashMap<String, String>() {
        {// ww w .ja  v  a 2  s .  co  m
            put("foo", "bar");
        }
    };

    ByteBuf binaryHeaders = serializer.encodeHeaders(headers);
    Map<String, String> decodedHeaders = serializer.decodeHeaders(binaryHeaders);
    assertEquals(headers, decodedHeaders);
    binaryHeaders.release();
}

From source file:com.uber.tchannel.messages.ThriftSerializerTest.java

License:Open Source License

@Test
public void testEncodeDecodeEmptyHeaders() {
    Map<String, String> emptyHeaders = new HashMap<>();
    ByteBuf binaryHeaders = serializer.encodeHeaders(emptyHeaders);
    Map<String, String> decodedHeaders = serializer.decodeHeaders(binaryHeaders);
    assertEquals(emptyHeaders, decodedHeaders);
    binaryHeaders.release();
}

From source file:com.uber.tchannel.messages.ThriftSerializerTest.java

License:Open Source License

@Test
public void testEncodeDecodeBody() throws Exception {

    // Given//w ww .j a v  a 2s.c o m
    Class<?> instanceType = Example.class;
    Example obj = (Example) instanceType.newInstance();
    obj.setAnInteger(42);
    obj.setAString("Hello, World!");

    // Then
    ByteBuf bodyBuf = this.serializer.encodeBody(obj);
    Example decodedObj = (Example) this.serializer.decodeBody(bodyBuf, instanceType);

    assertEquals(obj.getAnInteger(), decodedObj.getAnInteger());
    assertEquals(obj.getAString(), decodedObj.getAString());
    assertEquals(obj, decodedObj);
    bodyBuf.release();
}

From source file:com.uber.tchannel.tracing.TracingPropagationTest.java

License:Open Source License

private static TraceResponse callDownstreamThrift(String remainingEncodings) throws Exception {
    ThriftRequest<Example> request = new ThriftRequest.Builder<Example>("tchannel-name", "Behavior::trace")
            .setTimeout(1, TimeUnit.MINUTES).setBody(new Example(remainingEncodings, 0)).build();

    TFuture<ThriftResponse<Example>> responsePromise = subChannel.send(request, tchannel.getHost(),
            tchannel.getListeningPort());

    ThriftResponse<Example> thriftResponse = responsePromise.get();
    assertNull(thriftResponse.getError());
    String json = thriftResponse.getBody(Example.class).getAString();
    thriftResponse.release();//from w  w  w  .  j a v  a 2 s  .c o m
    ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(json.length());
    byteBuf.writeBytes(json.getBytes());
    TraceResponse response = new JSONSerializer().decodeBody(byteBuf, TraceResponse.class);
    byteBuf.release();
    return response;
}