Roy08
Roy08

Reputation: 263

Serialization gives wrong size of object

I'm working on an Android app where I use serialization to convert an object to a byte array. After the conversion I read the size and I got a much bigger value of the byte array.

The method that I have made is as followed:

public void Send(testpacket packet){        
    try
    {           
        // First convert the CommStruct to a byte array
        // Then send the byte array
        byte [] buffer = toByteArray(packet);

        int size = buffer.length;

        System.out.println("SIZE OF BYTE ARRAY: " + size);

        server.send(buffer);

    } 
    catch (IOException e)
    {
        Log.e("USBCommunicator", "problem sending TCP message", e);
    }   
}

The serialization method toByteArray converts a object to a byte array and looks as followed:

  public static byte[] toByteArray(Object obj) throws IOException {
        byte[] bytes = null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray();
        } finally {
            if (oos != null) {
             Log.i(TAG, "not null");
                oos.close();
            }
            if (bos != null) {
                bos.close();
             Log.i(TAG, "not null");
            }
        }
        return bytes;
    }

The object packet consists of two classes with a total of 7 integers (so the size should be 28 bytes). And is defined as followed:

public class testpacket implements java.io.Serializable {

public ObjectInfo VisionData;
public SensorDataStruct SensorData;

//Constructor
public testpacket(){
     // Call constructors
     VisionData = new ObjectInfo();
     SensorData = new SensorDataStruct();
}
 }

ObjectInfo consists of the following:

//ObjectInfo struct definition
public class ObjectInfo implements java.io.Serializable
{
public int ObjectXCor;
public int ObjectYCor;
public int ObjectMass;

//Constructor
public ObjectInfo(){
    ObjectMass = 0;
    ObjectXCor = 0;
    ObjectYCor = 0;
}
};

And SensorDataStruct is as followed:

//ObjectInfo struct definition
public class SensorDataStruct implements java.io.Serializable
{
public int PingData;
public int IRData;
public int ForceData;
public int CompassData;

//Constructor
public SensorDataStruct(){
    CompassData = 0;
    ForceData = 0;
    IRData = 0;
    PingData = 0; 
}
};

But when I read out the length of the byte buffer after the convertion the size is 426. Does anybody have a idea or suggestion why this is not 28 bytes? If i need to supply more information please say so! Any tips and suggestions are welcome!

Update

I have changed the code with the help of EJP. I use the DataOutputStream to convert the object data (the actual variable data) to bytes. The object decribed above in this post contains 7 integers and when the object it created the starting values is for all these integers 0.

The convertion function is as followed:

public static byte[] toByteArray(testpacket obj) throws IOException { byte[] bytes = null;

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream w = new DataOutputStream(baos);

        w.write(obj.SensorData.CompassData);
        w.write(obj.SensorData.ForceData);
        w.write(obj.SensorData.IRData);
        w.write(obj.SensorData.PingData);

        w.write(obj.VisionData.ObjectMass);
        w.write(obj.VisionData.ObjectXCor);
        w.write(obj.VisionData.ObjectYCor);

        //w.flush();

        bytes = baos.toByteArray();  

        int size = bytes.length;

        System.out.println("SIZE OF BYTE ARRAY IN CONVERTION FUNCTION: " + size);


                return bytes;

}

Now i only have one question: the size is 7 when i read out the size of the byte buffer. This is (i think) because of the that all values (0's) of the integers are so small that they fit in one byte each. My question is how can i make this so for each integer value Always four bytes will be used in the datastream? Any suggestions are welcome!

Upvotes: 0

Views: 360

Answers (1)

user207421
user207421

Reputation: 311023

The serialized stream for your object contains:

  1. An object stream header.
  2. Tag information saying the next item is an object.
  3. Class information for the object.
  4. Version information for the object.
  5. Type-name-value tuples, for each serialized member of the object.

Upvotes: 1

Related Questions