0

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?

2
  • 2
    No; you need a serialization system. Commented Apr 21, 2017 at 18:52
  • By 'assign', do you mean 'read into a byte array field'? Commented Apr 21, 2017 at 18:54

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This internally does a fair amount of parsing -- it will not simply do an unsafe memcpy of those bytes into the place in memory where the object lives, as the OP seems to want.
I'm not sure about performance. You also need to make sure the data coming in is a serialized java object. Otherwise you can try overriding the readObject methods. I will update the answer for you.
My point is that the OP seems to want C's functionality of, "I have this struct, I know it takes up these 213 bytes in memory, I have this array of 213 bytes -- I'm going to just assign a pointer to the start of that array, au voila, I have an instance of my struct." Java does not have that functionality, except possibly through the Unsafe classes -- tbh I've never messed with them. But serialization is definitely not that. It could of course be that I've misunderstood what the OP wants, and they're just looking for easy serialization without having to write much code.
In other words, if your solution isn't O(1) with the size of the object, assuming you already have a byte[] with the object's data -- which serialization is not -- then I don't think it's what the OP is looking for.
I think I see what you mean. I updated the answer for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.