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:io.atomix.catalyst.transport.NettyConnection.java

License:Apache License

/**
 * Handles response./*from   w ww.  ja v a2 s.com*/
 */
void handleResponse(ByteBuf response) {
    long requestId = response.readLong();
    byte status = response.readByte();
    switch (status) {
    case SUCCESS:
        handleResponseSuccess(requestId, readResponse(response));
        break;
    case FAILURE:
        handleResponseFailure(requestId, readError(response));
        break;
    }
    response.release();
}

From source file:io.atomix.cluster.messaging.impl.MessageDecoderTest.java

License:Apache License

@Test
public void testReadStringFromHeapBuffer() throws Exception {
    String payload = "huuhaa";
    ByteBuf byteBuf = Unpooled.wrappedBuffer(payload.getBytes(StandardCharsets.UTF_8));
    try {/* w w  w  . ja  v a  2s.  co m*/
        assertEquals(payload, MessageDecoder.readString(byteBuf, payload.length(), StandardCharsets.UTF_8));
    } finally {
        byteBuf.release();
    }
    byte[] bytes = payload.getBytes(StandardCharsets.UTF_8);
    byteBuf = Unpooled.buffer(4 + bytes.length);
    try {
        byteBuf.writeInt(1);
        byteBuf.writeBytes(bytes);
        byteBuf.readInt();
        assertEquals(payload, MessageDecoder.readString(byteBuf, payload.length(), StandardCharsets.UTF_8));
    } finally {
        byteBuf.release();
    }
}

From source file:io.atomix.cluster.messaging.impl.MessageDecoderTest.java

License:Apache License

@Test
public void testReadStringFromDirectBuffer() throws Exception {
    String payload = "huuhaa";
    ByteBuf byteBuf = Unpooled.directBuffer(payload.length())
            .writeBytes(payload.getBytes(StandardCharsets.UTF_8));
    try {/*from   w w  w  .  j  a v  a  2 s .c  om*/
        assertEquals(payload, MessageDecoder.readString(byteBuf, payload.length(), StandardCharsets.UTF_8));
    } finally {
        byteBuf.release();
    }
}

From source file:io.atomix.cluster.messaging.impl.MessageEncoder.java

License:Apache License

private void encodeRequest(InternalRequest request, ByteBuf out) {
    encodeMessage(request, out);//from  www. java  2s .c o m

    final ByteBuf buf = out.alloc().buffer(ByteBufUtil.utf8MaxBytes(request.subject()));
    try {
        final int length = ByteBufUtil.writeUtf8(buf, request.subject());
        // write length of message type
        out.writeShort(length);
        // write message type bytes
        out.writeBytes(buf);
    } finally {
        buf.release();
    }
}

From source file:io.crate.protocols.http.HttpBlobHandler.java

License:Apache License

private void put(HttpContent content, String index, String digest) throws IOException {
    if (digestBlob == null) {
        digestBlob = blobService.newBlob(index, digest);
    }//w  w w  . ja v a  2 s.  co  m
    boolean continueExpected = HttpUtil.is100ContinueExpected(currentMessage);
    if (content == null) {
        if (continueExpected) {
            ctx.writeAndFlush(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
        }
        return;
    }

    boolean isLast = content instanceof LastHttpContent;
    ByteBuf byteBuf = content.content();
    try {
        writeToFile(byteBuf, isLast, continueExpected);
    } finally {
        byteBuf.release();
    }
}

From source file:io.crate.protocols.postgres.PostgresWireProtocol.java

License:Apache License

private Properties readStartupMessage(ByteBuf buffer) {
    Properties properties = new Properties();
    ByteBuf byteBuf = buffer.readBytes(msgLength);
    while (true) {
        String key = readCString(byteBuf);
        if (key == null) {
            break;
        }/*from ww w.  j av  a2 s  . co  m*/
        String value = readCString(byteBuf);
        LOGGER.trace("payload: key={} value={}", key, value);
        if (!"".equals(key) && !"".equals(value)) {
            properties.setProperty(key, value);
        }
    }
    byteBuf.release();
    return properties;
}

From source file:io.crate.protocols.postgres.PostgresWireProtocolTest.java

License:Apache License

@Test
public void testHandleEmptySimpleQuery() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class),
            new AlwaysOKNullAuthentication(), null);
    EmbeddedChannel channel = new EmbeddedChannel(ctx.decoder, ctx.handler);

    ByteBuf buffer = Unpooled.buffer();
    try {//  ww  w  . java  2s .c o  m
        Messages.writeCString(buffer, ";".getBytes(StandardCharsets.UTF_8));
        ctx.handleSimpleQuery(buffer, channel);
    } finally {
        buffer.release();
    }

    ByteBuf firstResponse = channel.readOutbound();
    byte[] responseBytes = new byte[5];
    firstResponse.readBytes(responseBytes);
    // EmptyQueryResponse: 'I' | int32 len
    assertThat(responseBytes, is(new byte[] { 'I', 0, 0, 0, 4 }));

    ByteBuf secondResponse = channel.readOutbound();
    responseBytes = new byte[6];
    secondResponse.readBytes(responseBytes);
    // ReadyForQuery: 'Z' | int32 len | 'I'
    assertThat(responseBytes, is(new byte[] { 'Z', 0, 0, 0, 5, 'I' }));
}

From source file:io.crate.protocols.postgres.types.BasePGTypeTest.java

License:Apache License

void assertBytesWritten(Object value, byte[] expectedBytes, int expectedLength) {
    ByteBuf buffer = Unpooled.buffer();
    try {//from   w w w .  j a va2s  .  c  o m
        int bytesWritten = pgType.writeAsBinary(buffer, value);
        assertThat(bytesWritten, is(expectedLength));

        byte[] bytes = new byte[expectedLength];
        buffer.getBytes(0, bytes);
        assertThat(bytes, is(expectedBytes));
    } finally {
        buffer.release();
    }
}

From source file:io.crate.protocols.postgres.types.BasePGTypeTest.java

License:Apache License

@SuppressWarnings("unchecked")
private void assertBytesRead(byte[] value, T expectedValue, int pos, FormatCodes.FormatCode formatCode) {
    ByteBuf buffer = Unpooled.wrappedBuffer(value);
    T readValue;/* w w w  .j av a 2  s . c om*/
    if (formatCode == FormatCodes.FormatCode.BINARY) {
        readValue = (T) pgType.readBinaryValue(buffer, pos);
    } else {
        readValue = (T) pgType.readTextValue(buffer, pos);
    }
    buffer.release();
    assertThat(readValue, is(expectedValue));
}

From source file:io.crate.protocols.postgres.types.PGTypesTest.java

License:Apache License

private Object writeAndReadBinary(Entry entry, PGType pgType) {
    ByteBuf buffer = Unpooled.buffer();
    try {// www .  ja va 2 s .  c o  m
        pgType.writeAsBinary(buffer, entry.value);
        int length = buffer.readInt();
        return pgType.readBinaryValue(buffer, length);
    } finally {
        buffer.release();
    }
}