下面列出了io.netty.buffer.ByteBufUtil#appendPrettyHexDump ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void decodePayload(ByteBuf payload, int payloadLength) {
CommandCodec<?, ?> ctx = inflight.peek();
int startIndex = payload.readerIndex();
try {
if (LOG.isDebugEnabled())
LOG.debug("<<< DECODE " + ctx + " (" + payloadLength + " bytes)");
ctx.decodePayload(payload, payloadLength);
} catch (DB2Exception connex) {
// A common connection error occurred, so don't bother with a hex dump and
// generic error message
ctx.completionHandler.handle(CommandResponse.failure(connex));
} catch (Throwable t) {
int i = payload.readerIndex();
payload.readerIndex(startIndex);
StringBuilder sb = new StringBuilder(
"FATAL: Error parsing buffer at index " + i + " / 0x" + Integer.toHexString(i) + "\n");
ByteBufUtil.appendPrettyHexDump(sb, payload);
LOG.error(sb.toString(), t);
ctx.completionHandler.handle(CommandResponse.failure(t));
} finally {
payload.clear();
payload.release();
}
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf in = (ByteBuf) msg;
if (LOGGER.isLoggable(Level.FINEST)) {
StringBuilder dumper = new StringBuilder();
ByteBufUtil.appendPrettyHexDump(dumper, in);
LOGGER.log(Level.FINEST, "Received from {}: {}", new Object[]{ctx.channel(), dumper});
}
try {
Pdu pdu = PduCodec.decodePdu(in);
ctx.fireChannelRead(pdu);
} catch (Throwable err) {
LOGGER.log(Level.SEVERE, "Error decoding PDU", err);
ReferenceCountUtil.safeRelease(msg);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
ByteBufUtil.appendPrettyHexDump(builder,payload);
return builder.toString();
}
@Override
public void sendOneWayMessage(ByteBuf message, SendResultCallback callback) {
io.netty.channel.Channel _socket = this.socket;
if (_socket == null || !_socket.isOpen()) {
callback.messageSent(new Exception(this + " connection is closed"));
return;
}
if (LOGGER.isLoggable(Level.FINEST)) {
StringBuilder dumper = new StringBuilder();
ByteBufUtil.appendPrettyHexDump(dumper, message);
LOGGER.log(Level.FINEST, "Sending to {}: {}", new Object[]{_socket, dumper});
}
_socket.writeAndFlush(message).addListener(new GenericFutureListener() {
@Override
public void operationComplete(Future future) throws Exception {
if (future.isSuccess()) {
callback.messageSent(null);
} else {
LOGGER.log(Level.SEVERE, this + ": error " + future.cause(), future.cause());
callback.messageSent(future.cause());
close();
}
}
});
unflushedWrites.incrementAndGet();
}
public static String toString(ByteBuf frame) {
FrameType frameType = FrameHeaderCodec.frameType(frame);
int streamId = FrameHeaderCodec.streamId(frame);
StringBuilder payload = new StringBuilder();
payload
.append("\nFrame => Stream ID: ")
.append(streamId)
.append(" Type: ")
.append(frameType)
.append(" Flags: 0b")
.append(Integer.toBinaryString(FrameHeaderCodec.flags(frame)))
.append(" Length: " + frame.readableBytes());
if (frameType.hasInitialRequestN()) {
payload.append(" InitialRequestN: ").append(RequestStreamFrameCodec.initialRequestN(frame));
}
if (frameType == FrameType.REQUEST_N) {
payload.append(" RequestN: ").append(RequestNFrameCodec.requestN(frame));
}
if (FrameHeaderCodec.hasMetadata(frame)) {
payload.append("\nMetadata:\n");
ByteBufUtil.appendPrettyHexDump(payload, getMetadata(frame, frameType));
}
payload.append("\nData:\n");
ByteBufUtil.appendPrettyHexDump(payload, getData(frame, frameType));
return payload.toString();
}