You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Florian (Jira)" <ji...@apache.org> on 2022/06/11 23:07:00 UTC

[jira] [Created] (CODEC-305) unfinished bytes break base16 input stream

Florian created CODEC-305:
-----------------------------

             Summary: unfinished bytes break base16 input stream
                 Key: CODEC-305
                 URL: https://issues.apache.org/jira/browse/CODEC-305
             Project: Commons Codec
          Issue Type: Bug
    Affects Versions: 1.15
         Environment: adopt openjdk 11

windows 10
            Reporter: Florian


If the read() method of the input stream which is wrapped by a Base16InputStream returns the following three arrays of data it will skip the last character of the second array:
 * array with an odd length (e.g. "010")
 * array with an even length (e.g "2030")
 * array with an odd length (e.g. "405")

The above data should result in the following bytes [1, 2, 3, 4, 5], however
 * in lenient mode it reads [1, 2, 3, 64]
 * in strict mode it throws an IllegalArgumentException

I have encountered this issue when reading from an java 11 http input stream. In order to test this easier I have created the following class to reproduce the issue:
{code:java}
public class TestInputStream extends InputStream {

    public static void main(String[] args) throws IOException {
        final Base16InputStream stream = new Base16InputStream(
                new TestInputStream(),
                false,
                true,
                CodecPolicy.STRICT
        );
        int value;
        while ((value = stream.read()) != -1) {
            System.out.println((byte) value);
        }
    }

    private int readCount = 0;

    @Override
    public int read() {
        return 0;
    }

    @Override
    public int read(byte[] output) {
        switch (readCount++) {
            case 0: return writeBytes(output, "010");
            case 1: return writeBytes(output, "2030");
            case 2: return writeBytes(output, "405");
            default: return -1;
        }
    }

    private static int writeBytes(byte[] output, String str) {
        final byte[] data = str.getBytes();
        System.arraycopy(data, 0, output, 0, data.length);
        return data.length;
    }
    
} {code}



--
This message was sent by Atlassian Jira
(v8.20.7#820007)