Reputation: 847
I'm trying to find a reliable way to send control messages that obey a defined protocol in order to tell the server what kind of data he will receive. For example, I want to send a pure text message to invoke a remote method:
#METHOD1#CLOSE#
or I want to send a serialized object to the server:
#OBJECT# .......here comes the serialized object data....#CLOSE#
So basically I just want to send string control messages that are completely independent of the stream content that follows.
By wrapping an input stream into a Scanner object I'm able to extract strings from the input stream, but if this stream is a serialized object then the object can't be restored afterwards. Thanks for any help.
Upvotes: 0
Views: 897
Reputation: 408
You can use a scheme like Base64 (e.g. use a library from Apache) to encode the Object from bytes into a string and then back.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new ObjectOutputStream(baos).writeObject(object);
String serializedObject = Base64.encode(baos.toByteArray());
byte[] bytes = Base64.decode(serializedObject);
ByteArrayInputStream baos = new ByteArrayInputStream(bytes);
Object object = new ObjectInputStream(baos).readObject()
Upvotes: 1