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

前端 未结 1 1132
你的背包
你的背包 2020-12-19 08:48

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

Looking at the Buffe

相关标签:
1条回答
  • 2020-12-19 09:39

    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().

    0 讨论(0)
提交回复
热议问题