io.netty.buffer.ByteBufHolder Java Examples

The following examples show how to use io.netty.buffer.ByteBufHolder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TrafficReader.java    From LagMonitor with MIT License 7 votes vote down vote up
private void onChannel(Object object, boolean incoming) {
    ByteBuf bytes = null;
    if (object instanceof ByteBuf) {
        bytes = ((ByteBuf) object);
    } else if (object instanceof ByteBufHolder) {
        bytes = ((ByteBufHolder) object).content();
    }

    if (bytes != null) {
        int readableBytes = bytes.readableBytes();
        if (incoming) {
            incomingBytes.add(readableBytes);
        } else {
            outgoingBytes.add(readableBytes);
        }
    }
}
 
Example #2
Source File: FilteredStreamMessageTest.java    From armeria with Apache License 2.0 7 votes vote down vote up
@Test
void notifyCancellation() {
    final PooledHttpData data = PooledHttpData.wrap(newPooledBuffer()).withEndOfStream();
    final DefaultStreamMessage<ByteBufHolder> stream = new DefaultStreamMessage<>();
    stream.write(data);
    stream.close();

    final FilteredStreamMessage<ByteBufHolder, ByteBufHolder> filtered =
            new FilteredStreamMessage<ByteBufHolder, ByteBufHolder>(stream) {
                @Override
                protected ByteBufHolder filter(ByteBufHolder obj) {
                    return obj;
                }
            };
    SubscriptionOptionTest.notifyCancellation(filtered);
}
 
Example #3
Source File: AbstractTrafficShapingHandler.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
/**
 * Calculate the size of the given {@link Object}.
 *
 * This implementation supports {@link ByteBuf} and {@link ByteBufHolder}. Sub-classes may override this.
 * @param msg the msg for which the size should be calculated.
 * @return size the size of the msg or {@code -1} if unknown.
 * 计算给定对象的大小。这个实现支持ByteBuf和ByteBufHolder。子类可以重写。
 */
protected long calculateSize(Object msg) {
    if (msg instanceof ByteBuf) {
        return ((ByteBuf) msg).readableBytes();
    }
    if (msg instanceof ByteBufHolder) {
        return ((ByteBufHolder) msg).content().readableBytes();
    }
    return -1;
}
 
Example #4
Source File: DnsNameResolverContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
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>(min(8, answerCount));
        }

        cnames.put(r.name().toLowerCase(Locale.US), domainName.toLowerCase(Locale.US));
    }

    return cnames != null? cnames : Collections.<String, String>emptyMap();
}
 
Example #5
Source File: DnsNameResolverContext.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private InetAddress parseAddress(DnsRecord r, String name) {
    if (!(r instanceof DnsRawRecord)) {
        return null;
    }
    final ByteBuf content = ((ByteBufHolder) r).content();
    final int contentLen = content.readableBytes();
    if (contentLen != INADDRSZ4 && contentLen != INADDRSZ6) {
        return null;
    }

    final byte[] addrBytes = new byte[contentLen];
    content.getBytes(content.readerIndex(), addrBytes);

    try {
        return InetAddress.getByAddress(
                parent.isDecodeIdn() ? IDN.toUnicode(name) : name, addrBytes);
    } catch (UnknownHostException e) {
        // Should never reach here.
        throw new Error(e);
    }
}
 
Example #6
Source File: JMessageSizeEstimator.java    From Jupiter with Apache License 2.0 6 votes vote down vote up
@Override
public int size(Object msg) {
    if (msg instanceof ByteBuf) {
        return ((ByteBuf) msg).readableBytes();
    }

    if (msg instanceof ByteBufHolder) {
        return ((ByteBufHolder) msg).content().readableBytes();
    }

    if (msg instanceof FileRegion) {
        return 0;
    }

    // jupiter object
    if (msg instanceof PayloadHolder) {
        return ((PayloadHolder) msg).size();
    }

    return unknownSize;
}
 
Example #7
Source File: LoggingHandlerTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldLogByteBufHolderDataRead() throws Exception {
    ByteBufHolder msg = new DefaultByteBufHolder(Unpooled.copiedBuffer("hello", CharsetUtil.UTF_8)) {
        @Override
        public String toString() {
            return "foobar";
        }
    };

    EmbeddedChannel channel = new EmbeddedChannel(new LoggingHandler());
    channel.writeInbound(msg);
    verify(appender).doAppend(argThat(new RegexLogMatcher(".+READ: foobar, 5B$")));

    ByteBufHolder handledMsg = channel.readInbound();
    assertThat(msg, is(sameInstance(handledMsg)));
    handledMsg.release();
    assertThat(channel.readInbound(), is(nullValue()));
}
 
Example #8
Source File: ChunkedWriteHandler.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
PendingWrite(Object msg, ChannelPromise promise) {
    this.msg = msg;
    this.promise = promise;
    if(msg instanceof ByteBuf){
        bytes = ((ByteBuf) msg).readableBytes();
    }else if(msg instanceof ByteBuffer){
        bytes = ((ByteBuffer) msg).remaining();
    }else if(msg instanceof ByteBufHolder){
        bytes = ((ByteBufHolder) msg).content().readableBytes();
    }else if(msg instanceof ChunkedInput){
        bytes = Math.max(((ChunkedInput) msg).length(),0);
    }else if(msg instanceof FileRegion){
        bytes = ((FileRegion) msg).count() - ((FileRegion) msg).position();
    }else {
        bytes = 0;
    }
}
 
Example #9
Source File: ReactorNetty.java    From reactor-netty with Apache License 2.0 6 votes vote down vote up
/**
 * Pretty hex dump will be returned when the object is {@link ByteBuf} or {@link ByteBufHolder}
 */
public static String toPrettyHexDump(Object 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;
}
 
Example #10
Source File: TrafficReader.java    From LagMonitor with MIT License 6 votes vote down vote up
private void onChannel(Object object, boolean incoming) {
    ByteBuf bytes = null;
    if (object instanceof ByteBuf) {
        bytes = ((ByteBuf) object);
    } else if (object instanceof ByteBufHolder) {
        bytes = ((ByteBufHolder) object).content();
    }

    if (bytes != null) {
        int readableBytes = bytes.readableBytes();
        if (incoming) {
            incomingBytes.add(readableBytes);
        } else {
            outgoingBytes.add(readableBytes);
        }
    }
}
 
Example #11
Source File: DnsUtil.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Nullable
public static byte[] extractAddressBytes(DnsRecord record, Logger logger, String logPrefix) {
    final DnsRecordType type = record.type();
    final ByteBuf content = ((ByteBufHolder) record).content();
    final int contentLen = content.readableBytes();

    // Skip invalid records.
    if (type == DnsRecordType.A) {
        if (contentLen != 4) {
            warnInvalidRecord(logger, logPrefix, type, content);
            return null;
        }
    } else if (type == DnsRecordType.AAAA) {
        if (contentLen != 16) {
            warnInvalidRecord(logger, logPrefix, type, content);
            return null;
        }
    } else {
        return null;
    }

    final byte[] addrBytes = new byte[contentLen];
    content.getBytes(content.readerIndex(), addrBytes);
    return addrBytes;
}
 
Example #12
Source File: ChannelOutboundBuffer.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private static long total(Object msg) {
    if (msg instanceof ByteBuf) {
        return ((ByteBuf) msg).readableBytes();
    }
    if (msg instanceof FileRegion) {
        return ((FileRegion) msg).count();
    }
    if (msg instanceof ByteBufHolder) {
        return ((ByteBufHolder) msg).content().readableBytes();
    }
    return -1;
}
 
Example #13
Source File: DefaultMessageSizeEstimator.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int size(Object msg) {
    if (msg instanceof ByteBuf) {
        return ((ByteBuf) msg).readableBytes();
    }
    if (msg instanceof ByteBufHolder) {
        return ((ByteBufHolder) msg).content().readableBytes();
    }
    if (msg instanceof FileRegion) {
        return 0;
    }
    return unknownSize;
}
 
Example #14
Source File: AccessLogHandler.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
	if (msg instanceof HttpResponse) {
		final HttpResponse response = (HttpResponse) msg;
		final HttpResponseStatus status = response.status();

		if (status.equals(HttpResponseStatus.CONTINUE)) {
			//"FutureReturnValueIgnored" this is deliberate
			ctx.write(msg, promise);
			return;
		}

		final boolean chunked = HttpUtil.isTransferEncodingChunked(response);
		accessLog.status(status.codeAsText())
		         .chunked(chunked);
		if (!chunked) {
			accessLog.contentLength(HttpUtil.getContentLength(response, -1));
		}
	}
	if (msg instanceof LastHttpContent) {
		accessLog.increaseContentLength(((LastHttpContent) msg).content().readableBytes());
		ctx.write(msg, promise.unvoid())
		   .addListener(future -> {
		       if (future.isSuccess()) {
		           accessLog.log();
		       }
		   });
		return;
	}
	if (msg instanceof ByteBuf) {
		accessLog.increaseContentLength(((ByteBuf) msg).readableBytes());
	}
	if (msg instanceof ByteBufHolder) {
		accessLog.increaseContentLength(((ByteBufHolder) msg).content().readableBytes());
	}
	//"FutureReturnValueIgnored" this is deliberate
	ctx.write(msg, promise);
}
 
Example #15
Source File: LengthLimitingContentPreviewer.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static ByteBuf duplicateData(HttpData httpData, int length) {
    if (httpData instanceof ByteBufHolder) {
        final ByteBuf content = ((ByteBufHolder) httpData).content();
        if (content.readableBytes() == length) {
            return content.retainedDuplicate();
        }
        return content.retainedSlice(content.readerIndex(), length);
    } else {
        return Unpooled.wrappedBuffer(httpData.array(), 0, length);
    }
}
 
Example #16
Source File: HttpDataFile.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected HttpResponse doRead(ResponseHeaders headers, long length,
                              Executor fileReadExecutor, ByteBufAllocator alloc) {
    if (content instanceof ByteBufHolder) {
        final ByteBufHolder holder = (ByteBufHolder) content;
        return HttpResponse.of(headers, (HttpData) holder.retainedDuplicate());
    } else {
        return HttpResponse.of(headers, content);
    }
}
 
Example #17
Source File: Http1ObjectEncoder.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static ByteBuf dataChunk(HttpData data, int offset, int chunkSize) {
    if (data instanceof ByteBufHolder) {
        final ByteBuf buf = ((ByteBufHolder) data).content();
        return buf.retainedSlice(offset, chunkSize);
    } else {
        return Unpooled.wrappedBuffer(data.array(), offset, chunkSize);
    }
}
 
Example #18
Source File: PooledObjects.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Converts the given object to an unpooled copy and releases the given object.
 */
public static <T> T toUnpooled(T o) {
    if (o instanceof ByteBufHolder) {
        o = copyAndRelease((ByteBufHolder) o);
    } else if (o instanceof ByteBuf) {
        o = copyAndRelease((ByteBuf) o);
    }
    return o;
}
 
Example #19
Source File: PooledObjects.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static <T> T copyAndRelease(ByteBufHolder o) {
    try {
        final ByteBuf content = Unpooled.wrappedBuffer(ByteBufUtil.getBytes(o.content()));
        @SuppressWarnings("unchecked")
        final T copy = (T) o.replace(content);
        return copy;
    } finally {
        ReferenceCountUtil.safeRelease(o);
    }
}
 
Example #20
Source File: FileService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Cache<PathAndEncoding, AggregatedHttpFile> newCache(String cacheSpec) {
    final Caffeine<Object, Object> b = Caffeine.from(cacheSpec);
    b.recordStats()
     .removalListener((RemovalListener<PathAndEncoding, AggregatedHttpFile>) (key, value, cause) -> {
         if (value != null) {
             final HttpData content = value.content();
             if (content instanceof ByteBufHolder) {
                 ((ByteBufHolder) content).release();
             }
         }
     });
    return b.build();
}
 
Example #21
Source File: AbstractStreamDecoderTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void notEmpty() {
    final StreamDecoder decoder = newDecoder();
    final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
    buf.writeBytes(PAYLOAD);
    final HttpData data = decoder.decode(PooledHttpData.wrap(buf));
    assertThat(buf.refCnt()).isZero();
    assertThat(data).isInstanceOfSatisfying(ByteBufHolder.class, d -> assertThat(d.refCnt()).isEqualTo(1));
    ((ByteBufHolder) data).release();
}
 
Example #22
Source File: LoggingHandler.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
protected String formatMessage(String eventName, Object msg) {
    if (msg instanceof ByteBuf) {
        return formatByteBuf(eventName, (ByteBuf) msg);
    } else if (msg instanceof ByteBufHolder) {
        return formatByteBufHolder(eventName, (ByteBufHolder) msg);
    } else {
        return formatNonByteBuf(eventName, msg);
    }
}
 
Example #23
Source File: ChunkedWriteHandler.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private static int amount(Object msg) {
    if (msg instanceof ByteBuf) {
        return ((ByteBuf) msg).readableBytes();
    }
    if (msg instanceof ByteBufHolder) {
        return ((ByteBufHolder) msg).content().readableBytes();
    }
    return 1;
}
 
Example #24
Source File: AbstractTrafficShapingHandler.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the size of the given {@link Object}.
 *
 * This implementation supports {@link ByteBuf} and {@link ByteBufHolder}. Sub-classes may override this.
 *
 * @param msg
 *            the msg for which the size should be calculated.
 * @return size the size of the msg or {@code -1} if unknown.
 */
protected long calculateSize(Object msg) {
    if (msg instanceof ByteBuf) {
        return ((ByteBuf) msg).readableBytes();
    }
    if (msg instanceof ByteBufHolder) {
        return ((ByteBufHolder) msg).content().readableBytes();
    }
    return -1;
}
 
Example #25
Source File: AbstractStreamDecoderTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
public void empty_pooled() {
    final StreamDecoder decoder = newDecoder();
    final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer();
    final HttpData data = decoder.decode(PooledHttpData.wrap(buf));
    assertThat(buf.refCnt()).isZero();

    // Even for a pooled empty input, the result is unpooled since there's no point in pooling empty
    // buffers.
    assertThat(data).isNotInstanceOf(ByteBufHolder.class);
}
 
Example #26
Source File: ByteBufHolderAdapter.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public int readBytes(ByteBufHolder chunk, byte[] arr, int off, int len) {
    ByteBuf buf = chunk.content();
    int avail = buf.readableBytes();
    if (avail == 0) {
        return ChunkedInputStream.EOF;
    }
    int readed = Math.min(len, avail);
    buf.readBytes(arr, off, readed);
    return readed;
}
 
Example #27
Source File: ByteBufHolderAdapter.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public int readByte(ByteBufHolder chunk) {
    ByteBuf buf = chunk.content();
    if (buf.readableBytes() == 0) {
        return ChunkedInputStream.EOF;
    }
    return buf.readByte();
}
 
Example #28
Source File: HttpClientIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected HttpResponse serve(PooledHttpService delegate, ServiceRequestContext ctx,
                             PooledHttpRequest req) throws Exception {
    final PooledHttpResponse res = delegate.serve(ctx, req);
    final HttpResponseWriter decorated = HttpResponse.streaming();
    res.subscribeWithPooledObjects(new Subscriber<HttpObject>() {
        @Override
        public void onSubscribe(Subscription s) {
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(HttpObject httpObject) {
            if (httpObject instanceof ByteBufHolder) {
                try {
                    decorated.write(HttpData.copyOf(((ByteBufHolder) httpObject).content()));
                } finally {
                    ReferenceCountUtil.safeRelease(httpObject);
                }
            } else {
                decorated.write(httpObject);
            }
        }

        @Override
        public void onError(Throwable t) {
            decorated.close(t);
        }

        @Override
        public void onComplete() {
            decorated.close();
        }
    });
    return decorated;
}
 
Example #29
Source File: HttpServerMetricsHandler.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
	if (msg instanceof HttpRequest) {
		dataReceivedTime = System.nanoTime();
	}

	if (msg instanceof ByteBufHolder) {
		dataReceived += ((ByteBufHolder) msg).content().readableBytes();
	}
	else if (msg instanceof ByteBuf) {
		dataReceived += ((ByteBuf) msg).readableBytes();
	}

	if (msg instanceof LastHttpContent) {
		ChannelOperations<?,?> channelOps = ChannelOperations.get(ctx.channel());
		if (channelOps instanceof HttpServerOperations) {
			HttpServerOperations ops = (HttpServerOperations) channelOps;
			String path = uriTagValue == null ? ops.path : uriTagValue.apply(ops.path);
			String method = ops.method().name();
			recorder.recordDataReceivedTime(path, method, Duration.ofNanos(System.nanoTime() - dataReceivedTime));

			// Always take the remote address from the operations in order to consider proxy information
			recorder.recordDataReceived(ops.remoteAddress(), path, dataReceived);
		}

		dataReceived = 0;
	}

	ctx.fireChannelRead(msg);
}
 
Example #30
Source File: DefaultChannelGroup.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private static Object safeDuplicate(Object message) {
    if (message instanceof ByteBuf) {
        return ((ByteBuf) message).duplicate().retain();
    } else if (message instanceof ByteBufHolder) {
        return ((ByteBufHolder) message).duplicate().retain();
    } else {
        return ReferenceCountUtil.retain(message);
    }
}