United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4703112 RFE: Please add ImageIO.getReaderFileSuffixes() method
JDK-4703112 : RFE: Please add ImageIO.getReaderFileSuffixes() method

Details
Type:
Enhancement
Submit Date:
2002-06-17
Status:
Resolved
Updated Date:
2006-01-09
Project Name:
JDK
Resolved Date:
2006-01-09
Component:
client-libs
OS:
generic,windows_2000
Sub-Component:
javax.imageio
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports

Sub Tasks

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

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
                                     
2004-07-19
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
mustang


                                     
2004-08-21
EVALUATION

Missed mustang, retarget to dolphin.
                                     
2005-09-22
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.
                                     
2005-11-30
EVALUATION

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



Hardware and Software, Engineered to Work Together