Why isn't java.awt.image.BufferedImage serializable?
Asked Answered
A

1

6

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?

Amadus answered 10/3, 2015 at 0:18 Comment(8)
I'm pretty there aren't any (or very few at least) BufferedXXX classes that are serializable.Jarrad
Duplicate of #15059163, or quite similar I think. But it'd be interesting to know the reason why it can't be serialised. I think because it contains arch-specific raster data and endian.Shaeffer
@Fabio Bohnenberger Try to use int[] getRGB(....) and serialize the returned integer array instead of the image, and when you deserialize the array try to recreate your BufferedImage using the method setRGB(....)Sakti
BufferedImages can be managed —backed by platform-specific acceleration (such as graphics card texture memory). Such features aren't particularly easy or useful to serialize.Tetter
@Tetter it makes sense... So java provided the ImageIcon as an wrapper, that is simpler than BufferedImage and is SerializableAmadus
But, if there is something that shouldn't be serialized, this things must be transient, not all the class...Amadus
@FabioBohnenberger ImageIcon is actually a swing components which hold an Image, it is serializable, but should not be the solution to your question. You might want to check this #25087368Mostly
Could you extend BufferedImage to a class SerializableBufferedImage that implements Serializable?Ominous
F
5

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

Fabrication answered 10/3, 2015 at 3:24 Comment(6)
Upvoted. It obviously doesn't apply to javax.swing.ImageIcon.Dishtowel
@Jean-FrançoisSavard Why should an image not be Serializable? What's so special about an image compared to everything else?Fabrication
@Jean-FrançoisSavard An image is only a file if you already have the file. If you don't have it, and you need it, you have to get it from somewhere else. Images aren't necessarily files at all: they can be generated dynamically. This really is not making any sense.Dishtowel
@Jean-FrançoisSavard Sorry, I find that argument absurd. Somehow, you can turn everything into a file, that doesn't mean that Serialization doesn't make sense. By that definition, javax.swing.text.html.HTMLDocument shouldn't be Serializable either. Just because something has one or more commonly used file formats doesn't mean that direct serialization should not be provided. I see serialization primarily as a matter of convenience for programmers when exchanging data between VMs to actually not have to deal with storing stuff in separate files but simply dump an object graph.Fabrication
Saving to a file is not a solution. For example, look at RMI. The two VMs might actually not be accessing the same file system. So somehow, there needs to be a byte stream between the two VMs.Fabrication
It also doesn't address the object-graph issue.Dishtowel

© 2022 - 2024 — McMap. All rights reserved.