Example usage for io.netty.util ReferenceCountUtil safeRelease

List of usage examples for io.netty.util ReferenceCountUtil safeRelease

Introduction

In this page you can find the example usage for io.netty.util ReferenceCountUtil safeRelease.

Prototype

public static void safeRelease(Object msg) 

Source Link

Document

Try to call ReferenceCounted#release() if the specified message implements ReferenceCounted .

Usage

From source file:org.apache.hadoop.hbase.ipc.NettyRpcConnection.java

License:Apache License

@Override
public synchronized void cleanupConnection() {
    if (connectionHeaderPreamble != null) {
        ReferenceCountUtil.safeRelease(connectionHeaderPreamble);
    }/*w w  w  .j a va  2s . c  o  m*/
    if (connectionHeaderWithLength != null) {
        ReferenceCountUtil.safeRelease(connectionHeaderWithLength);
    }
}

From source file:org.apache.hadoop.hbase.security.CryptoAESWrapHandler.java

License:Apache License

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
    if (queue.isEmpty()) {
        return;/*from   ww  w  .j a v a2 s  . c o  m*/
    }
    ByteBuf buf = null;
    try {
        ChannelPromise promise = ctx.newPromise();
        int readableBytes = queue.readableBytes();
        buf = queue.remove(readableBytes, promise);
        byte[] bytes = new byte[readableBytes];
        buf.readBytes(bytes);
        byte[] wrapperBytes = cryptoAES.wrap(bytes, 0, bytes.length);
        ChannelPromise lenPromise = ctx.newPromise();
        ctx.write(ctx.alloc().buffer(4).writeInt(wrapperBytes.length), lenPromise);
        ChannelPromise contentPromise = ctx.newPromise();
        ctx.write(Unpooled.wrappedBuffer(wrapperBytes), contentPromise);
        PromiseCombiner combiner = new PromiseCombiner();
        combiner.addAll(lenPromise, contentPromise);
        combiner.finish(promise);
        ctx.flush();
    } finally {
        if (buf != null) {
            ReferenceCountUtil.safeRelease(buf);
        }
    }
}

From source file:org.apache.hadoop.hbase.security.SaslWrapHandler.java

License:Apache License

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
    if (queue.isEmpty()) {
        return;/*from www.  j  a  v a2  s.c  o m*/
    }
    ByteBuf buf = null;
    try {
        ChannelPromise promise = ctx.newPromise();
        int readableBytes = queue.readableBytes();
        buf = queue.remove(readableBytes, promise);
        byte[] bytes = new byte[readableBytes];
        buf.readBytes(bytes);
        byte[] wrapperBytes = saslClient.wrap(bytes, 0, bytes.length);
        ChannelPromise lenPromise = ctx.newPromise();
        ctx.write(ctx.alloc().buffer(4).writeInt(wrapperBytes.length), lenPromise);
        ChannelPromise contentPromise = ctx.newPromise();
        ctx.write(Unpooled.wrappedBuffer(wrapperBytes), contentPromise);
        PromiseCombiner combiner = new PromiseCombiner();
        combiner.addAll(lenPromise, contentPromise);
        combiner.finish(promise);
        ctx.flush();
    } finally {
        if (buf != null) {
            ReferenceCountUtil.safeRelease(buf);
        }
    }
}

From source file:org.apache.pulsar.common.api.raw.MessageParser.java

License:Apache License

/**
 * Parse a raw Pulsar entry payload and extract all the individual message that may be included in the batch. The
 * provided {@link MessageProcessor} will be invoked for each individual message.
 *///  w w  w. j av a  2 s. co m
