This is an automated email from the ASF dual-hosted git repository. johnnyv pushed a commit to branch 2.1.X in repository https://gitbox.apache.org/repos/asf/mina.git
The following commit(s) were added to refs/heads/2.1.X by this push: new 6540022 HexDump improvements 6540022 is described below commit 6540022cb71f25faab43b1f0a3cbce461484bb55 Author: Jonathan Valliere <john...@apache.org> AuthorDate: Wed Sep 23 10:33:12 2020 -0400 HexDump improvements --- .../apache/mina/core/buffer/AbstractIoBuffer.java | 22 ------- .../java/org/apache/mina/core/buffer/IoBuffer.java | 34 ++++++++--- .../apache/mina/core/buffer/IoBufferHexDumper.java | 71 +++++++++++----------- .../apache/mina/core/buffer/IoBufferWrapper.java | 21 ------- 4 files changed, 60 insertions(+), 88 deletions(-) diff --git a/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java b/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java index e725f08..cc8c758 100644 --- a/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java +++ b/mina-core/src/main/java/org/apache/mina/core/buffer/AbstractIoBuffer.java @@ -1575,28 +1575,6 @@ public abstract class AbstractIoBuffer extends IoBuffer { * {@inheritDoc} */ @Override - public String getHexDump() { - return this.getHexDump(Integer.MAX_VALUE); - } - - /** - * {@inheritDoc} - */ - @Override - public String getHexDump(int lengthLimit) { - return getHexDump(lengthLimit, false); - } - - @Override - public String getHexDump(int lengthLimit, boolean pretty) { - return (pretty) ? IoBufferHexDumper.getPrettyHexDump(this, this.position(), lengthLimit) - : IoBufferHexDumper.getHexdump(this, lengthLimit); - } - - /** - * {@inheritDoc} - */ - @Override public String getString(CharsetDecoder decoder) throws CharacterCodingException { if (!hasRemaining()) { return ""; diff --git a/mina-core/src/main/java/org/apache/mina/core/buffer/IoBuffer.java b/mina-core/src/main/java/org/apache/mina/core/buffer/IoBuffer.java index 648d56d..844ca96 100644 --- a/mina-core/src/main/java/org/apache/mina/core/buffer/IoBuffer.java +++ b/mina-core/src/main/java/org/apache/mina/core/buffer/IoBuffer.java @@ -1512,28 +1512,42 @@ public abstract class IoBuffer implements Comparable<IoBuffer> { * * @return hexidecimal representation of this buffer */ - public abstract String getHexDump(); + public String getHexDump() { + return this.getHexDump(this.remaining(), false); + } /** - * Return hexdump of this buffer with limited length. + * Returns hexdump of this buffer. The data and pointer are not changed as a + * result of this method call. * - * @param lengthLimit The maximum number of bytes to dump from the current - * buffer position. * @return hexidecimal representation of this buffer */ - public abstract String getHexDump(int lengthLimit); + public String getHexDump(boolean pretty) { + return getHexDump(this.remaining(), pretty); + } /** * Return hexdump of this buffer with limited length. * - * @param lengthLimit The maximum number of bytes to dump from the current - * buffer position. - * - * @param pretty Produces multi-line pretty hex dumps + * @param length The maximum number of bytes to dump from the current buffer + * position. + * @return hexidecimal representation of this buffer + */ + public String getHexDump(int length) { + return getHexDump(length, false); + } + + /** + * Return hexdump of this buffer with limited length. * + * @param length The maximum number of bytes to dump from the current buffer + * position. * @return hexidecimal representation of this buffer */ - public abstract String getHexDump(int lengthLimit, boolean pretty); + public String getHexDump(int length, boolean pretty) { + return (pretty) ? IoBufferHexDumper.getPrettyHexDumpSlice(this, this.position(), length) + : IoBufferHexDumper.getHexDumpSlice(this, this.position(), length); + } // ////////////////////////////// // String getters and putters // 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 1bcf322..14f4582 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 @@ -20,6 +20,7 @@ package org.apache.mina.core.buffer; import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; /** * Provides utility methods to dump an {@link IoBuffer} into a hex formatted @@ -32,21 +33,25 @@ class IoBufferHexDumper { /** * Dumps an {@link IoBuffer} to a hex formatted string. * - * @param in the buffer to dump - * @param length the limit at which hex dumping will stop + * @param buf the buffer to dump + * @param offset the starting position to begin reading the hex dump + * @param length the number of bytes to dump * @return a hex formatted string representation of the <i>in</i> * {@link IoBuffer}. */ - public static String getHexdump(IoBuffer in, int length) { - if (length < 0) { - throw new IllegalArgumentException("length: " + length + " must be non-negative number"); + public static String getHexDumpSlice(final IoBuffer buf, final int offset, final int length) { + if (buf == null) { + throw new IllegalArgumentException(); } - int pos = in.position(); - int rem = in.limit() - pos; - int items = Math.min(rem, length); + if (length < 0 || offset < 0 || offset + length > buf.limit()) { + throw new IndexOutOfBoundsException(); + } + + int pos = offset; + int items = Math.min(offset + length, offset + buf.limit()) - pos; - if (items == 0) { + if (items <= 0) { return ""; } @@ -55,7 +60,7 @@ class IoBufferHexDumper { StringBuilder out = new StringBuilder((items * 3) + 6); for (;;) { - int byteValue = in.get(pos++) & 0xFF; + int byteValue = buf.get(pos++) & 0xFF; out.append((char) hexDigit[(byteValue >> 4) & 0x0F]); out.append((char) hexDigit[byteValue & 0xf]); @@ -66,39 +71,32 @@ class IoBufferHexDumper { } } - if (items != rem) { - out.append("..."); - } - return out.toString(); } /** - * Produces a verbose hex dump from the {@link ReadableBuffer} - * - * @return The formatted String representing the content between position() and - * limit(). - */ - public static final String getPrettyHexDump(final IoBuffer buf) { - return getPrettyHexDump(buf, buf.position(), buf.remaining()); - } - - /** * Produces a verbose hex dump * - * @param start initial position which to read bytes + * @param offset initial position which to read bytes * * @param length number of bytes to display * * @return The formatted String representing the content between (offset) and * (offset+count) */ - public static final String getPrettyHexDump(final IoBuffer buf, final int start, final int length) { - final int len = Math.min(length, buf.limit() - start); + public static final String getPrettyHexDumpSlice(final IoBuffer buf, final int offset, final int length) { + if (buf == null) { + throw new IllegalArgumentException(); + } + + if (length < 0 || offset < 0 || offset + length > buf.limit()) { + throw new IndexOutOfBoundsException(); + } + final int len = Math.min(length, buf.limit() - offset); final byte[] bytes = new byte[len]; - int o = start; + int o = offset; for (int i = 0; i < len; i++) { bytes[i] = buf.get(o++); @@ -109,9 +107,9 @@ class IoBufferHexDumper { sb.append("Source "); sb.append(buf); sb.append(" showing index "); - sb.append(start); + sb.append(offset); sb.append(" through "); - sb.append((start + length)); + sb.append((offset + length)); sb.append("\n"); sb.append(toPrettyHexDump(bytes, 0, bytes.length)); @@ -131,8 +129,12 @@ class IoBufferHexDumper { * @return string hex dump */ public static final String toPrettyHexDump(final byte[] data, final int pos, final int len) { - if ((data == null) || ((pos < 0) | (len < 0)) || ((pos + len) > data.length)) { - throw new IllegalArgumentException("byte[] is null || pos < 0 || len < 0 || pos + len > byte[].length"); + if (data == null) { + throw new IllegalArgumentException(); + } + + if (len < 0 || pos < 0 || pos + len > data.length) { + throw new IndexOutOfBoundsException(); } final StringBuilder b = new StringBuilder(); @@ -141,7 +143,6 @@ class IoBufferHexDumper { for (int i = pos, c = 0, line = 16; i < len; i += line) { b.append(String.format("%06d", Integer.valueOf(c)) + " "); - b.append(toPrettyHexDumpLine(data, i, Math.min((pos + len) - i, line), 8, line)); if ((i + line) < len) { @@ -222,8 +223,8 @@ class IoBufferHexDumper { return b.toString(); } - private static final char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', - 'f' }; + private static final char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', + 'F' }; public static final String toHex(final byte b) { // Returns hex String representation of byte diff --git a/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java b/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java index d0d14dd..49a1201 100644 --- a/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java +++ b/mina-core/src/main/java/org/apache/mina/core/buffer/IoBufferWrapper.java @@ -817,19 +817,6 @@ public class IoBufferWrapper extends IoBuffer { * {@inheritDoc} */ @Override - public String getHexDump() { - return buf.getHexDump(); - } - - @Override - public String getHexDump(int lengthLimit, boolean pretty) { - return buf.getHexDump(lengthLimit, pretty); - } - - /** - * {@inheritDoc} - */ - @Override public String getString(int fieldSize, CharsetDecoder decoder) throws CharacterCodingException { return buf.getString(fieldSize, decoder); } @@ -1222,14 +1209,6 @@ public class IoBufferWrapper extends IoBuffer { * {@inheritDoc} */ @Override - public String getHexDump(int lengthLimit) { - return buf.getHexDump(lengthLimit); - } - - /** - * {@inheritDoc} - */ - @Override public boolean prefixedDataAvailable(int prefixLength) { return buf.prefixedDataAvailable(prefixLength); }