Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

storing a serialized object in a String?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I would like to deserialize an object and store its value as a String. This string is stored in an XML document and will need to be converted into an object on the receiver side which the xml document is sent to.
I have tried using the ByteArrayOutputStream class to obtain a byte array of the object and converting that into a String using the UTF-16 encoding. However when I get the byte array of the extracted string on the receiver side (via String.getBytes()) my ObjectInputStream instance throws an exception reporting an invalid stream header. I use the same encoding on both sides (UTF-16) but it does not work and the length of the byte array on the sender and receiver side are different.
Can you please guide me as to the best way of acheiving this. Do I have to use encodings such as Base64, if so which classes/libraries?
I have included segments of my code for the serialization and de-serialization below.
Thanks in advance,
Nima
===================
//code for the serializarion process
String objectToBeSerialised = "Test Message";
byte[] serialisedObjectbytes = null;
try {
ByteArrayOutputStream fo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(objectToBeSerialised = "
serialisedObjectbytes = fo.toByteArray();
so.flush();
fo.flush();
fo.close();
so.close();
}
catch (UnsupportedEncodingException e) {
System.out.println("Unsupported Encoding");
}
catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
String serialisationResult = null;
try {
serialisationResult = new String(serialisedObjectbytes, "UTF-16");
} catch (UnsupportedEncodingException ex) {System.exit(0);}


//code for the de-serializarion process
try {
byte[] inputByteArray = inputString.getBytes("UTF-16");
} catch (UnsupportedEncodingException ex1) {System.exit(0);}
try {
ByteArrayInputStream fi = new ByteArrayInputStream(inputByteArray);
ObjectInputStream si = new ObjectInputStream(fi);
String serviceDescriptor = (String) si.readObject();
si.close();
System.out.println("the deserialised string is: " + serviceDescriptor2);
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
 
Nima Kaveh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I just wanted to let you know that I have sorted the problem out. This was made possible by encoding the binary bytes of the serialized object into a hex encoded string.
The receiver simply decoded the hex encoded string into a byte array to obtain a byte array and successfully deserialize it into an object.
cheers,
Nima
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!