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.github.milenkovicm.kafka.handler.MetadataHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    final ByteBuf message = (ByteBuf) msg;

    final MetadataResponse metadataResponse = parseMetadataResponse(message);
    message.release();
    LOGGER.debug("received metadata response from broker! response: [{}]", metadataResponse);
    KafkaPromise promise = acks.get(metadataResponse.correlationId);

    if (promise != null) {
        acks.remove(metadataResponse.correlationId);

        if (metadataResponse.topics.size() > 0) {
            if (metadataResponse.topics.get(0).topicErrorCode != Error.NO_ERROR.value) {
                final Error error = Error.valueOf(metadataResponse.topics.get(0).topicErrorCode);
                promise.setFailure(error.exception);
            } else {
                promise.setSuccess(metadataResponse);
            }/*from www  .ja va 2 s.com*/
        }

    }
}

From source file:com.github.milenkovicm.kafka.KafkaTopic.java

License:Apache License

private void release(ByteBuf key, ByteBuf message) {
    if (message != null) {
        message.release();
    }/*from   www. j a va 2 s  . c  o  m*/

    if (key != null) {
        key.release();
    }
}

From source file:com.github.milenkovicm.kafka.util.DropBackoffStrategy.java

License:Apache License

@Override
public boolean handle(Channel channel, ByteBuf key, ByteBuf message) {
    if (key != null) {
        key.release();
    }//from  w w  w.ja v a 2  s .  com
    if (message != null) {
        message.release();
    }

    return true;
}

From source file:com.github.sadikovi.netflowlib.NetFlowReader.java

License:Apache License

/**
 * [[NetFlowReader]] provides interface to get parsed header and record buffer with chosen
 * strategy based on columns, predicate and statistics. Metadata, header are parsed as part of
 * initialization./*from   w  ww .jav  a 2  s. co m*/
 */
private NetFlowReader(DataInputStream inputStream, int buffer, boolean ignoreCorruptFile) throws IOException {
    in = inputStream;
    bufferLength = buffer;
    ignoreCorrupt = ignoreCorruptFile;
    byte[] metadata = null;
    ByteBuf buf = null;

    try {
        metadata = new byte[METADATA_LENGTH];
        in.read(metadata, 0, METADATA_LENGTH);

        // Parse metadata, byte order does not really matter, so we go for big endian. Metadata contains
        // magic numbers to verify consistency of the NetFlow file, byte order encoded as either 1 or 2,
        // and stream version which affects header parsing (currently only 1 and 3 are supported).
        buf = Unpooled.wrappedBuffer(metadata).order(ByteOrder.BIG_ENDIAN);
        short magic1 = buf.getUnsignedByte(0);
        short magic2 = buf.getUnsignedByte(1);
        short order = buf.getUnsignedByte(2);
        short stream = buf.getUnsignedByte(3);

        // Verify consistency of NetFlow file, also this ensures that we are at the beginning of the
        // input stream
        if (magic1 != HEADER_MAGIC1 || magic2 != HEADER_MAGIC2) {
            throw new IOException("Corrupt NetFlow file. Wrong magic number");
        }

        // Resolve byte order, last case corresponds to incorrect reading from buffer
        if (order == HEADER_BIG_ENDIAN) {
            byteOrder = ByteOrder.BIG_ENDIAN;
        } else if (order == HEADER_LITTLE_ENDIAN) {
            byteOrder = ByteOrder.LITTLE_ENDIAN;
        } else {
            throw new IOException("Could not recognize byte order " + order);
        }

        streamVersion = stream;

        // Check stream version
        ensureStreamVersion();

        // Read header
        header = getHeader();
    } catch (IOException err) {
        if (ignoreCorrupt) {
            // we subsume exception and log warning. Set header to null
            log.warn("Failed to initialize reader, ignoreCorruptFile=" + ignoreCorrupt + ", error=" + err);
            header = new CorruptNetFlowHeader();
        } else {
            throw err;
        }
    } finally {
        metadata = null;
        if (buf != null) {
            buf.release();
            buf = null;
        }
    }
}

From source file:com.github.sinsinpub.pero.manual.proxyhandler.HttpProxyServer.java

License:Apache License

private boolean authenticate(ChannelHandlerContext ctx, FullHttpRequest req) {
    assertThat(req.method(), Matchers.is(HttpMethod.CONNECT));

    if (testMode != TestMode.INTERMEDIARY) {
        ctx.pipeline().addBefore(ctx.name(), "lineDecoder", new LineBasedFrameDecoder(64, false, true));
    }//from ww  w .ja v  a 2s. c  o m

    ctx.pipeline().remove(HttpObjectAggregator.class);
    ctx.pipeline().remove(HttpRequestDecoder.class);

    boolean authzSuccess = false;
    if (username != null) {
        CharSequence authz = req.headers().get(HttpHeaderNames.PROXY_AUTHORIZATION);
        if (authz != null) {
            String[] authzParts = StringUtil.split(authz.toString(), ' ', 2);
            ByteBuf authzBuf64 = Unpooled.copiedBuffer(authzParts[1], CharsetUtil.US_ASCII);
            ByteBuf authzBuf = Base64.decode(authzBuf64);

            String expectedAuthz = username + ':' + password;
            authzSuccess = "Basic".equals(authzParts[0])
                    && expectedAuthz.equals(authzBuf.toString(CharsetUtil.US_ASCII));

            authzBuf64.release();
            authzBuf.release();
        }
    } else {
        authzSuccess = true;
    }

    return authzSuccess;
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test(expected = NotEnoughDataDecoderException.class)
public void testReadOctetStringToStringWhenInputBufferSizeLessThan4ThrowNotEnoughDataException()
        throws Exception {
    ByteBuf inputBuffer = Unpooled.buffer(1);

    try {//ww w.jav  a  2s  . co m
        ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);
    } finally {
        inputBuffer.release();
    }
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToStringWhenOctetStringSizeIsMinus1ReturnsEmptyString() throws Exception {
    byte[] input = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);

    String decodedString = ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);

    assertEquals("Decoded string size is not 0", 0, decodedString.length());

    inputBuffer.release();
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test(expected = StringSizeException.class)
public void testReadOctetStringToStringWhenOctetStringSizeIsOtherNegativeNumberThrowsStringSizeException()
        throws Exception {
    byte[] input = { (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00 };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);

    try {//from  w w w .  ja  v  a  2s  .co m
        ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);

    } finally {
        inputBuffer.release();
    }
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test(expected = StringSizeException.class)
public void testReadOctetStringToStringWhenOctetStringSizeIsGreaterThanBufferSizeThrowsStringSizeException()
        throws Exception {
    byte[] input = { 0x00, 0x00, 0x00, 0x02, 'a' };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);

    try {/*w w w  .ja va 2 s. c o  m*/
        ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);

    } finally {
        inputBuffer.release();
    }
}

From source file:com.github.spapageo.jannel.channel.ChannelBufferUtilsTest.java

License:Open Source License

@Test
public void testReadOctetStringToString_when_octet_string_size_is_zero_returns_empty_string() throws Exception {
    byte[] input = { 0x00, 0x00, 0x00, 0x00 };
    ByteBuf inputBuffer = Unpooled.copiedBuffer(input);

    String decodedString = ChannelBufferUtils.readOctetStringToString(inputBuffer, StandardCharsets.UTF_8);

    assertEquals("Decoded string size is not 0", 0, decodedString.length());
    inputBuffer.release();
}