I’m trying to serialize an object that contains an ImageIcon (which implements serializable). That image is a sample image of ubuntu, is about 320k size.
public static void write() {
ImageIcon aww = null;
aww = new ImageIcon("/home/javi/PRUEBA/img.jpg");
ab hue = new ab("hola", "adios", 10, 1111, aww);
ObjectOutputStream oos = null;
FileOutputStream a = null;
try {
a = new FileOutputStream("/home/javi/PRUEBA/mapa.atd");
oos = new ObjectOutputStream(a);
oos.writeObject(hue);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
a.close();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ab implements Serializable {
private static final long serialVersionUID = -4377893644898458425L;
String asdf;
String b;
int w;
Integer ab;
ImageIcon a;
public ab(String asdf, String b, int w, Integer ab, ImageIcon ww) {
super();
this.asdf = asdf;
this.b = b;
this.w = w;
this.ab = ab;
a = ww;
}
}
This is the code I’m using, and I don’t know why the image is about 320k and the output file is about 10Mb. If needed I can upload the img (/usr/share/unity-2d/warty-final-ubuntu.jpg)
The image data stored in the ImageIcon is probably a simple, non-compressed bitmap equivalent. An array of ints, possibly.
If you want to efficiently use the java serialization, you have to write a wrapper around this class and store the original binary file in the memory and mark the ImageIcon as
transient– and rebuild the ImageIcon from the data when it is necessary after the deserialization.