What's the Importance of the avaliable() method in InputStream?

Discussion in 'Java' started by res7cxbi@verizon.net, Jan 29, 2006.

  1. Guest

    What's the Importance of the avaliable() method in InputStream? Why
    would i use it?

    And what does available()'s method description in inputstream's javadoc
    mean?: "Returns the number of bytes that can be read (or skipped over)
    from this input stream without blocking by the next caller of a method
    for this input stream."
     
    , Jan 29, 2006
    #1
    1. Advertising

  2. ozgwei Guest

    Note that in the implementation of InputStream, this method always
    return 0. It is intended for BufferedInputStream to return the
    remaining bytes in the buffer.
     
    ozgwei, Jan 29, 2006
    #2
    1. Advertising

  3. Roedy Green Guest

    On 29 Jan 2006 02:00:15 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >And what does available()'s method description in inputstream's javadoc
    >mean?: "Returns the number of bytes that can be read (or skipped over)
    >from this input stream without blocking by the next caller of a method
    >for this input stream."


    you might decide it is not worth your while doing a read, you have
    other fish to fry. You will come back later and try again when there
    is a big enough batch worth the overhead of a read to the OS.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Jan 29, 2006
    #3
  4. wrote:
    > What's the Importance of the avaliable() method in InputStream? Why
    > would i use it?


    Not very important. You probably wouldn't want to use it. If you don't
    understand well exactly what it does (and doesn't) do and when and why
    you might want that, then save yourself grief and pretend that the
    method doesn't exist. It is rarely used by people who do know and
    understand all that.

    > And what does available()'s method description in inputstream's javadoc
    > mean?: "Returns the number of bytes that can be read (or skipped over)
    > from this input stream without blocking by the next caller of a method
    > for this input stream."


    It means that the next read on the input stream will retrieve at least
    that number of bytes, provided that that number or more are requested.
    Furthermore, the read will not block if the number of bytes returned by
    available() is greater than zero. Note, however, that by the time the
    read is actually performed, there may be more bytes available, so a
    return value of zero doesn't necessarily mean that the next read will
    block. Note also that available() will, perforce, return zero when the
    end of the stream has been detected, and that a read on the stream will
    not block in that case, either. A common mistake is to assume that
    available() returning zero is a definite signal of end-of-stream; that
    assumption is false.

    Contrary to ozgwei's comments, available is for more than just
    BufferedInputStream, but he is right about how BufferedInputStream
    implements the method. Note that java.io.InputStream itself is abstract.

    --
    John Bollinger
     
    John C. Bollinger, Jan 29, 2006
    #4
  5. Jim Korman Guest

    On Sun, 29 Jan 2006 12:17:37 GMT, Roedy Green
    <> wrote:

    >On 29 Jan 2006 02:00:15 -0800, wrote, quoted or
    >indirectly quoted someone who said :
    >
    >>And what does available()'s method description in inputstream's javadoc
    >>mean?: "Returns the number of bytes that can be read (or skipped over)
    >>from this input stream without blocking by the next caller of a method
    >>for this input stream."

    >
    >you might decide it is not worth your while doing a read, you have
    >other fish to fry. You will come back later and try again when there
    >is a big enough batch worth the overhead of a read to the OS.


    One place that I found it very useful is when using a
    SerialPortEventListener for "interrupt" driven serial I/O.
    The event only tells you that data is available, not how much.
    The Listener code can check InputStream.available(), read that
    much and quickly exit.

    Jim
     
    Jim Korman, Jan 29, 2006
    #5
  6. wrote:

    > What's the Importance of the avaliable() method in InputStream? Why
    > would i use it?


    It tells you the number of bytes in the current input buffer, as opposed to
    the total number of bytes waiting in the stream itself.

    > And what does available()'s method description in inputstream's javadoc
    > mean?: "Returns the number of bytes that can be read (or skipped over)
    > from this input stream without blocking by the next caller of a method
    > for this input stream."


    It means that available() tells you how many bytes are available for reading
    without requiring the machine go to the network connection to fetch more
    data. IOW, it's the number of bytes already available in the buffer.

    --
    Darryl L. Pierce <>
    Homepage: http://mcpierce.multiply.com/
    "McVeigh's lawyer got him the death penalty, which, quite frankly,
    I could have done." - Jon Stewart
     
    Darryl L. Pierce, Jan 30, 2006
    #6
    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. Shaun Clark
    Replies:
    0
    Views:
    3,426
    Shaun Clark
    Jun 28, 2003
  2. R
    Replies:
    5
    Views:
    2,146
    Kevin McMurtrie
    Mar 13, 2005
  3. Rahul
    Replies:
    1
    Views:
    2,543
    Thomas Weidenfeller
    Aug 4, 2005
  4. sam

    Avaliable free modules

    sam, Dec 16, 2004, in forum: Python
    Replies:
    3
    Views:
    307
    Christos TZOTZIOY Georgiou
    Dec 16, 2004
  5. ajcrm125
    Replies:
    6
    Views:
    5,559
Loading...

Share This Page