Click here to Skip to main content
15,889,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to cast a Object type into byte array in java?
Posted

If you are thinking about serailising your object, just make it Serializable[^].
 
Share this answer
 
Comments
Mehdi Gholam 26-Sep-11 8:01am    
My 5!
Varad Velingkar 26-Sep-11 8:02am    
no buddi i don't want to serialize i just want to convert the data object recieved from a vector into byte array
TorstenH. 26-Sep-11 8:24am    
why can't we vote for comments? this one would get my 5! *rofl*
Nagy Vilmos 26-Sep-11 8:26am    
Because. Just because... ;)
Nagy Vilmos 26-Sep-11 8:26am    
Why use vectors?
What is the object you are using in the Vector?

You need to decide how the internal objects are described. All the basic types such as String, int or double either support a direct conversion or can be simply converted with a few lines of code.

Instead of asking 'how to convert', think /why/ do you want a byte array? The only reason I can think of is for storage and/or communications which comes back to serialisation.
public byte[] convert(Object obj) throws IOException {
ObjectOutputStream os = null;

ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
os.flush();
os.writeObject(obj);
os.flush();
byte[] sendBuf = byteStream.toByteArray();
os.close();
return sendBuf;

}


try this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900