reading bytes from bytebuffer?

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

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!
 
E

EJP

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()
 
K

Knute Johnson

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

Nigel Wade

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top