Utsav
Utsav

Reputation:

How do i check if inputstream contains no data?

I am using the javax.comm package to perform read and write operations on the SerialPort.

I have created an object of type InputStream as InputStream in;

My question is ....

  1. Irrespective of data availibility on the SerialPort, in.available() always returns a zero due to which I am not able to decide whether bytes are available or not.

If i use the in.read() directly, it seems to block the execution forever..

Please help...

Awaiting your reply..

You can also mail me on my email address..

In anticipation of your reply....

Utsav

Upvotes: 0

Views: 598

Answers (1)

ZZ Coder
ZZ Coder

Reputation: 75496

This is typical behavior of block I/O. The read() is going to block till some bytes are available, EOF or error. You should create a new thread and just wait for more data.

Don't use available() call because it may create a fast loop and drive up your CPU usage. If you really want do this in a single thread, looking into NIO.

Upvotes: 1

Related Questions