JDK-4703112 : RFE: Please add ImageIO.getReaderFileSuffixes() method
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.imageio
  • Affected Version: 6
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_2000
  • CPU: generic,x86
  • Submitted: 2002-06-17
  • Updated: 2017-05-16
  • Resolved: 2006-01-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b67Fixed
Description
Name: rmT116609			Date: 06/17/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

DESCRIPTION OF THE PROBLEM :
add getReaderFileSuffixes() to the ImageIO class, same as
getReaderMIMETypes() and getReaderFormatTypes() but with
FileSuffixes

Additioanl Information:

When bringing up an Open File dialog, we need to know the file suffixes to 
be filterred on.  Currently I have just cut and pasted 1 of the other 2 
methods (getReaderMIMETypes() and getReaderFormatTypes()), and changed it to 
use FileSuffixes.  This does the job however this method should be in 
ImageIO with the other two.  It looks like a small oversight that it wasn't 
already included.
(Review ID: 153678) 
======================================================================
Peabody community fix submitted by: (company - Self , email - ###@###.###)

A DESCRIPTION OF THE FIX :
I've written getReaderFileSuffixes() and getWriterFileSuffixes() has
described in the evaluation of the bug.

ImageIO is not converted to using generics and
doesn't use some methods from the collection API,
this explains why the code of getReaderFileSuffixes() is not
just a copy/paste from getReaderFormatNames().

Futhermore, there are lost of code duplication between
get(Reader|Writer)(FileSuffixes|FormatNames|MIMETypes),
i think these methods could use a common code.
So at the ends of this message you can find a version that share the
same code between all the methods.

java -version :
java version "1.6.0-ea"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-ea-b57)
Java HotSpot(TM) Client VM (build 1.6.0-ea-b57, mixed mode)

diff -u ImageIO.java.old IomageIO.java.new :
--- ImageIO.java.old	Thu Nov 24 21:52:16 2005
+++ ImageIO.java.new	Thu Nov 24 21:37:09 2005
@@ -18,6 +18,7 @@
 import java.security.AccessController;
 import java.util.Arrays;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
@@ -468,6 +469,32 @@
 
         return toStringArray(s);
     }
+    
+    /**
+     * Returns an array of <code>String</code>s listing all of the
+     * file suffixes associated with the formats understood
+     * by the current set of registered readers.
+     *
+     * @return an array of <code>String</code>s.
+     */
+    public static String[] getReaderFileSuffixes() {
+        
+        Iterator<ImageReaderSpi> iter;
+        // Ensure category is present
+        try {
+            iter = theRegistry.getServiceProviders(ImageReaderSpi.class, true);
+        } catch (IllegalArgumentException e) {
+            return new String[0];
+        }
+
+        HashSet<String> s = new HashSet<String>();
+        while (iter.hasNext()) {
+            ImageReaderSpi spi = iter.next();
+            Collections.addAll(s,spi.getFileSuffixes());
+        }
+
+        return s.toArray(new String[s.size()]);
+    }
 
     static class ImageReaderIterator implements Iterator<ImageReader> {
         // Contains ImageReaderSpis
@@ -800,6 +827,32 @@
         }
 
         return toStringArray(s);
