Commons IO
  1. Commons IO
  2. IO-277

ReaderInputStream enters infinite loop when it encounters an unmappable character

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.1
    • Fix Version/s: 2.1
    • Component/s: Streams/Writers
    • Labels:
      None

      Description

      The ReaderInputStream.read(byte[] b, int off, int len) method enters an infinite loop when its CharsetEncoder encounters an unmappable character in the input buffer.

      When its CharsetEncoder encounters an unmappable character, the value of CoderResult lastCoderResult.isUnmappable() == true, and Reader.read() is not invoked on the underlying Reader ever again.

      Attaching source file that reproduces this behavior.

      One fix to consider is to call CharsetEncoder.onUnmappableCharacter(CodingErrorAction) in the ReaderInputStream constructor with a value other than the default CodingErrorAction.REPORT. e.g.:

      public ReaderInputStream(Reader reader, Charset charset, int bufferSize) {
      this.reader = reader;
      encoder = charset.newEncoder();
      encoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
      ...

      By replacing the unmappable character with encoder's default replacement character, this effectively prevents the infinite loop from occurring. I'm not sure if that's the ideal behavior, but it seems fairly consistent with what org.apache.commons.io.output.WriterOutputStream does.

        Activity

        No work has yet been logged on this issue.

          People

          • Assignee:
            Niall Pemberton
            Reporter:
            Mike Thomas
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development