Search in sources :

Example 11 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project reactor-netty by reactor.

the class ReactorNetty method toPrettyHexDump.

/**
 * Pretty hex dump will be returned when the object is {@link ByteBuf} or {@link ByteBufHolder}
 */
public static String toPrettyHexDump(Object msg) {
    Objects.requireNonNull(msg, "msg");
    String result;
    if (msg instanceof ByteBufHolder && !Objects.equals(Unpooled.EMPTY_BUFFER, ((ByteBufHolder) msg).content())) {
        ByteBuf buffer = ((ByteBufHolder) msg).content();
        result = "\n" + ByteBufUtil.prettyHexDump(buffer);
    } else if (msg instanceof ByteBuf) {
        result = "\n" + ByteBufUtil.prettyHexDump((ByteBuf) msg);
    } else {
        result = msg.toString();
    }
    return result;
}
Also used : ByteBufHolder(io.netty.buffer.ByteBufHolder) ByteBuf(io.netty.buffer.ByteBuf)

Example 12 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project netty by netty.

the class DnsNameResolverContext method buildAliasMap.

private static Map<String, String> buildAliasMap(DnsResponse response) {
    final int answerCount = response.count(DnsSection.ANSWER);
    Map<String, String> cnames = null;
    for (int i = 0; i < answerCount; i++) {
        final DnsRecord r = response.recordAt(DnsSection.ANSWER, i);
        final DnsRecordType type = r.type();
        if (type != DnsRecordType.CNAME) {
            continue;
        }
        if (!(r instanceof DnsRawRecord)) {
            continue;
        }
        final ByteBuf recordContent = ((ByteBufHolder) r).content();
        final String domainName = decodeDomainName(recordContent);
        if (domainName == null) {
            continue;
        }
        if (cnames == null) {
            cnames = new HashMap<String, String>();
        }
        cnames.put(r.name().toLowerCase(Locale.US), domainName.toLowerCase(Locale.US));
    }
    return cnames != null ? cnames : Collections.<String, String>emptyMap();
}
Also used : DnsRawRecord(io.netty.handler.codec.dns.DnsRawRecord) ByteBufHolder(io.netty.buffer.ByteBufHolder) DnsRecordType(io.netty.handler.codec.dns.DnsRecordType) DnsRecord(io.netty.handler.codec.dns.DnsRecord) ByteBuf(io.netty.buffer.ByteBuf)

Example 13 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project reactor-netty by reactor.

the class ChannelOperationsHandler method channelRead.

@Override
public final void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg == null || msg == Unpooled.EMPTY_BUFFER || msg instanceof EmptyByteBuf) {
        return;
    }
    try {
        ChannelOperations<?, ?> ops = ChannelOperations.get(ctx.channel());
        if (ops != null) {
            ops.onInboundNext(ctx, msg);
        } else {
            if (log.isDebugEnabled()) {
                String loggingMsg = msg.toString();
                if (msg instanceof HttpResponse) {
                    DecoderResult decoderResult = ((HttpResponse) msg).decoderResult();
                    if (decoderResult.isFailure()) {
                        log.debug("Decoding failed: " + msg + " : ", decoderResult.cause());
                    }
                }
                if (msg instanceof ByteBufHolder) {
                    loggingMsg = ((ByteBufHolder) msg).content().toString(Charset.defaultCharset());
                }
                log.debug("{} No ChannelOperation attached. Dropping: {}", ctx.channel().toString(), loggingMsg);
            }
            ReferenceCountUtil.release(msg);
        }
    } catch (Throwable err) {
        Exceptions.throwIfFatal(err);
        exceptionCaught(ctx, err);
        ReferenceCountUtil.safeRelease(msg);
    }
}
Also used : EmptyByteBuf(io.netty.buffer.EmptyByteBuf) HttpResponse(io.netty.handler.codec.http.HttpResponse) DecoderResult(io.netty.handler.codec.DecoderResult) ByteBufHolder(io.netty.buffer.ByteBufHolder)

Example 14 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project reactor-netty by reactor.

the class ChannelOperationsHandler method doWrite.

ChannelFuture doWrite(Object msg, ChannelPromise promise, PublisherSender inner) {
    if (// fastpath
    flushOnEach || // last drained element
    inner == null && pendingWrites.isEmpty() || !ctx.channel().isWritable()) {
        pendingBytes = 0L;
        ChannelFuture future = ctx.write(msg, promise);
        if (flushOnEachWithEventLoop && ctx.channel().isWritable()) {
            scheduleFlush();
        } else {
            ctx.flush();
        }
        return future;
    } else {
        if (msg instanceof ByteBuf) {
            pendingBytes = Operators.addCap(pendingBytes, ((ByteBuf) msg).readableBytes());
        } else if (msg instanceof ByteBufHolder) {
            pendingBytes = Operators.addCap(pendingBytes, ((ByteBufHolder) msg).content().readableBytes());
        } else if (msg instanceof FileRegion) {
            pendingBytes = Operators.addCap(pendingBytes, ((FileRegion) msg).count());
        }
        if (log.isTraceEnabled()) {
            log.trace("{} Pending write size = {}", ctx.channel(), pendingBytes);
        }
        ChannelFuture future = ctx.write(msg, promise);
        if (!ctx.channel().isWritable()) {
            pendingBytes = 0L;
            ctx.flush();
        }
        return future;
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) FileRegion(io.netty.channel.FileRegion) ByteBufHolder(io.netty.buffer.ByteBufHolder) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf)

Example 15 with ByteBufHolder

use of io.netty.buffer.ByteBufHolder in project curiostack by curioswitch.

the class StorageClient method readFile.

/**
 * 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.
 */
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 at " + filename + ": " + 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());
            return buf;
        }
    });
}
Also used : HttpData(com.linecorp.armeria.common.HttpData) ByteBufHolder(io.netty.buffer.ByteBufHolder) ByteBuf(io.netty.buffer.ByteBuf) InvalidResponseException(com.linecorp.armeria.client.InvalidResponseException)

Aggregations

ByteBufHolder (io.netty.buffer.ByteBufHolder)21 ByteBuf (io.netty.buffer.ByteBuf)13 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)5 Test (org.junit.jupiter.api.Test)5 ChannelFuture (io.netty.channel.ChannelFuture)4 DnsRawRecord (io.netty.handler.codec.dns.DnsRawRecord)4 DnsRecord (io.netty.handler.codec.dns.DnsRecord)4 HttpResponse (io.netty.handler.codec.http.HttpResponse)4 DefaultByteBufHolder (io.netty.buffer.DefaultByteBufHolder)3 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)3 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)3 HttpRequest (io.netty.handler.codec.http.HttpRequest)3 ArrayList (java.util.ArrayList)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 EmptyByteBuf (io.netty.buffer.EmptyByteBuf)2 DnsRecordType (io.netty.handler.codec.dns.DnsRecordType)2 Future (io.netty.util.concurrent.Future)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 URISyntaxException (java.net.URISyntaxException)2