Socket question
Here is some code for the thread/process to wait for input to arrive:
while ((input = in.readLine()) !=null)
{
//do stuff
}
I am trying to make it so I can see if the buffer has anything in it. If there is something there, read from it and do something. If the buffer is empty (received nothing from the socket) then stop and do something else.
Basically:
if (in.bufferEmpty() ==false)
{
//do something - socket has received data
}
else
{
//do something else, like, quit
}
I can't seem to do something like that. The input streams don't allow me (from what I can tell) to check on the internals of the buffer. So I have to write my own stream readers, etc, but I fanthom this is difficult to do.
Any help would be great. Thanks.
[1250 byte] By [
dayrinnia] at [2007-10-3 4:34:36]

InputStream.available()
Having said that, there are few if any correct uses of this API. Every program I've ever seen that used it was better off just deleting it.
If you're using blocking I/O you're better off dedicating a thread to reading the socket and putting your other processing somewhere else.
ejpa at 2007-7-14 22:38:16 >

Yes, NIO in non-blocking mode will do that, or InputStream.available(). The main objecttion to available() is that people often write code like this:
while (in.available() == 0)
{
Thread.sleep(2000);
}
int count = in.read(buffer);
// ...
In this code the while block is just literally a waste of time. If you have something constructive to do inside the block, such as polling another socket, by all means go ahead.
ejpa at 2007-7-14 22:38:16 >
