Add helper method that allows to create a ChannelBuffer out of a hex dump String. See #449

This commit is contained in:
norman 2012-07-11 09:41:31 +02:00
parent ab43b9aa11
commit bd1bc534d8
2 changed files with 28 additions and 0 deletions

View File

@ -867,6 +867,19 @@ public final class ChannelBuffers {
return new ReadOnlyChannelBuffer(buffer);
}
/**
* Create a {@link ChannelBuffer} from the given <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a>
*/
public static ChannelBuffer hexDump(String hexString) {
int len = hexString.length();
byte[] hexData = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
hexData[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
+ Character.digit(hexString.charAt(i + 1), 16));
}
return wrappedBuffer(hexData);
}
/**
* Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a>
* of the specified buffer's readable bytes.

View File

@ -28,6 +28,7 @@ import java.util.Map;
import java.util.Map.Entry;
import org.easymock.EasyMock;
import org.jboss.netty.util.CharsetUtil;
import org.junit.Test;
/**
@ -425,4 +426,18 @@ public class ChannelBuffersTest {
// Expected
}
}
/**
* Test for https://github.com/netty/netty/issues/449
*/
@Test
public void testHexDumpOperations() {
ChannelBuffer buffer = ChannelBuffers.copiedBuffer("This is some testdata", CharsetUtil.ISO_8859_1);
String hexDump = ChannelBuffers.hexDump(buffer);
ChannelBuffer buffer2 = ChannelBuffers.hexDump(hexDump);
assertEquals(buffer, buffer2);
String hexDump2 = ChannelBuffers.hexDump(buffer2);
assertEquals(hexDump, hexDump2);
}
}