What is the easiest way to convert an Object into a byte[]?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Gernot_Schmoelzer
Posted On:   Friday, December 6, 2002 10:25 AM

Hi all! I need to send an Object through a socket. The data sent should be base64 encoded and thus I need to convert the Object before transmitting into a byte[]. I tried it with the following code. Sometimes it works, sometimes I get a "StreamCorruptedException: invalid stream header" exception when I would like to restore the object... Sender: ByteArrayOutputStream st1 = new ByteArrayOutputStream(); ObjectOutputStream st2 = new ObjectOutputStream(st1); st2.writeObject(groupData); strToServer = Base64.encode64(st1.toByteArray()); this is sent through the socket... Receiver: strInputLine    More>>

Hi all!



I need to send an Object through a socket. The data sent should be base64 encoded and thus I need to convert the Object before transmitting into a byte[].



I tried it with the following code. Sometimes it works, sometimes I get a "StreamCorruptedException: invalid stream header" exception when I would like to restore the object...



Sender:

ByteArrayOutputStream st1 = new ByteArrayOutputStream();

ObjectOutputStream st2 = new ObjectOutputStream(st1);

st2.writeObject(groupData);

strToServer = Base64.encode64(st1.toByteArray());

this is sent through the socket...



Receiver:

strInputLine = new String(Base64.decode64(strInputLine));

ByteArrayInputStream is1 = new ByteArrayInputStream(strInputLine.getBytes());

ObjectInputStream is2 = new ObjectInputStream(is1);

GroupEntity groupData = (GroupEntity)is2.readObject();

HERE THE EXCEPTION OCCURS!!!



I would be very glad if somebody could help me...!!!


Thanks, Gernot

   <<Less
About | Sitemap | Contact