java data structure for growing data?

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

I need to transmit some information over a network. The data is a
message of arbitrary length. I can include information in the header
about the size of the message. But I need a java object to store the
data in that can grow as needed to store more information, but can also
be made exactly the size of the message once I know how big the message
is. ie. If my message is 52342 bytes, I want to store an int saying
it is 52342 bytes, followed by exactly 52342 bytes of data. Is there
an object that is appropriate for this?

Thanks!
 
A

Alan Krueger

I need to transmit some information over a network. The data is a
message of arbitrary length. I can include information in the header
about the size of the message. But I need a java object to store the
data in that can grow as needed to store more information, but can also
be made exactly the size of the message once I know how big the message
is. ie. If my message is 52342 bytes, I want to store an int saying
it is 52342 bytes, followed by exactly 52342 bytes of data. Is there
an object that is appropriate for this?

ByteArrayOutputStream out = new ByteArrayOutputStream();
// ...write stuff to out...
int size = out.size();
networkStream.write( out.toByteArray() );
 
R

Roedy Green

I need to transmit some information over a network. The data is a
message of arbitrary length. I can include information in the header
about the size of the message. But I need a java object to store the
data in that can grow as needed to store more information, but can also
be made exactly the size of the message once I know how big the message
is. ie. If my message is 52342 bytes, I want to store an int saying
it is 52342 bytes, followed by exactly 52342 bytes of data. Is there
an object that is appropriate for this?

You serialise the object to a ByteArrayStream then to a byte[].

See http://mindprod.com/applets/fileio.html for how to create it and
how to ship it over a socket.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top