You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Alex Herbert (Jira)" <ji...@apache.org> on 2019/11/26 16:56:00 UTC

[jira] [Commented] (CODEC-259) Broken direct java.nio.ByteBuffer support in org.apache.commons.codec.binary.Hex

    [ https://issues.apache.org/jira/browse/CODEC-259?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16982672#comment-16982672 ] 

Alex Herbert commented on CODEC-259:
------------------------------------

The current implementation converts a ByteBuffer to a byte[]. This uses the underlying array if present but does not check the remaining number of bytes in the buffer. The conversion can be changed to:
{code:java}
    private static byte[] toByteArray(final ByteBuffer byteBuffer) {
        final int remaining = byteBuffer.remaining();
        // Use the underlying buffer if possible
        if (byteBuffer.hasArray()) {
            final byte[] byteArray = byteBuffer.array();
            if (remaining == byteArray.length) {
                // ***************
                // This will effectively consume the bytes !!!
                // ***************
                byteBuffer.position(remaining);
                return byteArray;
            }
        }
        // Copy the bytes
        final byte[] byteArray = new byte[remaining];
        byteBuffer.get(byteArray);
        return byteArray;
    }
{code}
If I put the above implementation into master then it fails the current tests. The method to set the position to the limit and consume the bytes is something that is not currently assumed to occur. However it would occur if the code to copy the bytes was executed if there was no backing array.

Any opinion on whether the ByteBuffer should have remaining() == 0 after use by the encode or decode methods?

 

> Broken direct java.nio.ByteBuffer support in org.apache.commons.codec.binary.Hex
> --------------------------------------------------------------------------------
>
>                 Key: CODEC-259
>                 URL: https://issues.apache.org/jira/browse/CODEC-259
>             Project: Commons Codec
>          Issue Type: Bug
>    Affects Versions: 1.11, 1.12
>            Reporter: Tomas Shestakov
>            Priority: Major
>             Fix For: 1.14
>
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> java.nio.ByteBuffer support in org.apache.commons.codec.binary.Hex does not work properly for direct ByteBuffer created by ByteBuffer.allocateDirect method and for heap ByteBuffers which arrayOffset() or byteBuffer.position() greater than zero or byteBuffer.remaining() is not equal byteBuffer.array().{color:#660e7a}length{color}:
> This test will produce java.lang.UnsupportedOperationException
> {code:java}
> @Test
> public void testEncodeHexByteBufferEmpty() {
>     assertTrue(Arrays.equals(new char[0], Hex.encodeHex(ByteBuffer.allocateDirect(0))));
> }
> {code}
> This one will fail
> {code:java}
> @Test
> public void testEncodeHexByteBufferHelloWorldLowerCaseHex() {
>     final ByteBuffer b = ByteBuffer.wrap(StringUtils.getBytesUtf8("[Hello World]"), 1, 11);
>     final String expected = "48656c6c6f20576f726c64";
>     char[] actual;
>     actual = Hex.encodeHex(b);
>     assertEquals(expected, new String(actual));
>     actual = Hex.encodeHex(b, true);
>     assertEquals(expected, new String(actual));
>     actual = Hex.encodeHex(b, false);
>     assertFalse(expected.equals(new String(actual)));
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)