• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Channel.read() implementation  RSS feed

 
Chandramouli Ram
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am reading throgh the Grizzly framework that uses NIO, and I have difficulty understanding the following code snippet extracted from:
com.sun.grizzly.filter.ReadFilter


While the code works just fine, why does it require the loop variable to be >2 to break the while loop?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey i dont have any idea about the Grizzly framework. But most probably the code here is trying to handle a scenario, where select has returned a channel that either is not ready for a read(this should not be the case if the interest set entry is correct for this channel) or the data has been read and processed by some other worker. I am not sure whether this is the correct way of handling. I can understand this logic, if there is a delay between the instant Selector.select() returns and the channel is ready to read, but i dont think that is the case. Selector.select() assures that the channel will be ready to perform an operation for which an interest has been registered.
 
Chandramouli Ram
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitesh Kant:
Hey i dont have any idea about the Grizzly framework. But most probably the code here is trying to handle a scenario, where select has returned a channel that either is not ready for a read(this should not be the case if the interest set entry is correct for this channel) or the data has been read and processed by some other worker. I am not sure whether this is the correct way of handling. I can understand this logic, if there is a delay between the instant Selector.select() returns and the channel is ready to read, but i dont think that is the case. Selector.select() assures that the channel will be ready to perform an operation for which an interest has been registered.


Thanks for your response Nitesh. While this code is extracted from grizzly, the logic is entirely NIO based.
The interest options are set appropriately, channel is ready for reading.
The code:
count = socketChannel.read(byteBuffer)

reads the input on this channel into the byteBuffer, returning the number of bytes read. This returns -1 if the channel has reached end-of-stream. Based on the code I provided earlier, the while loop breaks if count becomes zero twice. This is what I am trying to understand.
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code:
count = socketChannel.read(byteBuffer)

reads the input on this channel into the byteBuffer, returning the number of bytes read. This returns -1 if the channel has reached end-of-stream. Based on the code I provided earlier, the while loop breaks if count becomes zero twice. This is what I am trying to understand.


For a non-blocking channel, a return index of zero means that for that moment, there is nothing to be read. The code reads the socket till it is open (while loop checks for socket.isOpen()) and till the time data is available. What i think (and as the comment suggests just above the loop variable check), even if no data is read at a particular instant, the code does not want to return and again wait for the selector to give it an event callback. Instead, it just wants to try if it can read now! I dont know, from where it found the magic figure of 2 though
All over, without a context, this code looks a little strange to me to say the least. Having said that, i think this code will be a result of *some* analysis.
 
Chandramouli Ram
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitesh Kant:

I dont know, from where it found the magic figure of 2 though
All over, without a context, this code looks a little strange to me to say the least. Having said that, i think this code will be a result of *some* analysis.


Thanks again for your response Nitesh.

The context - while does not clarify the code intricacy that's being discussed- is (as mentioned in my first post): com.sun.grizzly.filter.ReadFilter.
Probably 2 consequent read of "zero" bytes is sufficient to quit the loop!
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Probably 2 consequent read of "zero" bytes is sufficient to quit the loop!

Yeah, may be the coder decided on it by getting a random number using Math.Random

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!