This is an automated email from the ASF dual-hosted git repository.

johnnyv pushed a commit to branch 2.1
in repository https://gitbox.apache.org/repos/asf/mina.git


The following commit(s) were added to refs/heads/2.1 by this push:
     new d3ffb4b  Adds fix for DIRMINA-1104.  Adds IoBufferHexDumperTest unit 
test to check for regression.  Modifies the IllegalArgumentException routine to 
only throw when the length parameter is less than zero whereas before it only 
checked for zero.  There is no reason why a length of zero should throw an 
exception.
d3ffb4b is described below

commit d3ffb4b779912be1e100b40261877e167ace565c
Author: johnnyv <john...@apache.org>
AuthorDate: Thu Apr 25 12:09:58 2019 -0400

    Adds fix for DIRMINA-1104.  Adds IoBufferHexDumperTest unit test to
    check for regression.  Modifies the IllegalArgumentException routine to
    only throw when the length parameter is less than zero whereas before it
    only checked for zero.  There is no reason why a length of zero should
    throw an exception.
---
 .../apache/mina/core/buffer/IoBufferHexDumper.java | 104 ++++++++++-----------
 .../mina/core/buffer/IoBufferHexDumperTest.java    |  43 +++++++++
 2 files changed, 94 insertions(+), 53 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 59380b1..452498c 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
@@ -41,67 +41,65 @@ class IoBufferHexDumper {
      * Initialize lookup tables.
      */
     static {
-        final byte[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', 
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
+       final byte[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', 
'9', 'A', 'B', 'C', 'D', 'E', 'F' };
 
-        int i;
-        byte[] high = new byte[256];
-        byte[] low = new byte[256];
+       int i;
+       byte[] high = new byte[256];
+       byte[] low = new byte[256];
 
-        for (i = 0; i < 256; i++) {
-            high[i] = digits[i >>> 4];
-            low[i] = digits[i & 0x0F];
-        }
+       for (i = 0; i < 256; i++) {
+           high[i] = digits[i >>> 4];
+           low[i] = digits[i & 0x0F];
+       }
 
-        highDigits = high;
-        lowDigits = low;
+       highDigits = high;
+       lowDigits = low;
     }
 
     /**
      * Dumps an {@link IoBuffer} to a hex formatted string.
      * 
-     * @param in the buffer to dump
-     * @param lengthLimit the limit at which hex dumping will stop
-     * @return a hex formatted string representation of the <i>in</i> {@link 
IoBuffer}.
+     * @param in
+     *            the buffer to dump
+     * @param length
+     *            the limit at which hex dumping will stop
+     * @return a hex formatted string representation of the <i>in</i>
+     *         {@link IoBuffer}.
      */
-    public static String getHexdump(IoBuffer in, int lengthLimit) {
-        if (lengthLimit == 0) {
-            throw new IllegalArgumentException("lengthLimit: " + lengthLimit + 
" (expected: 1+)");
-        }
-
-        int limit = in.limit();
-        int pos = in.position();
-        
-        boolean truncate = limit - pos > lengthLimit;
-        int size;
-        if (truncate) {
-            size = lengthLimit;
-        } else {
-            size = limit - pos;
-        }
-
-        if (size == 0) {
-            return "empty";
-        }
-
-        StringBuilder out = new StringBuilder(size * 3 + 3);
-
-        // fill the first
-        int byteValue = in.get(pos++) & 0xFF;
-        out.append((char) highDigits[byteValue]);
-        out.append((char) lowDigits[byteValue]);
-
-        // and the others, too
-        for (; pos < limit; ) {
-            out.append(' ');
-            byteValue = in.get(pos++) & 0xFF;
-            out.append((char) highDigits[byteValue]);
-            out.append((char) lowDigits[byteValue]);
-        }
-
-        if (truncate) {
-            out.append("...");
-        }
-
-        return out.toString();
+    public static String getHexdump(IoBuffer in, int length) {
+       if (length < 0) {
+           throw new IllegalArgumentException("length: " + length + " must be 
non-negative number");
+       }
+
+       int pos = in.position();
+       int rem = in.limit() - pos;
+       int items = Math.min(rem, length);
+
+       if (items == 0) {
+           return "";
+       }
+
+       int lim = pos + items;
+
+       StringBuilder out = new StringBuilder((items * 3) + 6);
+
+       /* first sequence to align the spaces */{
+           int byteValue = in.get(pos++) & 0xFF;
+           out.append((char) highDigits[byteValue]);
+           out.append((char) lowDigits[byteValue]);
+       }
+
+       /* 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();
     }
 }
\ No newline at end of file
diff --git 
a/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferHexDumperTest.java
 
b/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferHexDumperTest.java
new file mode 100644
index 0000000..5d24874
--- /dev/null
+++ 
b/mina-core/src/test/java/org/apache/mina/core/buffer/IoBufferHexDumperTest.java
@@ -0,0 +1,43 @@
+package org.apache.mina.core.buffer;
+
+import static org.junit.Assert.*;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+public class IoBufferHexDumperTest {
+
+    @Test
+    public void checkHexDumpLength() {
+       IoBuffer buf = IoBuffer.allocate(5000);
+
+       for (int i = 0; i < 20; i++) {
+           buf.putShort((short) 0xF0A0);
+       }
+
+       buf.flip();
+
+//     System.out.println(buf.getHexDump());
+//     System.out.println(buf.getHexDump(20));
+//     System.out.println(buf.getHexDump(50));
+
+       /* special case */
+       assertEquals(0, buf.getHexDump(0).length());
+       
+       /* no truncate needed */
+       assertEquals(buf.limit() * 3 - 1, buf.getHexDump().length());
+       assertEquals((Math.min(300, buf.limit()) * 3) - 1, 
buf.getHexDump(300).length());
+       
+       /* must truncate */
+       assertEquals((7 * 3) + 2, buf.getHexDump(7).length());
+       assertEquals((10 * 3) + 2, buf.getHexDump(10).length());
+       assertEquals((30 * 3) + 2, buf.getHexDump(30).length());
+       
+    }
+}

Reply via email to