reading bytes from bytebuffer?

Discussion in 'Java' started by nooneinparticular314159@yahoo.com, Mar 12, 2008.

  1. Guest

    I'm reading some bytes sent over the network from a bytebuffer that
    I'm using with Java.NIO. The last time I tried this, I was reading
    text data, so I created a charbuffer view of the bytebuffer and
    appended to a string until my string was long enough to contain the
    data that I wanted to manipulate. This time, I want to get bytes out,
    and I'm being sent bytes. Great! Except what I can't figure out is
    how to tell if enough bytes have arrived yet.

    ie. Let's say that I really want 100 bytes of data. Until 100 bytes
    arrive, I can't do anything useful with the data. How can I test to
    see if 100 new bytes are currently available for reading in the
    bytebuffer? What happens if, say, only 60 bytes have arrived? Is
    there a way to test the length of the newly arrived bytes and not read
    from the buffer until 100 show up?

    Thanks!
    , Mar 12, 2008
    #1
    1. Advertising

  2. EJP Guest

    wrote:
    > ie. Let's say that I really want 100 bytes of data. Until 100 bytes
    > arrive, I can't do anything useful with the data. How can I test to
    > see if 100 new bytes are currently available for reading in the
    > bytebuffer?


    ByteBuffer.position()

    > What happens if, say, only 60 bytes have arrived?


    You should read some more data from the channel.

    > Is there a way to test the length of the newly arrived bytes and not read
    > from the buffer until 100 show up?


    ByteBuffer.position()
    EJP, Mar 12, 2008
    #2
    1. Advertising

  3. wrote:
    > I'm reading some bytes sent over the network from a bytebuffer that
    > I'm using with Java.NIO. The last time I tried this, I was reading
    > text data, so I created a charbuffer view of the bytebuffer and
    > appended to a string until my string was long enough to contain the
    > data that I wanted to manipulate. This time, I want to get bytes out,
    > and I'm being sent bytes. Great! Except what I can't figure out is
    > how to tell if enough bytes have arrived yet.
    >
    > ie. Let's say that I really want 100 bytes of data. Until 100 bytes
    > arrive, I can't do anything useful with the data. How can I test to
    > see if 100 new bytes are currently available for reading in the
    > bytebuffer? What happens if, say, only 60 bytes have arrived? Is
    > there a way to test the length of the newly arrived bytes and not read
    > from the buffer until 100 show up?
    >
    > Thanks!


    Create your buffer the size you need to read and call
    SocketChannel.read() until the hasRemaining() of the buffer is 0.

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
    Knute Johnson, Mar 12, 2008
    #3
  4. EJP wrote:
    > wrote:
    >> ie. Let's say that I really want 100 bytes of data. Until 100 bytes
    >> arrive, I can't do anything useful with the data. How can I test to
    >> see if 100 new bytes are currently available for reading in the
    >> bytebuffer?

    >
    > ByteBuffer.position()
    >
    >> What happens if, say, only 60 bytes have arrived?

    >
    > You should read some more data from the channel.
    >
    >> Is there a way to test the length of the newly arrived bytes and not read
    >> from the buffer until 100 show up?

    >
    > ByteBuffer.position()


    I like that even better.

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
    Knute Johnson, Mar 12, 2008
    #4
  5. Nigel Wade Guest

    wrote:

    > I'm reading some bytes sent over the network from a bytebuffer that
    > I'm using with Java.NIO. The last time I tried this, I was reading
    > text data, so I created a charbuffer view of the bytebuffer and
    > appended to a string until my string was long enough to contain the
    > data that I wanted to manipulate. This time, I want to get bytes out,
    > and I'm being sent bytes. Great! Except what I can't figure out is
    > how to tell if enough bytes have arrived yet.
    >
    > ie. Let's say that I really want 100 bytes of data. Until 100 bytes
    > arrive, I can't do anything useful with the data. How can I test to
    > see if 100 new bytes are currently available for reading in the
    > bytebuffer? What happens if, say, only 60 bytes have arrived? Is
    > there a way to test the length of the newly arrived bytes and not read
    > from the buffer until 100 show up?
    >
    > Thanks!


    ByteBuffer doesn't actually read anything from an input stream. It is merely a
    convenience wrapper around an already created byte array. If you need 100 bytes
    in the byte array then you need to ensure that they are provided by whatever
    stream input you are using to populate the byte array. One way is to use a
    DataInputStream and use the readFully() method of that class.

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, Mar 12, 2008
    #5
    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. Jason Collins
    Replies:
    3
    Views:
    5,993
    Jason Collins
    Feb 18, 2004
  2. Jeff Silvis
    Replies:
    1
    Views:
    771
    Paul Lutus
    Sep 16, 2004
  3. -
    Replies:
    1
    Views:
    343
    Stefan Schulz
    Jul 5, 2005
  4. jespersahner

    Moving bytes in a Bytebuffer

    jespersahner, Jul 15, 2005, in forum: Java
    Replies:
    2
    Views:
    946
    jespersahner
    Aug 4, 2005
  5. Replies:
    2
    Views:
    1,443
Loading...

Share This Page