JDK-4032947 : java.awt.Frame cant be serialized if icon is set
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_2.5
  • CPU: sparc
  • Submitted: 1997-02-18
  • Updated: 1999-11-30
  • Resolved: 1999-11-30
Related Reports
Duplicate :  
Description
Following test fails because frame's icon is set.

import java.awt.*;
import java.io.*;

public class ImageBug {
    public static void main(String[] args) {
       
        Image img = Toolkit.getDefaultToolkit().getImage("Button.gif");
        System.out.println("Img = " + img);
 
        Frame f = new Frame("Button");
        f.setIconImage(img);
        try {
            ObjectOutputStream os = new ObjectOutputStream(
                                        new ByteArrayOutputStream());
            os.writeObject(f);
            os.close();
            System.out.println("OK");
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
    }    
}

Comments
SUGGESTED FIX The following class could be made private within java.awt.Frame and used to wrap values for the icon field. The code below still needs a way to block until width/height are available in writeObject(); and it hasn't been tested. import java.awt.Graphics; import java.awt.Toolkit; import java.awt.Image; import java.awt.image.ImageObserver; import java.awt.image.PixelGrabber; import java.awt.image.ColorModel; import java.awt.image.MemoryImageSource; import java.awt.image.ImageProducer; import java.io.ObjectOutputStream; import java.io.ObjectInputStream; import java.io.IOException; import java.io.Serializable; /** * A serializable wrapper for Frame icon Images; delegates all * Image methods to a real Image. At writeObject time we store * the width, height, and pixels and at readObject time we create * a new Image delegate. The int[] pixel array isn't saved between * writeObject calls, to avoid the storage penalty (assuming that * calls to writeObject will be infrequent relative to the lifetime * of the Frame). * * Note that this class is probably acceptable for Frame icons because * there will not be many of them and they're expected to be small * and unique. General support for image persistence needs to account * for multiple references to the same image and should probably use * a more compact pixel representation (when that's possible). */ public final class SImage extends Image implements Serializable { private transient Image delegate; SImage(Image x) { delegate = x; } public int getWidth(ImageObserver o) { return delegate.getWidth(o); } public int getHeight(ImageObserver o) { return delegate.getHeight(o); } public ImageProducer getSource() { return delegate.getSource(); } public Graphics getGraphics() { return delegate.getGraphics(); } public Object getProperty(String n, ImageObserver o) { return delegate.getProperty(n, o); } public void flush() { delegate.flush(); } private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); int w = s.readInt(); int h = s.readInt(); int[] pixels = (int[])(s.readObject()); Toolkit tk = Toolkit.getDefaultToolkit(); ColorModel cm = ColorModel.getRGBdefault(); delegate = tk.createImage(new MemoryImageSource(w, h, cm, pixels, 0, w)); } private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); int w = getWidth(); int h = getHeight(); int[] pixels = new int[w * h]; try { PixelGrabber pg = new PixelGrabber(delegate, 0, 0, w, h, pixels, 0, w); pg.grabPixels(); if ((pg.getStatus() & ImageObserver.ABORT) != 0) { throw new IOException("failed to load image contents"); } } catch (InterruptedException e) { throw new IOException("image load interrupted"); } s.writeInt(w); s.writeInt(h); s.writeObject(pixels); } }
11-06-2004

EVALUATION The problem occurs because image's aren't serializable. They will not be until Java 2D ships, as I understand it. We could workaround the problem in this case (see Suggesed Fix) however it's not pleasant. hans.muller@Eng 1997-02-20 ============================================================================= There is a similar bug #4030693 that has been closed as duplicate of bug #4025021. sandeep.konchady@Eng 1999-11-30
30-11-1999