ByteBufUtil#decodeHexDump

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.
This commit is contained in:
Scott Mitchell 2017-05-30 11:24:34 -07:00
parent 742ee76424
commit b71abcedd1
2 changed files with 47 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import io.netty.util.CharsetUtil;
import io.netty.util.Recycler; import io.netty.util.Recycler;
import io.netty.util.Recycler.Handle; import io.netty.util.Recycler.Handle;
import io.netty.util.concurrent.FastThreadLocal; import io.netty.util.concurrent.FastThreadLocal;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.PlatformDependent; import io.netty.util.internal.PlatformDependent;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
import io.netty.util.internal.SystemPropertyUtil; import io.netty.util.internal.SystemPropertyUtil;
@ -124,6 +125,13 @@ public final class ByteBufUtil {
return HexUtil.hexDump(array, fromIndex, 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 * Calculates the hash code of the specified buffer. This method is
* useful when implementing a new buffer type. * useful when implementing a new buffer type.
@ -869,6 +877,25 @@ public final class ByteBufUtil {
return new String(buf); 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) { private static String hexDump(byte[] array, int fromIndex, int length) {
if (length < 0) { if (length < 0) {
throw new IllegalArgumentException("length: " + length); throw new IllegalArgumentException("length: " + length);

View File

@ -23,12 +23,32 @@ import java.nio.charset.Charset;
import java.util.Random; import java.util.Random;
import static io.netty.buffer.Unpooled.unreleasableBuffer; import static io.netty.buffer.Unpooled.unreleasableBuffer;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
public class ByteBufUtilTest { 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 @Test
public void equalsBufferSubsections() { public void equalsBufferSubsections() {
byte[] b1 = new byte[128]; byte[] b1 = new byte[128];