Author: elecharny
Date: Tue Dec 13 11:29:49 2011
New Revision: 1213652
URL: http://svn.apache.org/viewvc?rev=1213652&view=rev
Log:
o Added a static method to dump a ByteBuffer with a limited number of bytes,
and with a flag for String dump
o Speeded up the translation to a hexa (and consume less memory)
o Added some tests
Modified:
mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java
Modified:
mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java?rev=1213652&r1=1213651&r2=1213652&view=diff
==============================================================================
--- mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
(original)
+++ mina/trunk/core/src/main/java/org/apache/mina/util/ByteBufferDumper.java
Tue Dec 13 11:29:49 2011
@@ -26,43 +26,23 @@ import java.nio.ByteBuffer;
* @author <a href="http://mina.apache.org">Apache MINA Project</a>
*/
public class ByteBufferDumper {
-
- /**
- * The high digits lookup table.
- */
- private static final byte[] highDigits;
-
- /**
- * The low digits lookup table.
- */
- private static final byte[] lowDigits;
-
+ /** Hex chars */
+ private static final byte[] HEX_CHAR = new byte[]
+ { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
'D', 'E', 'F' };
+
/**
- * Initialize lookup tables.
+ * Dump the content of a IoBuffer
+ *
+ * @param buffer The IoBuffer to dump
+ * @return A string representing the IoBuffer content
*/
- static {
- 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];
-
- for (i = 0; i < 256; i++) {
- high[i] = digits[i >>> 4];
- low[i] = digits[i & 0x0F];
- }
-
- highDigits = high;
- lowDigits = low;
- }
-
public static String dump(IoBuffer buffer) {
StringBuilder sb = new StringBuilder();
boolean isFirst = true;
for (int i = 0; i < buffer.limit(); i++) {
- int byteValue = buffer.get(i) & 0xFF;
+ byte byteValue = buffer.get(i);
if (isFirst) {
isFirst = false;
@@ -70,51 +50,62 @@ public class ByteBufferDumper {
sb.append(' ');
}
- sb.append("0x");
- sb.append((char) highDigits[byteValue]);
- sb.append((char) lowDigits[byteValue]);
+ sb.append( new String( new byte[]
+ { '0', 'x', HEX_CHAR[( byteValue & 0x00F0 ) >> 4],
HEX_CHAR[byteValue & 0x000F] } ));
}
return sb.toString();
}
-
- public static String dump(ByteBuffer buffer) {
+
+ /**
+ * Dump the content of the given ByteBuffer, up to a number of bytes. If
the
+ * toAscii flag is set to <code>true</code>, this method will try to
convert
+ * the bytes to a String
+ *
+ * @param buffer The buffer to dump
+ * @param nbBytes The number of bytes to dump (-1 for all of them)
+ * @param toAscii If we want to write the message as a String
+ * @return A dump of this ByteBuffer
+ */
+ public static String dump(ByteBuffer buffer, int nbBytes, boolean toAscii)
{
byte[] data = buffer.array();
int start = buffer.position();
- int size = buffer.remaining();
+ int size = Math.min(buffer.remaining(), nbBytes >= 0?nbBytes :
Integer.MAX_VALUE);
+ int length = buffer.remaining();
// is not ASCII printable ?
boolean binaryContent = false;
- for (int i = start; i < size; i++) {
- byte b = data[i];
-
- if ((b < 32 || b > 126) && b != 13 && b != 10) {
- binaryContent = true;
- break;
+ if (toAscii) {
+ for (int i = start; i < size; i++) {
+ byte b = data[i];
+
+ if (((b < 32) || (b > 126)) && (b != 13) && (b != 10)) {
+ binaryContent = true;
+ break;
+ }
}
}
- if (binaryContent) {
- StringBuilder out = new StringBuilder(size * 3 + 24);
- out.append("ByteBuffer[len=").append(size).append(",bytes='\n");
+ if (!toAscii || binaryContent) {
+ StringBuilder out = new StringBuilder(size * 3 + 30);
+ out.append("ByteBuffer[len=").append(length).append(",bytes='");
// fill the first
int byteValue = data[start] & 0xFF;
- out.append((char) highDigits[byteValue]);
- out.append((char) lowDigits[byteValue]);
+ boolean isFirst = true;
// and the others, too
- for (int i = start + 1; i < size; i++) {
- if (i%16 == 0) {
- out.append('\n');
+ for (int i = start; i < size; i++) {
+ if (isFirst) {
+ isFirst = false;
} else {
out.append(' ');
}
byteValue = data[i] & 0xFF;
- out.append((char) highDigits[byteValue]);
- out.append((char) lowDigits[byteValue]);
+ out.append( new String( new byte[]
+ { '0', 'x', HEX_CHAR[( byteValue & 0x00F0 ) >> 4],
HEX_CHAR[byteValue & 0x000F] } ));
}
out.append("']");
@@ -124,7 +115,7 @@ public class ByteBufferDumper {
} else {
StringBuilder sb = new StringBuilder(size);
sb.append("ByteBuffer[len=")
- .append(buffer.remaining())
+ .append(length)
.append(",str='")
.append(new String(data, start, size))
.append("']");
@@ -133,4 +124,14 @@ public class ByteBufferDumper {
}
}
+ /**
+ * Dumps the given buffer. If the buffer contains only ascii, it will write
+ * the buffer content as a String.
+ *
+ * @param buffer The buffer to dump
+ * @return A string representing the buffer content
+ */
+ public static String dump(ByteBuffer buffer) {
+ return dump(buffer, -1, true);
+ }
}
Modified:
mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java?rev=1213652&r1=1213651&r2=1213652&view=diff
==============================================================================
---
mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java
(original)
+++
mina/trunk/core/src/test/java/org/apache/mina/util/ByteBufferDumperTest.java
Tue Dec 13 11:29:49 2011
@@ -27,9 +27,7 @@ import org.junit.Test;
public class ByteBufferDumperTest {
@Test
- public void string_test() {
- ByteBufferDumper dumper = new ByteBufferDumper();
-
+ public void stringTest() {
String toTest = "yopYOP\n\r";
byte[] charData = toTest.getBytes();
assertEquals(toTest.length(), charData.length);
@@ -42,14 +40,14 @@ public class ByteBufferDumperTest {
int remaining = myBuffer.remaining();
int pos = myBuffer.position();
- String dump = dumper.dump(myBuffer);
+ String dump = ByteBufferDumper.dump(myBuffer);
assertEquals("ByteBuffer[len=8,str='" + toTest + "']", dump);
assertEquals(remaining, myBuffer.remaining());
assertEquals(pos, myBuffer.position());
}
@Test
- public void binary_test() {
+ public void binaryTest() {
ByteBuffer myBuffer = ByteBuffer.allocate(4);
myBuffer.put((byte) 0x88);
myBuffer.put((byte) 0x03);
@@ -60,10 +58,21 @@ public class ByteBufferDumperTest {
int pos = myBuffer.position();
String dump = ByteBufferDumper.dump(myBuffer);
System.err.println(dump);
- assertEquals("ByteBuffer[len=3,bytes='\n88 03 FF']", dump);
+ assertEquals("ByteBuffer[len=3,bytes='0x88 0x03 0xFF']", dump);
assertEquals(remaining, myBuffer.remaining());
assertEquals(pos, myBuffer.position());
-
}
+ @Test
+ public void testWithSizeLimit() {
+ ByteBuffer bb = ByteBuffer.allocate(10);
+ bb.put(new byte[]{0x01, (byte)0x8F, 0x04, 0x7A, (byte)0xc2, 0x23,
(byte)0xA0, 0x08, 0x44});
+ bb.flip();
+
+ assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2']",
ByteBufferDumper.dump(bb, 5, false));
+ assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2']",
ByteBufferDumper.dump(bb, 5, true));
+ assertEquals("ByteBuffer[len=9,str='']", ByteBufferDumper.dump(bb, 0,
true));
+ assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2 0x23
0xA0 0x08 0x44']", ByteBufferDumper.dump(bb, 10, true));
+ assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2 0x23
0xA0 0x08 0x44']", ByteBufferDumper.dump(bb, -1, false));
+ }
}