Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 574965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:53:15+00:00 2026-05-13T13:53:15+00:00

I have a BufferedReader (generated by new BufferedReader(new InputStreamReader(process.getInputStream())) ). I’m quite new to

  • 0

I have a BufferedReader (generated by new BufferedReader(new InputStreamReader(process.getInputStream()))). I’m quite new to the concept of a BufferedReader but as I see it, it has three states:

  1. A line is waiting to be read; calling bufferedReader.readLine will return this string instantly.
  2. The stream is open, but there is no line waiting to be read; calling bufferedReader.readLine will hang the thread until a line becomes available.
  3. The stream is closed; calling bufferedReader.readLine will return null.

Now I want to determine the state of the BufferedReader, so that I can determine whether I can safely read from it without hanging my application. The underlying process (see above) is notoriously unreliable and so might have hung; in this case, I don’t want my host application to hang. Therefore I’m implementing a kind of timeout. I tried to do this first with threading but it got horribly complicated.

Calling BufferedReader.ready() will not distinguish between cases (2) and (3) above. In other words, if ready() returns false, it might be that the stream just closed (in other words, my underlying process closed gracefully) or it might be that the underlying process hung.

So my question is: how do I determine which of these three states my BufferedReader is in without actually calling readLine? Unfortunately I can’t just call readLine to check this, as it opens my app up to a hang.

I am using JDK version 1.5.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:53:15+00:00Added an answer on May 13, 2026 at 1:53 pm

    Finally I found a solution to this. Most of the answers here rely on threads, but as I specified earlier, I am looking for a solution which doesn’t require threads. However, my basis was the process. What I found was that processes seem to exit if both the output (called “input”) and error streams are empty and closed. This makes sense if you think about it.

    So I just polled the output and error streams and also tried to determine if the process had exited or not. Below is a rough copy of my solution.

    public String readLineWithTimeout(Process process, long timeout) throws IOException, TimeoutException {
      BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
      BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
      boolean finished = false;
      long startTime = 0;
      while (!finished) {
        if (output.ready()) {
          return output.readLine();
        } else if (error.ready()) {
          error.readLine();
        } else {
          try {
            process.exitValue();
            return null;
          } catch (IllegalThreadStateException ex) {
            //Expected behaviour
          }
        }
        if (startTime == 0) {
          startTime = System.currentTimeMills();
        } else if (System.currentTimeMillis() > startTime + timeout) {
          throw new TimeoutException();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 446k
  • Answers 446k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this: int el = [electric integerValue]; int wb =… May 15, 2026 at 7:07 pm
  • Editorial Team
    Editorial Team added an answer Other guys have corrected your mistake, but I thought you… May 15, 2026 at 7:07 pm
  • Editorial Team
    Editorial Team added an answer If you don't want to have a ParentId property, you… May 15, 2026 at 7:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.