• Post Reply Bookmark Topic Watch Topic
  • New Topic

Non-serializable Object to Byte Array

 
Suresh Ray
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
How do we convert a Non-serializable object to a byte array?
I do know, if the object is serializable, then we can use ObjectOutputStream and ByteArrayOutputStream to write an object and execute the bytearray() method to get the bytes.
But, in case of Non-serializable objects, how do we obtain the object in the form of a byte array?
Note: From String object we can obtain bytes, but I do not want to convert the Object to String.
Object obj = new Object();
obj.??
Any help will be appreciated.
Thanks.
 
Suresh Ray
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestions?
 
Brunck McGuint
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great question. I'm stuck with the same exact problem. Anybody with a solution ???
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object can be written/read from a Stream Only if it's serialized. It does not matter if the Stream is as ObjectxxxStream or ByteArrayxxxStream.
try subclassing:

You can see more Here. But here is what is most relevant:

To allow subtypes of non-serializable classes to be serialized, the subtype may assume responsibility for saving and restoring the state of the supertype's public, protected, and (if accessible) package fields. The subtype may assume this responsibility only if the class it extends has an accessible no-arg constructor to initialize the class's state. It is an error to declare a class Serializable if this is not the case. The error will be detected at runtime.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!