Is it possible to assign a byte arra(which are received via a tcp socket) directly to a Java Object without parsing the data?
In c it is possible to assign bytes directly to a struct, is that possible in java?
Is it possible to assign a byte arra(which are received via a tcp socket) directly to a Java Object without parsing the data?
In c it is possible to assign bytes directly to a struct, is that possible in java?
It is not possible to assign a byte array to an object in java and have all the member variables populated automatically, but it is possible to get a java object out of a byte array using serialization.
You can use ObjectInputStream and ObjectOutputStream to get objects in and out of a stream. To get one out of a byte array wrap a ByteArrayInputStream in a ObjectInputStream. The object must implement the Serializable interface. This should help you avoid parsing byte arrays manually.
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
MyObject o = (MyObject) in.readObject();
If you are reading data that is not a serialized java object you can add methods to the object to help the serialization.
From the javadoc for ObjectInputStream
Serializable classes that require special handling during the serialization and deserialization process should implement the following methods:
private void writeObject(java.io.ObjectOutputStream stream) throws IOException;
private void readObject(java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;
So you could read in data manually using stream.read(...) inside your custom readObject method and use that to set member variables on your object.
byte[] with the object's data -- which serialization is not -- then I don't think it's what the OP is looking for.