[#1238] Correctly log the content of the MessageBuf and not depend on MessageBuf.toString()

Add an extra static method to BufUtil which will convert the content of any MessaBuf implementation to a String
This commit is contained in:
Norman Maurer 2013-04-08 09:38:50 +02:00
parent 2a162eb140
commit d34daebeca
2 changed files with 27 additions and 1 deletions

View File

@ -25,6 +25,7 @@ import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.util.Iterator;
/**
* A collection of utility methods that is related with handling {@link ByteBuf}, {@link MessageBuf}, and their
@ -417,5 +418,29 @@ public final class BufUtil {
return dst.flip().toString();
}
/**
* Return the content of the given {@link MessageBuf} as string representation.
*/
public static String contentToString(MessageBuf<?> buf) {
if (buf.isEmpty()) {
return "[]";
}
Iterator<?> it = buf.iterator();
StringBuilder sb = new StringBuilder();
sb.append('[');
while (it.hasNext()) {
Object msg = it.next();
if (msg == buf) {
sb.append('(' + buf.getClass().getSimpleName() + ')');
} else {
sb.append(msg);
}
if (it.hasNext()) {
sb.append(", ");
}
}
return sb.append(']').toString();
}
private BufUtil() { }
}

View File

@ -15,6 +15,7 @@
*/
package io.netty.handler.logging;
import io.netty.buffer.BufUtil;
import io.netty.buffer.MessageBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
@ -107,6 +108,6 @@ public class MessageLoggingHandler
}
protected String formatBuffer(String message, MessageBuf<Object> buf) {
return message + '(' + buf.size() + "): " + buf;
return message + '(' + buf.size() + "): " + BufUtil.contentToString(buf);
}
}