Course contentsShow
← Java
Lesson 1964 of 2,87040. File I/O: Character StreamsFree lesson

The ready() Method: Non-Blocking Checks

Learning when a Reader is ready to read without blocking and how this differs from InputStream.available().

The ready() Method: Non-Blocking Checks

You'll learn how to check if a Reader is ready to provide data without causing your program to wait.

The Core Problem

When you call read() on a Reader, your program might block—it pauses and waits until data becomes available. Sometimes you want to know: "Is there data ready right now?" That's where ready() comes in.

What ready() Does

The ready() method returns a boolean:

  • true means the next read() call is guaranteed not to block—data is immediately available
  • false means reading might block (but doesn't guarantee it will)
Reader reader = // ... some Reader
if (reader.ready()) {
    int data = reader.read(); // Won't block
    // Process data...
} else {
    // Do something else instead of waiting
}

How It Differs from InputStream.available()

With byte streams, available() returns an integer—the number of bytes ready to read. With character streams, ready() returns a boolean—just a yes/no answer. This reflects the complexity of character encoding: a Reader might have buffered bytes internally but need more bytes to complete a valid character.

Practical Use

ready() is useful when you want non-blocking I/O behavior—checking if input is available before attempting to read, allowing your program to do other work if nothing's ready yet.

Key Takeaway:
Use ready() to check if a Reader can provide data immediately without blocking; it returns true when the next read() is guaranteed non-blocking, unlike available() which returns a byte count for InputStream.