Skip to content

Commit

Permalink
ByteBufUtil#decodeHexDump
Browse files Browse the repository at this point in the history
Motivation:
ByteBufUtil provides a hexDump method. For debugging purposes it is often useful to decode that hex dump to get the original content, but no such method exists.

Modifications:
- Add ByteBufUtil#decodeHexDump

Result:
ByteBufUtil#decodeHexDump is available to make debugging easier.
  • Loading branch information
Scottmitch committed May 30, 2017
1 parent 742ee76 commit b71abce
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
27 changes: 27 additions & 0 deletions buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.netty.util.Recycler;
import io.netty.util.Recycler.Handle;
import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil;
import io.netty.util.internal.SystemPropertyUtil;
Expand Down Expand Up @@ -124,6 +125,13 @@ public static String hexDump(byte[] array, int fromIndex, int length) {
return HexUtil.hexDump(array, fromIndex, length);
}

/**
* Decodes a string generated by {@link #hexDump(byte[])}
*/
public static byte[] decodeHexDump(CharSequence hexDump) {
return HexUtil.decodeHexDump(hexDump, 0, hexDump.length());
}

/**
* Calculates the hash code of the specified buffer. This method is
* useful when implementing a new buffer type.
Expand Down Expand Up @@ -869,6 +877,25 @@ private static String hexDump(ByteBuf buffer, int fromIndex, int length) {
return new String(buf);
}

private static byte[] decodeHexDump(CharSequence hexDump, int fromIndex, int length) {
if (length < 0) {
throw new IllegalArgumentException("length: " + length);
}
if (length == 0) {
return EmptyArrays.EMPTY_BYTES;
}

int endIndex = fromIndex + length - 1;
byte[] bytes = new byte[length >>> 1];

for (; fromIndex < endIndex; fromIndex += 2) {
bytes[fromIndex >>> 1] = (byte) ((Character.digit(hexDump.charAt(fromIndex), 16) << 4) +
Character.digit(hexDump.charAt(fromIndex + 1), 16));
}

return bytes;
}

private static String hexDump(byte[] array, int fromIndex, int length) {
if (length < 0) {
throw new IllegalArgumentException("length: " + length);
Expand Down
20 changes: 20 additions & 0 deletions buffer/src/test/java/io/netty/buffer/ByteBufUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,32 @@
import java.util.Random;

import static io.netty.buffer.Unpooled.unreleasableBuffer;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class ByteBufUtilTest {
@Test
public void decodeHexEvenLength() {
decodeHex(256);
}

@Test
public void decodeHexOddLength() {
decodeHex(257);
}

private static void decodeHex(int len) {
byte[] b = new byte[len];
Random rand = new Random();
rand.nextBytes(b);
String hexDump = ByteBufUtil.hexDump(b);
byte[] decodedBytes = ByteBufUtil.decodeHexDump(hexDump);
assertArrayEquals(b, decodedBytes);
}

@Test
public void equalsBufferSubsections() {
byte[] b1 = new byte[128];
Expand Down

0 comments on commit b71abce

Please sign in to comment.