public static void parseMessage(TopicName topicName, long ledgerId, long entryId, ByteBuf headersAndPayload,
        MessageProcessor processor) throws IOException {
    MessageMetadata msgMetadata = null;
    ByteBuf payload = headersAndPayload;
    ByteBuf uncompressedPayload = null;
    ReferenceCountedObject<MessageMetadata> refCntMsgMetadata = null;

    try {
        if (!verifyChecksum(topicName, headersAndPayload, ledgerId, entryId)) {
            // discard message with checksum error
            return;
        }

        try {
            msgMetadata = Commands.parseMessageMetadata(payload);
        } catch (Throwable t) {
            log.warn("[{}] Failed to deserialize metadata for message {}:{} - Ignoring", topicName, ledgerId,
                    entryId);
            return;
        }

        if (msgMetadata.getEncryptionKeysCount() > 0) {
            throw new IOException("Cannot parse encrypted message " + msgMetadata + " on topic " + topicName);
        }

        uncompressedPayload = uncompressPayloadIfNeeded(topicName, msgMetadata, headersAndPayload, ledgerId,
                entryId);

        if (uncompressedPayload == null) {
            // Message was discarded on decompression error
            return;
        }

        final int numMessages = msgMetadata.getNumMessagesInBatch();
        refCntMsgMetadata = new ReferenceCountedObject<>(msgMetadata, (x) -> x.recycle());

        if (numMessages == 1 && !msgMetadata.hasNumMessagesInBatch()) {
            processor.process(RawMessageImpl.get(refCntMsgMetadata, null, uncompressedPayload.retain(),
                    ledgerId, entryId, 0));
        } else {
            // handle batch message enqueuing; uncompressed payload has all messages in batch
            receiveIndividualMessagesFromBatch(refCntMsgMetadata, uncompressedPayload, ledgerId, entryId,
                    processor);
        }

    } finally {
        ReferenceCountUtil.safeRelease(uncompressedPayload);
        ReferenceCountUtil.safeRelease(refCntMsgMetadata);
    }
}

From source file:org.curioswitch.curiostack.gcloud.storage.StorageClient.java

License:Open Source License

/**
 * Reads the contents of a file from cloud storage. Ownership of the returned {@link ByteBuf} is
 * transferred to the caller, which must release it. The future will complete with {@code null} if
 * the file is not found.//  www  . jav  a2s . c  om
 */
public CompletableFuture<ByteBuf> readFile(String filename, EventLoop eventLoop, ByteBufAllocator alloc) {
    String url = objectUrlPrefix + urlPathSegmentEscaper().escape(filename) + "?alt=media";

    return httpClient.get(url).aggregateWithPooledObjects(eventLoop, alloc).thenApply(msg -> {
        if (msg.status().equals(HttpStatus.NOT_FOUND)) {
            ReferenceCountUtil.safeRelease(msg.content());
            return null;
        }
        if (!msg.status().equals(HttpStatus.OK)) {
            String response = msg.contentUtf8();
            ReferenceCountUtil.safeRelease(msg.content());
            throw new InvalidResponseException("Could not fetch file: " + response);
        }
        HttpData data = msg.content();
        if (data instanceof ByteBufHolder) {
            return ((ByteBufHolder) msg.content()).content();
        } else {
            ByteBuf buf = alloc.buffer(data.length());
            buf.writeBytes(data.array(), data.offset(), data.length());
            return buf;
        }
    });
}

From source file:org.curioswitch.curiostack.gcloud.storage.StorageClient.java

License:Open Source License

private CompletableFuture<Void> sendMutationRequest(HttpMethod method, Object request, String url,
        EventLoop eventLoop, ByteBufAllocator alloc) {
    HttpData data = serializeRequest(request, alloc);

    RequestHeaders headers = RequestHeaders.builder(HttpMethod.POST, url).contentType(MediaType.JSON_UTF_8)
            .build();/*from  w w w. j  a v a 2s  .  c o  m*/
    HttpResponse res = httpClient.execute(headers, data);
    return res.aggregateWithPooledObjects(eventLoop, alloc).handle((msg, t) -> {
        if (t != null) {
            throw new RuntimeException("Unexpected error composing file.", t);
        }
        try {
            if (msg.status().equals(HttpStatus.OK)) {
                return null;
            } else {
                throw new IllegalStateException("Could not compose file: " + msg.content().toStringUtf8());
            }
        } finally {
            ReferenceCountUtil.safeRelease(msg.content());
        }
    });
}

From source file:org.redisson.command.RedisExecutor.java

License:Apache License

protected void free(Object[] params) {
    for (Object obj : params) {
        ReferenceCountUtil.safeRelease(obj);
    }
}

From source file:qunar.tc.qmq.protocol.Datagram.java

License:Apache License

public void release() {
    ReferenceCountUtil.safeRelease(body);
}

From source file:qunar.tc.qmq.store.buffer.MemTableBuffer.java

License:Apache License

@Override
public boolean release() {
    ReferenceCountUtil.safeRelease(buf);
    return true;
}