diff --git a/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferHexDumper.java b/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferHexDumper.java index 59380b14b..4a96bcd55 100644 --- a/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferHexDumper.java +++ b/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferHexDumper.java @@ -63,45 +63,40 @@ class IoBufferHexDumper { * @param lengthLimit the limit at which hex dumping will stop * @return a hex formatted string representation of the in {@link IoBuffer}. */ - public static String getHexdump(IoBuffer in, int lengthLimit) { - if (lengthLimit == 0) { - throw new IllegalArgumentException("lengthLimit: " + lengthLimit + " (expected: 1+)"); - } + public static String getHexdump(IoBuffer in, int length) { + if (length < 0) { + throw new IllegalArgumentException("length: " + length + " must be non-negative number"); + } - int limit = in.limit(); - int pos = in.position(); - - boolean truncate = limit - pos > lengthLimit; - int size; - if (truncate) { - size = lengthLimit; - } else { - size = limit - pos; - } + int pos = in.position(); + int rem = in.limit() - pos; + int items = Math.min(rem, length); - if (size == 0) { - return "empty"; - } + if (items == 0) { + return ""; + } - StringBuilder out = new StringBuilder(size * 3 + 3); + int lim = pos + items; - // fill the first - int byteValue = in.get(pos++) & 0xFF; - out.append((char) highDigits[byteValue]); - out.append((char) lowDigits[byteValue]); + StringBuilder out = new StringBuilder((items * 3) + 6); - // and the others, too - for (; pos < limit; ) { - out.append(' '); - byteValue = in.get(pos++) & 0xFF; - out.append((char) highDigits[byteValue]); - out.append((char) lowDigits[byteValue]); - } + /* first sequence to align the spaces */{ + int byteValue = in.get(pos++) & 0xFF; + out.append((char) highDigits[byteValue]); + out.append((char) lowDigits[byteValue]); + } - if (truncate) { - out.append("..."); - } + /* loop remainder */for (; pos < lim;) { + out.append(' '); + int byteValue = in.get(pos++) & 0xFF; + out.append((char) highDigits[byteValue]); + out.append((char) lowDigits[byteValue]); + } + + if (items != rem) { + out.append("..."); + } - return out.toString(); + return out.toString(); } } \ No newline at end of file