I'm saving some Java objects in files. I seralize them that way :
Class Timestamp.java:
public class Timestamp implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private static SimpleDateFormat staticFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", ENGLISH);
private SimpleDateFormat instanceFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", ENGLISH);
private long time;
}
Class ObjId.java:
public class ObjId implements java.io.Serializable {
private final int x;
private final int y;
private final int z;
public ObjId(final int x, final int y, final int z) {
this.x = x;
this.y = y;
this.z = z;
}
}
CachedObj.java :
class CachedObj implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private final ObjId id;
private final byte[] data;
private final String eTag;
private Timestamp modified;
private Timestamp expires;
private boolean mustRevalidate;
public CachedObj(final ObjId ID, final byte[] data, final String eTag, final Timestamp modified, final Timestamp expires, final boolean mustRevalidate) {
this.id= ID;
this.data = data;
this.eTag = eTag;
this.modified = modified;
this.expires = expires;
this.mustRevalidate = mustRevalidate;
}
}
The rest of my code :
public void saveObjToFlash(CachedObj obj, String fileName) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(obj);
out.flush();
bos.close();
out.close();
try (OutputStream outputStream = new FileOutputStream(fileName)) {
bos.writeTo(outputStream);
} catch (Exception e) {
e.getStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
In another place in my code I deserialize that way :
public CachedObj getCachedObjFromFile(String filename) {
try {
File file = new File(filename);
FileInputStream fileInput = new FileInputStream(file);
ObjectInputStream objInput = new ObjectInputStream(fileInput);
CachedObj obj= (CachedObj ) objInput.readObject();
fileInput.close();
objInput.close();
return obj;
} catch (IOException ex) {
ex.printStackTrace();
} catch (java.lang.ClassNotFoundException exx) {
exx.printStackTrace();
}
return null;
}
And I have a function that calculates an CachedObj object size :
public int getObjectSize(CachedObj obj) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(obj);
out.flush();
out.close();
bos.close();
int sizeObj = bos.toByteArray().length;
return sizeObj;
} catch (IOException e) {
e.printStackTrace();
return -1;
}
}
My question is, when I run the following code :
public static void main(String[] args) {
byte[] bytesArr = new byte[4];
CachedObj obj1 = new CachedObj new ObjId(100, 100, 100), bytesArr , "etag100",
new Timestamp(1659523700),
new Timestamp(1659523700), false);
int size1 = getObjectSize(obj1);
saveObjToFlash(obj1, "file");
CachedObj obj2 = getCachedObjFromFile("file"); // objects obj1 and obj2 seems to have same exact fields values
int size2 = getObjectSize(obj2); // I always get : size2 = size1 - 2 !!
}
Why are size1 and size2 different ? How can I get the size of obj2 such as I'll get the same value of size1 ?