Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't java.awt.image.BufferedImage serializable?

I'm trying to serialize a BufferedImage in Java, but running my program I received a NotSerializableException.

Looking at the BufferedImage class, I noticed that it does not implements Serializable.

Why doesn't BufferedImage implement Serializable?

like image 316
fabiojb Avatar asked Mar 10 '15 00:03

fabiojb


1 Answers

I think you've just discovered a missing feature.

  • Does it make sense to have BufferedImage implements Serializable? In my opinion it does. Especially if the BufferedImage was not loaded from a file, but created and drawn upon. But even if it's from a file, who cares where the stuff comes from if I want to exchange it between VMs via RMI or similar?
  • Is there anything in BufferedImage that provides a strong technical reason against BufferedImage implements Serializable? I browsed the source code, and I don't think so.

I checked whether the bug database already contains an entry for that, and I couldn't find anything related. So, this is your chance to make your contribution and suggest a feature request via the bug database. http://bugs.java.com/bugdatabase/

As a workaround, you might want to look at the implementation of readObject() and writeObject() in class javax.swing.ImageIcon. ImageIcon is Serializable. Maybe you can wrap the BufferedImage in an ImageIcon for your use case, or somehow otherwise provide the logic from ImageIcon.readObject() / ImageIcon.writeObject().

like image 155
Christian Hujer Avatar answered Nov 18 '22 13:11

Christian Hujer