InputStream vs. ByteChannel as most basic source of bytes

Discussion in 'Java' started by Harald Kirsch, Aug 28, 2003.

  1. The class I am designing currently processes bytes in
    the sequence they can be read from a source. Now I am
    looking for the most basic byte source provided by
    the Java library.

    Before nio was introduced, InputStream was the most basic
    general source of bytes (apart from byte[]). Now there is
    also ReadableByteChannel, which seems to be even more
    basic, mainly because it has fewer methods.

    But then there is non-blocking behaviour...

    Given that I choose ReadableByteChannel as the input source
    for my new class, what is the best way for it to handle
    non-blocking I/O, in particular the case where a read() returns
    nothing although EOF is not yet reached. I could:

    1) Write in the docs that my new class does not work with
    non-blocking I/O. (Hmmpf)

    2) Idle waiting like:
    while( channel.read()==0 ) Thread.sleep(bbrrrzzzzz);
    This is double Hmmpf.

    3) Cooperative waiting:
    while( channel.read()==0 ) Thread.yield();
    This seems to me to be a useful option.

    4) Read all the documentation about Selector and let
    my new class stick a selector on top of the channel.

    Since the class is not intended to multiplex different input
    sources itself, (4) is probably an overkill resulting internally
    exactly in (3). But this is only a guess.

    Any ideas whether (4) could prove beneficial in the future or why
    (3) may be all wrong anyway?

    Thanks,
    Harald.
    Harald Kirsch, Aug 28, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H.MuthuKumaraRajan
    Replies:
    3
    Views:
    430
    H.MuthuKumaraRajan
    Feb 4, 2004
  2. bugbear
    Replies:
    1
    Views:
    3,553
    Mart?n.
    Nov 12, 2004
  3. R
    Replies:
    5
    Views:
    2,102
    Kevin McMurtrie
    Mar 13, 2005
  4. maurora
    Replies:
    2
    Views:
    306
  5. xkenneth
    Replies:
    8
    Views:
    330
    Bruno Desthuilliers
    Feb 6, 2008
Loading...

Share This Page