title: Converting class objects to byte buffer
body: <p>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[]).</p>
  
  <p>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?</p>
  
  <p>Below is te class Comsstruct that i want to send and recieve. Any sugestions are welcome!</p>
  
  <pre><code>     // 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;
  </code></pre>
  
  <h1>Update</h1>
  
  <p>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)?</p>
  
  <p>There is a serialsation method and a send method:</p>
  
  <pre><code>// 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);
  }   
  </code></pre>
  
  <p>And a recieve handler function:</p>
  
  <pre><code>    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();
  }
  </code></pre>
  
  <p>My question is if this is a good solution or not?</p>