Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-714

ReaderInputStream does not call CharsetEncoder.flush(...)

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.8.0
    • 2.12.0
    • Streams/Writers
    • None

    Description

      As defined by the CharsetEncoder documentation the encoding operation consists of:

      1. (reset)
      2. encode
      3. flush

      However, org.apache.commons.io.input.ReaderInputStream does not call flush. This leads to incorrect results for charsets whose flush method appends additional bytes.

      Example:

      // Charset whose CharsetEncoder.flush(...) puts bytes
      Charset charset = Charset.forName("Cp930");
      
      // \u0391: Causes CharsetEncoder.flush(...) to put additional bytes
      String s = "\u0391";
      
      byte[] expected = s.getBytes(charset);
      byte[] actual;
      try (InputStream in = new ReaderInputStream(new StringReader(s), charset)) {
          actual = IOUtils.toByteArray(in);
      }
      
      if (!Arrays.equals(expected, actual)) {
          throw new AssertionError("\n  Expected: " + Arrays.toString(expected) + "\n  Actual:   " + Arrays.toString(actual));
      }
      

      Also make sure to check the result of flush() because OVERFLOW as result is possible. In theory isError() == true might be possible as well, but I don't think any of charset implementations currently return that.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              Marcono1234 Marcono1234
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 1h 20m
                  1h 20m