@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; } }
@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; } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof ByteBufHolder) { return data.equals(((ByteBufHolder) o).content()); } return false; }
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; }
/** * 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; } }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof ByteBufHolder) { return data.equals(((ByteBufHolder) o).content()); } return false; }
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; }
public static ByteBuf safeBuffer(ByteBufHolder holder, ByteBufAllocator allocator) { return safeBuffer(holder.content(), allocator); }
/** * Generates the default log message of the specified event whose argument is a {@link ByteBufHolder}. */ private static String formatByteBufHolder(ChannelHandlerContext ctx, String eventName, ByteBufHolder msg) { String chStr = ctx.channel().toString(); String msgStr = msg.toString(); ByteBuf content = msg.content(); int length = content.readableBytes(); if (length == 0) { StringBuilder buf = new StringBuilder(chStr.length() + 1 + eventName.length() + 2 + msgStr.length() + 4); buf.append(chStr).append(' ').append(eventName).append(", ").append(msgStr).append(", 0B"); return buf.toString(); } else { int rows = length / 16 + (length % 15 == 0? 0 : 1) + 4; StringBuilder buf = new StringBuilder( chStr.length() + 1 + eventName.length() + 2 + msgStr.length() + 2 + 10 + 1 + 2 + rows * 80); buf.append(chStr).append(' ').append(eventName).append(": ") .append(msgStr).append(", ").append(length).append('B').append(NEWLINE); appendPrettyHexDump(buf, content); return buf.toString(); } }
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; }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o instanceof ByteBufHolder) { return data.equals(((ByteBufHolder) o).content()); } return false; }
private long resolveLength(ByteBufHolder message) { if (message instanceof HttpData) { return ((HttpData) message).length(); } else { return message.content().readableBytes(); } }
private long resolveLength(ByteBufHolder message) { if (message instanceof HttpData) { return ((HttpData) message).length(); } else { return message.content().readableBytes(); } }
private String formatByteBufHolder(String eventName, ByteBufHolder msg) { return formatByteBuf(eventName, msg.content()); } }
@Override protected void onData(ByteBufHolder message) { ByteBuf content = message.content(); try { byte[] bytes = ByteBufUtil.getBytes(content); jacksonProcessor.onNext(bytes); } finally { ReferenceCountUtil.release(content); } }
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); } }
@SuppressWarnings("unchecked") private static <T> T retainedDuplicate(ByteBufHolder o) { return (T) o.replace(o.content().retainedDuplicate()); }
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); } }
@Override public HttpData decode(HttpData obj) { if (obj instanceof ByteBufHolder) { decoder.writeInbound(((ByteBufHolder) obj).content()); } else { final ByteBuf compressed = Unpooled.wrappedBuffer(obj.array(), obj.offset(), obj.length()); decoder.writeInbound(compressed); } return HttpData.of(fetchDecoderOutput()); }
protected final ByteBuf toByteBuf(HttpData data) { if (data instanceof ByteBufHolder) { return ((ByteBufHolder) data).content(); } final ByteBuf buf = channel().alloc().directBuffer(data.length(), data.length()); buf.writeBytes(data.array(), data.offset(), data.length()); return buf; } }