Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I get available bytes to read from a Socket using Java NIO?

Tags:

java

sockets

nio

I want to know if there are available bytes to be read in a Java NIO Socket. In C/C++ it can be done using something like this:

int available(int fd){
  long readable = 0;

    if (ioctl(fd, FIONREAD, &readable) < 0){
      // error
    }

  return (int) readable;
}

It is possible to do the same operation with Java NIO (using SocketChannel, Selector, Socket, etc.)?

like image 267
logoff Avatar asked Sep 27 '12 11:09

logoff


2 Answers

Can't be done. An API is present, formally speaking, via SocketChannel.socket().getInputStream().available(), but getInputStream() operations will fail on a non-blocking channel, so it can't be used in your circumstance.

EDIT: Now that you've illuminated us a little, what you require still doesn't exist in Java, but as you are in non-blocking mode it doesn't matter in the slightest. Just read into a buffer that is at least as big as your socket receive buffer: the number of bytes read is the number of bytes that could have been read without blocking, which is exactly what you just did.

like image 106
user207421 Avatar answered Oct 15 '22 12:10

user207421


The idea behind NIO is to provide a way to wait for events (e.g. "connection has readable data ready") from any one of multiple connections at the same time, which sounds like exactly what you are looking for. Take a look at java.nio.channels.Selector. You basically register the "readable" event of all the connections you have with this selector object and then call one of three select methods that will wait for an event on one of your connections:

  1. select() - blocks until an event is available (use if your program has nothing else to do)
  2. select(long timeout) - blocks until an event is available or a timeout happens (use if you want to conserve CPU load and increase network responsiveness if it's OK if your program slows down a bit)
  3. selectNow() - returns immediately (use if your program needs to keep running)

Then you use the selectedKeys method to grab a list of all connections that have an event waiting (for example, have data ready to be read). Then just iterate over this list and read from the connections with data only and ignore the other connections that aren't in the list as they have no data available.

This will allow you to check without blocking WHETHER data is available (is connection in the list or not), but not HOW MUCH data is available. But if you then do a read on the connection with data available, it will return immediately without blocking and return as much data as is available, if you've given it a big enough buffer. You can then choose to buffer this data somewhere and make the amount of data in the buffer available, but something tells me you don't really need this anyways and just want to go ahead and process the data.

like image 35
Markus A. Avatar answered Oct 15 '22 12:10

Markus A.