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;
}
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();
}
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);
}
}
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;
}
}
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;
}
});
}
Aggregations