+    }
+    
+    /**
+     * Returns an array of <code>String</code>s listing all of the
+     * file suffixes associated with the formats understood
+     * by the current set of registered writers.
+     *
+     * @return an array of <code>String</code>s.
+     */
+    public static String[] getWriterFileSuffixes() {
+        
+        Iterator<ImageWriterSpi> iter;
+        // Ensure category is present
+        try {
+            iter = theRegistry.getServiceProviders(ImageWriterSpi.class, true);
+        } catch (IllegalArgumentException e) {
+            return new String[0];
+        }
+
+        HashSet<String> s = new HashSet<String>();
+        while (iter.hasNext()) {
+            ImageWriterSpi spi = iter.next();
+            Collections.addAll(s,spi.getFileSuffixes());
+        }
+
+        return s.toArray(new String[s.size()]);
     }
 
     static class ImageWriterIterator implements Iterator<ImageWriter> {


The code in case of shaing code :

enum SpiInfo {
    FORMAT_NAMES {
      @Override String[] info(ImageReaderWriterSpi spi) {
         return spi.getFormatNames();
      }
    },
    MIME_TYPES {
      @Override String[] info(ImageReaderWriterSpi spi) {
        return spi.getMIMETypes();
       }
    },
    FILE_SUFFIXES {
      @Override String[] info(ImageReaderWriterSpi spi) {
        return spi.getFileSuffixes();
      }
    };
    
    abstract String[] info(ImageReaderWriterSpi spi);
  }
    
  public static String[] getWriterFormatNames() {
    return getReaderWriterInfos(ImageWriterSpi.class,SpiInfo.FORMAT_NAMES);
  }
  
  public static String[] getWriterMIMETypes() {
    return getReaderWriterInfos(ImageWriterSpi.class,SpiInfo.MIME_TYPES);
  }
  
  public static String[] getWriterFileSuffixes() {
    return getReaderWriterInfos(ImageWriterSpi.class,SpiInfo.FILE_SUFFIXES);
  }
  
  public static String[] getReaderFormatNames() {
    return getReaderWriterInfos(ImageReaderSpi.class,SpiInfo.FORMAT_NAMES);
  }
    
  public static String[] getReaderMIMETypes() {
    return getReaderWriterInfos(ImageReaderSpi.class,SpiInfo.MIME_TYPES);
  }
    
  public static String[] getReaderFileSuffixes() {
    return getReaderWriterInfos(ImageReaderSpi.class,SpiInfo.FILE_SUFFIXES);
  }
  
  private static <S extends ImageReaderWriterSpi>
    String[] getReaderWriterInfos(Class<S> spiClass,SpiInfo info) {
        
        Iterator<S> iter;
        // Ensure category is present
        try {
            iter = theRegistry.getServiceProviders(spiClass, true);
        } catch (IllegalArgumentException e) {
            return new String[0];
        }

        HashSet<String> s = new HashSet<String>();
        while (iter.hasNext()) {
            ImageReaderWriterSpi spi = iter.next();
            Collections.addAll(s,spi.getFileSuffixes());
        }

        return s.toArray(new String[s.size()]);
    }

R����mi Forax

JUnit TESTCASE :
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;

public class Test {

    public static void testGetReaderFileSuffixes() {
      String[] suffixes=ImageIO.getReaderFileSuffixes();
      
      for(String suffixe:suffixes) {
        Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix(suffixe);
        if (!it.hasNext())
          throw new AssertionError("getReaderFileSuffixes returns an unknown suffixe");
      }
    }
    
    public static void testGetWriterFileSuffixes() {
      String[] suffixes=ImageIO.getWriterFileSuffixes();
        
      for(String suffixe:suffixes) {
        Iterator<ImageWriter> it = ImageIO.getImageWritersBySuffix(suffixe);
        if (!it.hasNext())
          throw new AssertionError("getWriterFileSuffixes returns an unknown suffixe");
      }
    }
    
    public static void main(String[] args) {
        testGetReaderFileSuffixes();
        testGetWriterFileSuffixes();
    }

}


FIX FOR BUG NUMBER:
4703112

Comments
EVALUATION Contribution-Forum:https://jdk-collaboration.dev.java.net/servlets/ProjectForumMessageView?forumID=1463&messageID=10399
06-12-2005

EVALUATION A complete fix and testcase has been provided by a developer from the jdk-collaboration community, which was attached to this bug report. This is a low-risk RFE, so we should try to get this in for Mustang. Will require a CCC request though.
30-11-2005

EVALUATION Missed mustang, retarget to dolphin.
22-09-2005

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mustang
21-08-2004

EVALUATION This information is available via the SPI. Also the ImageIO class will return the list of informal names via the ImageIO.getWriterFormatNames() method. It would be possible however to add an additional convenience method to the ImageIO class. If doing this then a getWriterSuffixes() method should be added too. ###@###.### 2002-06-17 ============================= This is a reasonable request, and should be simple to add these two new methods to the ImageIO convenience class. Committing to fix in Mustang. ###@###.### 2004-07-19
19-07-2004