Gergely
Gergely

Reputation: 157

BufferedReader doesn't return

I was given a task to create a java method which reads and returns the first line from the console without invoking System.in.read(byte[]) or System.in.read(byte[],int,int). (The System.in has been modified to throw an IOException if they are called.)

I came up with this solution:

InputStream a = new InputStream(){
    public int read() throws IOException{
        return System.in.read();
    }
};
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(a));
return consoleReader.readLine();

No matter what I write into the console the consoleReader.readLine() method never returns! How can I fix this?

Edit: I must use whatever InputStream System.in has been set to.

Upvotes: 3

Views: 872

Answers (4)

Holger
Holger

Reputation: 298233

The approach of creating a custom InputStream which only implements int read() is going into the right direction, unfortunately, the inherited int read(byte[] b, int off, int len), which is eventually invoked for BufferedReader.readLine is trying to fill the entire buffer, unless the end of stream has been reached.

Therefore, you have to override this method as well, allowing earlier return if there are no more bytes available:

InputStream a = new InputStream(){
    @Override
    public int read() throws IOException {
        return System.in.read();
    }
    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int r=0;
        do {
            int x=read();
            if(x<0) return r==0? -1: r;
            b[off++]=(byte)x;
            r++;
        } while(r<len && System.in.available()>0);
        return r;
    }
};
BufferedReader reader = new BufferedReader(new InputStreamReader(a));
return reader.readLine();

Note that this follows the convention of reading at least one character in every read operation (unless end of stream has been reached). This is what the other I/O classes expect and BufferedReader will call read again, if no complete line has been read yet.

Upvotes: 1

Adisesha
Adisesha

Reputation: 5258

It will return if you provide enough text to fill 8192 bytes which is default char buffer size of BufferedReader. When you call readLine(), it will end up calling a.read(). It should supply at least 8192 bytes.

Upvotes: 0

Sumon Mal
Sumon Mal

Reputation: 71

Here is the answer, no System.in is in use.

    // using Console
    Console console = System.console();
    if (console == null) {
        System.out.println("No console: not in interactive mode!");
        System.exit(0);
    }
     System.out.print(console.readLine());

Upvotes: 0

Sanjit Kumar Mishra
Sanjit Kumar Mishra

Reputation: 1209

Use can write it like this way:

Scanner console = new Scanner(System.in);
System.out.println(console.next());

Upvotes: 0

Related Questions