Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/main/java/org/apache/commons/codec/binary/Hex.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

import org.apache.commons.codec.BinaryDecoder;
import org.apache.commons.codec.BinaryEncoder;
Expand Down Expand Up @@ -209,7 +210,7 @@ protected static char[] encodeHex(final byte[] data, final char[] toDigits) {
* @since 1.11
*/
protected static char[] encodeHex(final ByteBuffer data, final char[] toDigits) {
return encodeHex(data.array(), toDigits);
return encodeHex(bufferToArray(data), toDigits);
}

/**
Expand Down Expand Up @@ -353,7 +354,7 @@ public byte[] decode(final byte[] array) throws DecoderException {
* @since 1.11
*/
public byte[] decode(final ByteBuffer buffer) throws DecoderException {
return decodeHex(new String(buffer.array(), getCharset()).toCharArray());
return decodeHex(new String(bufferToArray(buffer), getCharset()).toCharArray());
}

/**
Expand Down Expand Up @@ -447,7 +448,7 @@ public Object encode(final Object object) throws EncoderException {
if (object instanceof String) {
byteArray = ((String) object).getBytes(this.getCharset());
} else if (object instanceof ByteBuffer) {
byteArray = ((ByteBuffer) object).array();
byteArray = bufferToArray((ByteBuffer) object);
} else {
try {
byteArray = (byte[]) object;
Expand Down Expand Up @@ -487,4 +488,19 @@ public String getCharsetName() {
public String toString() {
return super.toString() + "[charsetName=" + this.charset + "]";
}

private static byte[] bufferToArray(ByteBuffer byteBuffer) {
int size = byteBuffer.remaining();

if (byteBuffer.hasArray()) {
int offset = byteBuffer.arrayOffset() + byteBuffer.position();
return offset == 0 && size == byteBuffer.array().length
? byteBuffer.array()
: Arrays.copyOfRange(byteBuffer.array(), offset, offset + size);
}

final byte[] bytes = new byte[size];
byteBuffer.duplicate().get(bytes);
return bytes;
}
}
23 changes: 12 additions & 11 deletions src/test/java/org/apache/commons/codec/binary/HexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,19 @@ public void testDecodeByteArrayOddCharacters() {

@Test
public void testDecodeByteBufferEmpty() throws DecoderException {
assertTrue(Arrays.equals(new byte[0], new Hex().decode(ByteBuffer.allocate(0))));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to add tests instead of updating each call to allocate with a call to allocateDirect. This is something that could be done either with subclasses (one for allocate, one for allocateDirect) or with parameterizing the test with lambdas.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry but have no time for that

assertTrue(Arrays.equals(new byte[0], new Hex().decode(ByteBuffer.allocateDirect(0))));
}

@Test
public void testDecodeByteBufferObjectEmpty() throws DecoderException {
assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex().decode((Object) ByteBuffer.allocate(0))));
assertTrue(Arrays.equals(new byte[0], (byte[]) new Hex().decode((Object) ByteBuffer.allocateDirect(0))));
}

@Test
public void testDecodeByteBufferOddCharacters() {
final ByteBuffer buffer = ByteBuffer.allocate(1);
final ByteBuffer buffer = ByteBuffer.allocateDirect(1);
buffer.put((byte) 65);
buffer.flip();
try {
new Hex().decode(buffer);
fail("An exception wasn't thrown when trying to decode an odd number of characters");
Expand Down Expand Up @@ -314,12 +315,12 @@ public void testEncodeByteArrayObjectEmpty() throws EncoderException {

@Test
public void testEncodeByteBufferEmpty() {
assertTrue(Arrays.equals(new byte[0], new Hex().encode(ByteBuffer.allocate(0))));
assertTrue(Arrays.equals(new byte[0], new Hex().encode(ByteBuffer.allocateDirect(0))));
}

@Test
public void testEncodeByteBufferObjectEmpty() throws EncoderException {
assertTrue(Arrays.equals(new char[0], (char[]) new Hex().encode((Object) ByteBuffer.allocate(0))));
assertTrue(Arrays.equals(new char[0], (char[]) new Hex().encode((Object) ByteBuffer.allocateDirect(0))));
}

@Test
Expand Down Expand Up @@ -405,13 +406,13 @@ public void testEncodeHexByteArrayZeroes() {

@Test
public void testEncodeHexByteBufferEmpty() {
assertTrue(Arrays.equals(new char[0], Hex.encodeHex(ByteBuffer.allocate(0))));
assertTrue(Arrays.equals(new byte[0], new Hex().encode(ByteBuffer.allocate(0))));
assertTrue(Arrays.equals(new char[0], Hex.encodeHex(ByteBuffer.allocateDirect(0))));
assertTrue(Arrays.equals(new byte[0], new Hex().encode(ByteBuffer.allocateDirect(0))));
}

@Test
public void testEncodeHexByteBufferHelloWorldLowerCaseHex() {
final ByteBuffer b = StringUtils.getByteBufferUtf8("Hello World");
final ByteBuffer b = ByteBuffer.wrap(StringUtils.getBytesUtf8("[Hello World]"), 1, 11);
final String expected = "48656c6c6f20576f726c64";
char[] actual;
actual = Hex.encodeHex(b);
Expand All @@ -424,7 +425,7 @@ public void testEncodeHexByteBufferHelloWorldLowerCaseHex() {

@Test
public void testEncodeHexByteBufferHelloWorldUpperCaseHex() {
final ByteBuffer b = StringUtils.getByteBufferUtf8("Hello World");
final ByteBuffer b = ByteBuffer.wrap(StringUtils.getBytesUtf8("[Hello World]"), 1, 11);
final String expected = "48656C6C6F20576F726C64";
char[] actual;
actual = Hex.encodeHex(b);
Expand All @@ -437,13 +438,13 @@ public void testEncodeHexByteBufferHelloWorldUpperCaseHex() {

@Test
public void testEncodeHex_ByteBufferOfZeroes() {
final char[] c = Hex.encodeHex(ByteBuffer.allocate(36));
final char[] c = Hex.encodeHex(ByteBuffer.allocateDirect(36));
assertEquals("000000000000000000000000000000000000000000000000000000000000000000000000", new String(c));
}

@Test
public void testEncodeHexByteString_ByteBufferOfZeroes() {
final String c = Hex.encodeHexString(ByteBuffer.allocate(36));
final String c = Hex.encodeHexString(ByteBuffer.allocateDirect(36));
assertEquals("000000000000000000000000000000000000000000000000000000000000000000000000", c);
}

Expand Down