I have a class that containts two class objects with some variables. I want to send a instance of the class via USB with a send function (and recieve on the other side). The send function accepts byte arrays (byte[]).
My question is how do i convert the instance of the class to a byte array so that i can send it? And how do i properly reconstruct it on the other side?
Below is te class Comsstruct that i want to send and recieve. Any sugestions are welcome!
// ObjectInfo struct definition public class ObjectInfo { int ObjectXCor; int ObjectYCor; int ObjectMass; }; // ObjectInfo struct definition public class SensorDataStruct{ int PingData; int IRData; int ForceData; int CompassData; }; // ObjectInfo struct definition public class CommStruct{ public ObjectInfo VisionData; public SensorDataStruct SensorData; }; public CommStruct SendPacket; public CommStruct RecievePacket; Update
I have found a solution but with the suggestions that i got i wonder if this will work (and if it is a good solution)?
There is a serialsation method and a send method:
// Method to convert object to a byte array public static byte[] serializeObject(CommStruct obj) throws IOException { ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bytesOut); oos.writeObject(obj); oos.flush(); byte[] bytes = bytesOut.toByteArray(); bytesOut.close(); oos.close(); return bytes; } // Send struct function public void Send(){ try { // First convert the CommStruct to a byte array // Then send the byte array server.send(serializeObject(SendPacket)); } catch (IOException e) { Log.e("microbridge", "problem sending TCP message", e); } And a recieve handler function:
public void onReceive(com.example.communicationmodulebase.Client client, byte[] data) { // Event handler for recieving data // Try to receive CommStruct try { ByteArrayInputStream bytesIn = new ByteArrayInputStream(data); ObjectInputStream ois = new ObjectInputStream(bytesIn); RecievePacket = (CommStruct) ois.readObject(); ois.close(); } catch (IOException e) { Log.e("microbridge", "problem recieving TCP message", e); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.e("microbridge", "problem recieving TCP message", e); } // Send the recieved data packet back SendPacket = RecievePacket; // Send CommStruct Send(); } My question is if this is a good solution or not?
22 Answers
You could have your classes implement the Serializable interface, and then send that over a bytestream. But that's a really bad idea and will most probably not work.
The reasons for why this wouldn't work, or isn't guaranteed to work, is that there's no guarantee that different implementations of java will use the same serializing mechanism, thus the bytearray could very well differ in different Java implementations.
Instead I'd recommend you to convert the objects to some intermediary format like xml or json and send that instead. That way the receiver doesn't even have to be coded in java.
5You can reference here: Convert object to byte array and convert byte array to object