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?

fabiojb
  • 331
  • 2
  • 10
  • I'm pretty there aren't any (or very few at least) BufferedXXX classes that are serializable. – Falmarri Mar 10 '15 at 00:45
  • Duplicate of http://stackoverflow.com/questions/15058663/how-to-serialize-an-object-that-includes-bufferedimages, 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. – Chris Dennett Mar 10 '15 at 00:45
  • 2
    @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(....)` – Naruto Biju Mode Mar 10 '15 at 00:50
  • 4
    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. – VGR Mar 10 '15 at 02:03
  • @VGR it makes sense... So java provided the `ImageIcon` as an wrapper, that is simpler than `BufferedImage` and is `Serializable` – fabiojb Mar 10 '15 at 02:16
  • But, if there is something that shouldn't be serialized, this things must be transient, not all the class... – fabiojb Mar 10 '15 at 02:55
  • 2
    @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 http://stackoverflow.com/questions/25086868/how-to-send-images-through-sockets-in-java – Jean-François Savard Mar 10 '15 at 03:24
  • Could you extend `BufferedImage` to a class `SerializableBufferedImage` that implements `Serializable`? – MeetTitan Mar 10 '15 at 04:52

1 Answers1

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

Christian Hujer
  • 17,035
  • 5
  • 40
  • 47
  • 2
    Upvoted. It obviously *doesn't* apply to `javax.swing.ImageIcon.` – user207421 Mar 10 '15 at 03:34
  • 1
    @Jean-FrançoisSavard Why should an image not be `Serializable`? What's so special about an image compared to everything else? – Christian Hujer Mar 10 '15 at 03:35
  • 2
    @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. – user207421 Mar 10 '15 at 03:40
  • 3
    @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. – Christian Hujer Mar 10 '15 at 03:40
  • 2
    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. – Christian Hujer Mar 10 '15 at 03:46
  • 1
    It also doesn't address the object-graph issue. – user207421 Mar 10 '15 at 03:47