Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Correct use of ReadableByteChannel

Reply
Thread Tools

Correct use of ReadableByteChannel

 
 
Harald Kirsch
Guest
Posts: n/a
 
      06-28-2004
For a piece of code intented to be rather
generic, I thought it would be a good idea
that it reads its input from a
ReadableByteChannel. The main reason to
choose it over an InputStream was that the
channel can be obtained for an InputStream as
well as for a RandomAccessFile.

Now the question:

How do you correctly perform a blocking read
from a ReadableByteChannel given that it does
not necessarily block?

I came up with

ReadableByteChannel source;
ByteBuffer inBuf;
...
int result;
while( 0==(result=source.read(inBuf)) ) Thread.yield();

In case the channel is blocking, the yield()
will never be called. If the channel is
non-blocking, the yield() may be called. But
on a machine without much load, I am afraid
it will generate load because no other thread
is interested in the processor.

Any ideas how this can be done better?

Harald.
 
Reply With Quote
 
 
 
 
iksrazal
Guest
Posts: n/a
 
      06-28-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Harald Kirsch) wrote in message news:<(E-Mail Removed). com>...
> How do you correctly perform a blocking read
> from a ReadableByteChannel given that it does
> not necessarily block?
>
> I came up with
>
> ReadableByteChannel source;
> ByteBuffer inBuf;
> ...
> int result;
> while( 0==(result=source.read(inBuf)) ) Thread.yield();
>
> In case the channel is blocking, the yield()
> will never be called. If the channel is
> non-blocking, the yield() may be called. But
> on a machine without much load, I am afraid
> it will generate load because no other thread
> is interested in the processor.
>
> Any ideas how this can be done better?
>
> Harald.


I don't claim to have a lot of answers here, but I've been looking at
this problem a lot recently. One of the options I'm exploring is using
the emberio nio framework licensed under bsd. The code below does say
its mostly helpful for non-blocking but seemingly that is because it
keeps a read state for retrieval. So may be able to adapt this.
Furthermore, a channel by default is blocking or can be made to ber
blocking by giving a timeout. In any event, this is from emberio:

/**
* Reads data from the underlying channel into the internal
ByteBufer here.
* This attempts to fill up to the limit - and
* returns true if it actually does go up to the limit. Returns
false if it can't
* read all the way up to limit characters. Either way, the read
status is held here
* internally so you can also query it later. This complete/not
complete information
* is most useful for non-blocking readers.
*/
public boolean
read () throws IOException
{
int numBytes = -1;

// Think this little loop is odd? Well it is. The reason for it
// is because TCP stacks have a tendency to activate with only 1
byte
// available, and a subsequent read immediately gets the rest of
the data.
// Very annoying, and this leads to us in the common sense
scenario with
// non-blocking to have to inject multiple READ events for no good
reason.
// This little loop tries the read again if it didn't complete the
first
// time around, and provides a noticable perf boost.
//
// The above of course applies for non-blocking I/O where we
attempt a bit
// more I/O than the socket would indicate. For blocking I/O we
sit around
// snarfing it all.
for (int i = 0; ; i++)
{
if (endpoint.directWritesEnabled())
{
numBytes = channel.read (inBuffer);
}
else
{
synchronized (channel)
{
numBytes = channel.read (inBuffer);
}
}
if (numBytes < 0)
{
endpoint.close (EIOReasonCode.DISCONNECTION);
return (false);
}
else if (!inBuffer.hasRemaining())
{
return (true);
}
if (!endpoint.isBlocking() && i >= 2)
{
break;
}
}
return (!inBuffer.hasRemaining());
}

Lastly, the o'reilly nio book has an example of using
ReadableByteChannel with worker threads.

If you come up with something you think works, could you post it?

HTH

Outsource to an American programmer living in brazil!
http://www.braziloutsource.com/
iksrazal
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simulation result is correct but synthesis result is not correct J.Ram VHDL 7 12-03-2008 01:26 PM
Uploaded File Empty but in correct folder with correct name froil Perl Misc 12 03-02-2006 01:21 PM
Correct White Balance Doesn't Mean Correct Color?? jim evans Digital Photography 28 12-27-2005 05:10 AM
correct or not correct? Dan HTML 7 10-02-2003 10:16 PM
To correct my program. please, check to find errors and correct me. joon Java 1 07-08-2003 06:13 AM



Advertisments