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

NullPointerException in ImageReader.getImageMetadata, getStreamMetadata

XMLWordPrintable

    • beta2
    • generic
    • generic



      Name: bkR10012 Date: 05/02/2001


      Spec. of the class javax.imageio.ImageReader says:

      "getImageMetadata

       public IIOMetadata getImageMetadata(int imageIndex,
                                          String formatName,
                                          Set nodeNames)
                                   throws IOException
      .....
           The default implementation simply returns the result of calling
           getImageMetadata(imageIndex), after checking that the format name
           is supported. If it is not, null is returned."

      "getStreamMetadata

       public IIOMetadata getStreamMetadata(String formatName,
                                           Set nodeNames)
                                    throws IOException
      .....
           The default implementation simply returns the result of calling
           getStreamMetadata(), after checking that the format name is
           supported. If it is not, null is returned. "

      But these methods throw unexpected NullPointerException if
      getImageMetadata(imageIndex)/getStreamMetadata() return IIOMetadata
      object with null values of nativeMetadataFormatName and
      extraMetadataFormatNames.

      See test source and log below.
      ---------------------------------------- test.java
      import javax.imageio.*;
      import javax.imageio.spi.*;
      import javax.imageio.metadata.*;
      import javax.imageio.stream.MemoryCacheImageInputStream;
      import java.awt.image.BufferedImage;
      import java.io.IOException;
      import java.io.ByteArrayInputStream;
      import java.util.*;
      import org.w3c.dom.Node;

      public class test {
          private final static byte[] ba = {};

          public static void main(String argv[]) {
          
              ImageReader ireader = new DummyImageReaderImpl(null);
              MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream
                  (new ByteArrayInputStream(ba));
              ireader.setInput(mciis);

              String formatName = "some name";
              Set nodeNames = java.util.Collections.EMPTY_SET;
          
              try {
                  ireader.getImageMetadata(0, formatName, nodeNames);
              } catch (Throwable ee) {
                  System.out.println("getImageMetadata Unexpected exception: " + ee);
              }
              try {
                  ireader.getStreamMetadata(formatName, nodeNames);
              } catch (Throwable ee) {
                  System.out.println("getStreamMetadata Unexpected exception: " + ee);
              }
          }
      }

      class DummyImageReaderImpl extends ImageReader {

          public DummyImageReaderImpl(ImageReaderSpi originatingProvider) {
              super(originatingProvider);
          }
          
          public IIOMetadata getStreamMetadata() throws IOException {
              return new DummyIIOMetadataImpl(true, null, null, null, null);
          }
          
          public IIOMetadata getImageMetadata(int imageIndex)
              throws IOException {

              if (input == null)
                  throw new IllegalStateException();
              if (imageIndex >= 1 || imageIndex < 0)
                  throw new IndexOutOfBoundsException();
              if (seekForwardOnly) {
                  if (imageIndex < minIndex)
                      throw new IndexOutOfBoundsException();
                  minIndex = imageIndex;
              }
              return new DummyIIOMetadataImpl(false, null, null, null, null);
          }

          public BufferedImage read(int imageIndex, ImageReadParam param)
              throws IOException {
              if (input == null)
                  throw new IllegalStateException();
              if (imageIndex >= 1 || imageIndex < 0)
                  throw new IndexOutOfBoundsException();
              if (seekForwardOnly) {
                  if (imageIndex < minIndex)
                      throw new IndexOutOfBoundsException();
                  minIndex = imageIndex;
              }
              
              return getDestination(param, getImageTypes(imageIndex), 10, 15);
          }

          public Iterator getImageTypes(int imageIndex) throws IOException {
              if (input == null)
                  throw new IllegalStateException();
              if (imageIndex >= 1 || imageIndex < 0)
                  throw new IndexOutOfBoundsException();
                  
              Vector imageTypes = new Vector();
              imageTypes.add(ImageTypeSpecifier.createFromBufferedImageType
                  (BufferedImage.TYPE_BYTE_GRAY ));
              return imageTypes.iterator();
          }
          public int getNumImages(boolean allowSearch) throws IOException {return 1;}
          public int getWidth(int imageIndex) throws IOException {return 1;}
          public int getHeight(int imageIndex) throws IOException {return 1;}
      }

      class DummyIIOMetadataImpl extends IIOMetadata {

          public DummyIIOMetadataImpl() { super(); }
          public DummyIIOMetadataImpl(boolean standardMetadataFormatSupported,
                                      String nativeMetadataFormatName,
                                      String nativeMetadataFormatClassName,
                                      String[] extraMetadataFormatNames,
                                      String[] extraMetadataFormatClassNames) {
              super(standardMetadataFormatSupported,
                    nativeMetadataFormatName,
                    nativeMetadataFormatClassName,
                    extraMetadataFormatNames,
                    extraMetadataFormatClassNames);
          }
          public boolean isReadOnly() {return true;}
          public Node getAsTree(String formatName) {return null;}
          public void mergeTree(String formatName, Node root)
              throws IIOInvalidTreeException {throw new IllegalStateException();}
          public void reset() {throw new IllegalStateException();}
      }----------------------------------------
      % java -version
      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b62)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b62, mixed mode)
      % java test
      getImageMetadata Unexpected exception: java.lang.NullPointerException
      getStreamMetadata Unexpected exception: java.lang.NullPointerException
      ----------------------------------------

      This bug causes failures of the new JCK tests
      api/javax_imageio/ImageReader/index.html#getImageMetadata
      api/javax_imageio/ImageReader/index.html#getStreamMetadata

      ======================================================================

            dricesunw Daniel Rice (Inactive)
            kuzsunw Kuz Kuz (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: