473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

BufferedReader.read() blocks,when checking if there's any input

Hi everyone ,

i have the following problem , i read data from another application that i started through the runtime.exec() in my Java class. I need to periodically check if there is any data sent from it but when i try to BufferedReader.read() from the stream and the stream is empty the java app blocks ,supposably waiting for either the child process to end or for any data to be sent....

Both scenarios does not work for me and so can anyone please tell me the following:

- how to check if there is any data sent by the cild process without blocking the parent???

thanks a lot in advance , anyone...?
Jun 15 '07 #1
5 6444
dmjpro
2,476 2GB
Hi everyone ,

i have the following problem , i read data from another application that i started through the runtime.exec() in my Java class. I need to periodically check if there is any data sent from it but when i try to BufferedReader.read() from the stream and the stream is empty the java app blocks ,supposably waiting for either the child process to end or for any data to be sent....

Both scenarios does not work for me and so can anyone please tell me the following:

- how to check if there is any data sent by the cild process without blocking the parent???

thanks a lot in advance , anyone...?

The picture is not clear to me....
plz send ur code .....

kind regards,
dmjpro.
Jun 16 '07 #2
Hi, here's some code to make it clear:

String[] command={"prog.exe"};
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);

InputStreamReader new_isr = new InputStreamReader(proc.getInputStream());
BufferedReader new_br = new BufferedReader(new_isr);
int new_line = 0;

if ( (new_line = new_br.read()) != -1)// Here's the problem
System.out.println((char)new_line);

The problem is that new_br.read() blocks when called if there is nothing to read in the stream. Normally the stream would send -1 when closed but i need to keep it open and check periodically if there is something sent in it.

When i try and do it this way the new_br.read() blocks when the stream is empty and thus my program hangs until prog.exe is forcably terminated...

I wantes to know if there is another way to check if there is data sent to the stream.

thanks in advance
Jun 16 '07 #3
JosAH
11,448 Expert 8TB
There was no need to start a new thread for your question. If you want more
attention, simply 'bump' your other thread. The problem is that you're just using
one single thread for reading/writing and to control your child process.

On top of that, you're using a buffered reader which attempts to read line by line
or worse. Your C process writes a last line without the newline at the end. Your
buffered reader keeps on attempting to read more from the child process but
there isn't any. When the child process stops your buffered reader receives
the <eof> condition and un-blocks.

kind regards,

Jos
Jun 16 '07 #4
There was no need to start a new thread for your question. If you want more
attention, simply 'bump' your other thread. The problem is that you're just using
one single thread for reading/writing and to control your child process.

On top of that, you're using a buffered reader which attempts to read line by line
or worse. Your C process writes a last line without the newline at the end. Your
buffered reader keeps on attempting to read more from the child process but
there isn't any. When the child process stops your buffered reader receives
the <eof> condition and un-blocks.

kind regards,

Jos
Thanks to u all, i was aware of the problems and i have already found the solutions:)
Jun 17 '07 #5
JosAH
11,448 Expert 8TB
Thanks to u all, i was aware of the problems and i have already found the solutions:)
Good; care to share your solution with us?

kind regards,

Jos
Jun 17 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dr.Kadzija | last post by:
i have a client-server application. client and server should communicate via tcp sockets. ok, so i use Sockets, PrintWriter and BufferedReader. the problem is that: both client and server will send...
6
by: qazmlp | last post by:
I have an 'if else' statement block like this: if( inputArg != VALID_VALUE ) { return error ; } else { // Do some major operation return success ;
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
6
by: placid | last post by:
Hi all, I have been looking into non-blocking read (readline) operations on PIPES on windows XP and there seems to be no way of doing this. Ive read that you could use a Thread to read from the...
2
by: Rajen | last post by:
Suppose the field length is 25 characters. After entering the 25th character, it should be available to process. Program should not wait for the user to press enter/return key. Thank you.
3
by: GaryDean | last post by:
I have just been through the docs on the Data Access Application blocks and it seems that they complicate things more than make things simple. To me it seems that there is nothing more simple and...
2
by: jyohere | last post by:
I want to know which one will be faster randomAccessFile or Bufferedeader. I am reading a file about 18 MB. For that using randomAccessFile i will read from the beginning of the file the first time...
1
by: sassoon12 | last post by:
hi. Is it possible to use bufferedreader in an applet? if so where am i going wrong here ? im getting the following error: E:\CIS26\Hw1App.java:23: unreported exception java.io.IOException; must be...
22
by: pbd22 | last post by:
Hi. I am building a custom telnet interface and my problem is that I want to read the user input along with the previously written stream. Right now I am logging the user. I have Login:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.