JDK-4090471 : java.io.InputStreamReader.ready returns false when EOF reached
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1.4,1.3.0
  • Priority: P5
  • Status: Open
  • Resolution: Unresolved
  • OS: generic,solaris_2.6
  • CPU: generic,sparc
  • Submitted: 1997-11-04
  • Updated: 2005-11-21
Description
Here's an example that demonstrates the problem. This program does not exit when FileReader is used. import java.io.*;
 
class Main {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Main <file>");
            System.exit(-1);
        }
        try {
//          BufferedReader in = new BufferedReader(new FileReader(args[0]));
          Reader in = new FileReader(args[0]);
//            Reader in = new BufferedReader(new StringReader("This is a test"));
            char[] buf = new char[512];
            int howmany;
      
      while (true) {
                if (in.ready()) {
                    howmany = in.read(buf);
                    if (howmany <= 0) {
                        break;
                    } else {
                        System.out.println(new String(buf, 0, howmany));
                    }
                } else {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // Thread.currentThread().interr
upt();
                        System.err.println(e);
                        break;
                    }
                }
            }
            in.close(import java.io.*;
 
class Main {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage: java Main <file>");
            System.exit(-1);
        }
        try {
//          BufferedReader in = new BufferedReader(new FileReader(args[0]));
          Reader in = new FileReader(args[0]);
//            Reader in = new BufferedReader(new StringReader("This is a test"));
            char[] buf = new char[512];
            int howmany;
      
      while (true) {
                if (in.ready()) {
                    howmany = in.read(buf);
                    if (howmany <= 0) {
                        break;
                    } else {
                        System.out.println(new String(buf, 0, howmany));
                    }
                } else {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // Thread.currentThread().interr
upt();
                        System.err.println(e);
                        break;
                    }
                }
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Comments
EVALUATION The specifications of the ready methods in both Reader and InputStreamReader do not at present guarantee that true will be returned if EOF has been or is about to be reached. It would be nice to support this, but at present we cannot implement it without adding an "atEOF" or some similar method to InputStream. I'm changing this to an RFE so that it will be reconsidered in some future release. -- mr@eng 4/23/1998
23-04-1998