Skip to main content

How to send a java object message

3 replies [Last post]
intel3
Offline
Joined: 2009-03-23
Points: 0

I don't understand how to send a java object using the message protocol passing it through a bidirectional pipe.

can someone provide sample code on how to do it, it would really help me a lot!

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
iiggoorr
Offline
Joined: 2008-09-22
Points: 0

HI,

well unfortunately there is no direct way to pu java object in jxta message.

The way to do it is to serialize your object to byte array[] and then put the byte array to
net.jxta.endpoint.ByteArrayMessageElement.

Then you put your ByteArrayMessageElement to message and send it.

on the other and you must deserialize your byte array back to object and cast it to right instance.

Here are the methods for serializing and deserializing.

public static byte[] serializeMessage(Serializable object) throws IOException
{
ObjectOutputStream oos =null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
oos= new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] temp=baos.toByteArray();
oos.close();
baos.close();
return temp;
}

public static Object deSerializeMessage(byte[]data) throws IOException, ClassNotFoundException
{
Object recived;
recived = new ObjectInputStream(new
ByteArrayInputStream(data)).readObject();
return recived;
}

as you can see object that you want to serialize must implement serialization interface.
After deserialization don't forget to cast.

hope this helps you.

bb

boylejohnr
Offline
Joined: 2008-10-27
Points: 0

Be aware that you can not exceed the MTU 64K for a JXTA message.

enygma2002
Offline
Joined: 2008-12-22
Points: 0

Yes, better use a JxtaSocket that does a lot of heavy work for you.