Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4393995

ImageFormatException loading JPEG image

    XMLWordPrintable

Details

    • Bug
    • Resolution: Cannot Reproduce
    • P3
    • None
    • 1.3.0
    • deploy
    • x86
    • windows_2000

    Description



      Name: yyT116575 Date: 11/30/2000


      System.getProperty("java.version") returns "1.3.0rc1"
      System.getProperty("java.class.version") returns "47.0"


      When using the default JVM for Netscape 6 PR1, an applet which attempts
      to create a java.awt.Image for a byte array containing a JPEG image receives
      this exception:

      sun.awt.image.ImageFormatException: JPEG datastream contains no image
      at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
      at sun.awt.image.JPEGImageDecoder.produceImage(Unknown Source)
      at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
      at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
      at sun.awt.image.ImageFetcher.run(Unknown Source)

      Note: This sounds similar to bug 4025679 reported against JDK 1.0, and fixed in
      JDK 1.1fcs.

      A small example follows:

      JPEGApplet.java:

      import java.applet.*;
      import java.awt.*;

      public class JPEGApplet extends Applet
      {
        public void init()
        {
          System.out.println("JPEGApplet started");
          try { Thread.currentThread().sleep(5000); }
          catch (InterruptedException ie) {}
          new Splash(getParentFrame(), "image.jpg", 270, 164);
        }

        private Frame getParentFrame()
        {
          Component c = getParent();
          while (c != null && !(c instanceof Frame))
            c = c.getParent();
          return (Frame)c;
        }
      }

      Splash.java:

      import java.awt.*;

      class Splash extends Window
      {
        Splash(Frame parent, String imageResourceName, int width, int height)
        {
          super(parent);
          setBackground(Color.white);
          Dimension screen = getToolkit().getScreenSize();
          int myWidth = width + 50;
          int myHeight = height + 50;
          setBounds((screen.width - myWidth) / 2, (screen.height - myHeight) / 2, myWidth, myHeight);
          add(new ImageCanvas(imageResourceName, width, height), "Center");
          setVisible(true);
        }
      }

      ImageCanvas.java:

      import java.awt.*;
      import java.awt.image.*;
      import java.net.URL;
      import java.io.*;
      import java.util.*;

      class ImageCanvas extends Canvas
      {
        ImageCanvas(String imageResourceName, int width, int height)
        {
          try
            {
      InputStream is = getClass().getResourceAsStream(imageResourceName);
      if (is == null)
      {
      System.out.println("Unable to get " + imageResourceName);
      return;
      }
      byte[] buf = new byte[is.available()];
      is.read(buf);
      is.close();
      image = getToolkit().createImage(buf);
            }
          catch (IOException ioe)
            {
      System.out.println("Unable to get " + imageResourceName + ": " + ioe);
      return;
            }
          if (image != null)
            prepareImage(image, width, height, this);
          this.width = width;
          this.height = height;
        }

        public void paint(Graphics g)
        {
          if (image != null)
            {
      Dimension size = getSize();
      g.drawImage(image, (size.width - width) / 2, (size.height - height) / 2, this);
            }
        }

        public void update(Graphics g)
        {
          paint(g);
        }

        public Dimension getMinimumSize()
        {
          if (image == null)
            return new Dimension(0,0);
          int width = image.getWidth(this);
          int height = image.getHeight(this);
          if (width <= 0 || height <= 0)
            {
      width = this.width;
      height = this.height;
            }
          return new Dimension(width, height);
        }

        public Dimension getPreferredSize()
        {
          return getMinimumSize();
        }

        private Image image;
        private int width;
        private int height;
      }

      image.jpg: can be accessed from
      http://customer.platypuspartners.com/JPEGApplet/image.jpg

      JPEGApplet.html:

      <HTML>
      <BODY>
      <applet code="JPEGApplet.class" archive="JPEGApplet.jar" width=200 height=200>
      </applet>
      </BODY>

      How to recreate:

      javac JPEGApplet.java
      jar cf JPEGApplet.jar *.class image.jpg
      Start Netscape 6
      Select Tasks -> Tools -> Java Console
      In the URL field, enter the URL of the JPEGApplet.html file
      (Review ID: 104971)
      ======================================================================

      Attachments

        Activity

          People

            djayaramsunw Devananda Jayaraman (Inactive)
            yyoungsunw Yung-ching Young (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: