From 1931addc59a4e979c1d9b5fa3a267e961c9b7136 Mon Sep 17 00:00:00 2001 From: johnnyv Date: Sat, 4 May 2019 11:56:25 -0400 Subject: [PATCH] Backport fix DIRMINA-1104 see d3ffb4b779912be1e100b40261877e167ace565c --- .../mina/core/buffer/IoBufferHexDumper.java | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) 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