Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

reading bytes from bytebuffer?

Thread view: 
nooneinparticular314159@yahoo.com - 12 Mar 2008 01:30 GMT
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!
EJP - 12 Mar 2008 02:24 GMT
> 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()
Knute Johnson - 12 Mar 2008 02:37 GMT
>> 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
[quoted text clipped - 11 lines]
>
> ByteBuffer.position()

I like that even better.

Signature

Knute Johnson
email s/nospam/linux/

     ------->>>>>>http://www.NewsDem

Knute Johnson - 12 Mar 2008 02:35 GMT
> 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
[quoted text clipped - 12 lines]
>
> Thanks!

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

Signature

Knute Johnson
email s/nospam/linux/

     ------->>>>>>http://www.NewsDem

Nigel Wade - 12 Mar 2008 10:38 GMT
> 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
[quoted text clipped - 12 lines]
>
> 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.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.