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

[io] CountingInputStream.getCount() often returns invalid values

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0
    • 1.1
    • None
    • None
    • Operating System: Linux
      Platform: PC

    • 33336

    Description

      In all read methods there is code like this:

      int found = super.read(b);
      this.count += found;
      return found;

      or like this:

      this.count++;
      return super.read();

      It is ok until we will reach EOF. In this case super.read() returns -1, and
      decreases count in the first case, and increases in the second. In such case
      count should not be changed. So when we have something like this:

      File file = new File("somefile.txt") //File with text "123" and no newlines

      CountingInputStream cis = new CountingInputStream(new FileInputStream (file));
      BufferedReader reader = new BufferedReader(new InputStreamReader(cis));
      while(reader.read() != -1) {}

      After this code when we call cis.getCount() it will return 2, while number of
      bytes in read file is 3.

      Attachments

        1. ASF.LICENSE.NOT.GRANTED--cis.patch
          3 kB
          Marcelo Liberato

        Activity

          People

            Unassigned Unassigned
            amorfis Pawel Stawicki
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: