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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.