Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under what conditions does Java's Scanner.hasNextLine() block?

Tags:

The javadoc for Scanner.hasNextLine() states:

Returns true if there is another line in the input of this scanner. This method may block while waiting for input. The scanner does not advance past any input.

Under what conditions will the method block?

like image 428
smwikipedia Avatar asked Mar 24 '15 14:03

smwikipedia


2 Answers

It depends on the source that the Scanner gets the input from.

For example, if it's a file, the entire input is available, so hasNextLine() wouldn't block (since it can know with certainty when the end of the file is reached and there's no more input.

On the other hand, if the source is standard input, there can always be more input - the user can always type more input - so hasNextLine() would block until the user types in a new line of input.

like image 63
Eran Avatar answered Oct 21 '22 05:10

Eran


How to decide if it will block?

To decide if hasNextLine will block or not is unfortunately not a supported use case.

This is because the underlying sources doesn't always provide an API for peeking in the stream. Put differently, the implementation of hasNextLine calls methods that themselves may block so the problem is sort of inherent.


So, what to do?

If this is indeed a required use case, I would recommend one of the following approaches:

  • Make sure the conditions are right for the hasNextLine. Only provide the scanner with sources that have a definite end (such as a file or string) and never an "open ended" input such as System.in.

    If this is part of an API you could wrap the scanner in your own class that only exposes "safe" constructors.

  • Roll your own class from scratch that does have a willHasNextLineBlock type of method. This could probably be implemented somewhat robustly using InputStream.available.


Under the category of super ugly workarounds we find:

  • Making an attempt at calling hasNextLine in a separate thread and see if it returns within reasonable time, as follows:

    boolean wouldBlock = false;
    Thread t = new Thread(() -> s.hasNextLine());
    t.start();
    try {
        t.join(100);
    } catch (InterruptedException e) {
        wouldBlock = true;
    }
    
  • Use a custom input stream (something like a peekable stream that one could tap into before calling hasNextLine. Something like the this

    CustomStream wrapped = new CustomStream(originalSource)
    Scanner s = new Scanner(wrapped);
    ...
    if (wrapped.hasNextLine())
        // s.hasNextLine would not block
    else
        // s.hasNextLine would block
    

    (Note however that this is somewhat unsafe, since the scanner may have buffered some data from the CustomStream.)

like image 44
aioobe Avatar answered Oct 21 '22 07:10

aioobe