Uploaded image for project: 'Commons Codec'
  1. Commons Codec
  2. CODEC-305

Base16InputStream skips characters

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.15
    • 1.16
    • None
    • adopt openjdk 11

      windows 10

    Description

      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:

      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;
          }
          
      } 

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              flgps Florian
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: