src/share/classes/javax/imageio/ImageReader.java

Print this page
rev 9344 : 8034998: Fix raw and unchecked lint warnings in javax.imageio
Reviewed-by: darcy, prr


 776      * a document from the returned <code>IIOMetadata</code> object.
 777      * @param nodeNames a <code>Set</code> containing the names of
 778      * nodes that may be contained in a retrieved document.
 779      *
 780      * @return an <code>IIOMetadata</code> object, or <code>null</code>.
 781      *
 782      * @exception IllegalArgumentException if <code>formatName</code>
 783      * is <code>null</code>.
 784      * @exception IllegalArgumentException if <code>nodeNames</code>
 785      * is <code>null</code>.
 786      * @exception IOException if an error occurs during reading.
 787      */
 788     public IIOMetadata getStreamMetadata(String formatName,
 789                                          Set<String> nodeNames)
 790         throws IOException
 791     {
 792         return getMetadata(formatName, nodeNames, true, 0);
 793     }
 794 
 795     private IIOMetadata getMetadata(String formatName,
 796                                     Set nodeNames,
 797                                     boolean wantStream,
 798                                     int imageIndex) throws IOException {
 799         if (formatName == null) {
 800             throw new IllegalArgumentException("formatName == null!");
 801         }
 802         if (nodeNames == null) {
 803             throw new IllegalArgumentException("nodeNames == null!");
 804         }
 805         IIOMetadata metadata =
 806             wantStream
 807             ? getStreamMetadata()
 808             : getImageMetadata(imageIndex);
 809         if (metadata != null) {
 810             if (metadata.isStandardMetadataFormatSupported() &&
 811                 formatName.equals
 812                 (IIOMetadataFormatImpl.standardMetadataFormatName)) {
 813                 return metadata;
 814             }
 815             String nativeName = metadata.getNativeMetadataFormatName();
 816             if (nativeName != null && formatName.equals(nativeName)) {


1048      * set.
1049      * @exception IndexOutOfBoundsException if the supplied index is
1050      * out of bounds.
1051      * @exception IllegalArgumentException if the set of source and
1052      * destination bands specified by
1053      * <code>param.getSourceBands</code> and
1054      * <code>param.getDestinationBands</code> differ in length or
1055      * include indices that are out of bounds.
1056      * @exception IllegalArgumentException if the resulting image
1057      * would have a width or height less than 1.
1058      * @exception IOException if an error occurs during reading.
1059      */
1060     public IIOImage readAll(int imageIndex, ImageReadParam param)
1061         throws IOException {
1062         if (imageIndex < getMinIndex()) {
1063             throw new IndexOutOfBoundsException("imageIndex < getMinIndex()!");
1064         }
1065 
1066         BufferedImage im = read(imageIndex, param);
1067 
1068         ArrayList thumbnails = null;
1069         int numThumbnails = getNumThumbnails(imageIndex);
1070         if (numThumbnails > 0) {
1071             thumbnails = new ArrayList();
1072             for (int j = 0; j < numThumbnails; j++) {
1073                 thumbnails.add(readThumbnail(imageIndex, j));
1074             }
1075         }
1076 
1077         IIOMetadata metadata = getImageMetadata(imageIndex);
1078         return new IIOImage(im, thumbnails, metadata);
1079     }
1080 
1081     /**
1082      * Returns an <code>Iterator</code> containing all the images,
1083      * thumbnails, and metadata, starting at the index given by
1084      * <code>getMinIndex</code>, from the input source in the form of
1085      * <code>IIOImage</code> objects.  An <code>Iterator</code>
1086      * containing <code>ImageReadParam</code> objects is supplied; one
1087      * element is consumed for each image read from the input source
1088      * until no more images are available.  If the read param
1089      * <code>Iterator</code> runs out of elements, but there are still
1090      * more images available from the input source, default read
1091      * params are used for the remaining images.


1139      * set.
1140      * @exception IllegalArgumentException if any
1141      * non-<code>null</code> element of <code>params</code> is not an
1142      * <code>ImageReadParam</code>.
1143      * @exception IllegalArgumentException if the set of source and
1144      * destination bands specified by
1145      * <code>param.getSourceBands</code> and
1146      * <code>param.getDestinationBands</code> differ in length or
1147      * include indices that are out of bounds.
1148      * @exception IllegalArgumentException if a resulting image would
1149      * have a width or height less than 1.
1150      * @exception IOException if an error occurs during reading.
1151      *
1152      * @see ImageReadParam
1153      * @see IIOImage
1154      */
1155     public Iterator<IIOImage>
1156         readAll(Iterator<? extends ImageReadParam> params)
1157         throws IOException
1158     {
1159         List output = new ArrayList();
1160 
1161         int imageIndex = getMinIndex();
1162 
1163         // Inform IIOReadProgressListeners we're starting a sequence
1164         processSequenceStarted(imageIndex);
1165 
1166         while (true) {
1167             // Inform IIOReadProgressListeners and IIOReadUpdateListeners
1168             // that we're starting a new image
1169 
1170             ImageReadParam param = null;
1171             if (params != null && params.hasNext()) {
1172                 Object o = params.next();
1173                 if (o != null) {
1174                     if (o instanceof ImageReadParam) {
1175                         param = (ImageReadParam)o;
1176                     } else {
1177                         throw new IllegalArgumentException
1178                             ("Non-ImageReadParam supplied as part of params!");
1179                     }
1180                 }
1181             }
1182 
1183             BufferedImage bi = null;
1184             try {
1185                 bi = read(imageIndex, param);
1186             } catch (IndexOutOfBoundsException e) {
1187                 break;
1188             }
1189 
1190             ArrayList thumbnails = null;
1191             int numThumbnails = getNumThumbnails(imageIndex);
1192             if (numThumbnails > 0) {
1193                 thumbnails = new ArrayList();
1194                 for (int j = 0; j < numThumbnails; j++) {
1195                     thumbnails.add(readThumbnail(imageIndex, j));
1196                 }
1197             }
1198 
1199             IIOMetadata metadata = getImageMetadata(imageIndex);
1200             IIOImage im = new IIOImage(bi, thumbnails, metadata);
1201             output.add(im);
1202 
1203             ++imageIndex;
1204         }
1205 
1206         // Inform IIOReadProgressListeners we're ending a sequence
1207         processSequenceComplete();
1208 
1209         return output.iterator();
1210     }
1211 
1212     /**
1213      * Returns <code>true</code> if this plug-in supports reading


1780     protected synchronized boolean abortRequested() {
1781         return this.abortFlag;
1782     }
1783 
1784     /**
1785      * Clears any previous abort request.  After this method has been
1786      * called, <code>abortRequested</code> will return
1787      * <code>false</code>.
1788      *
1789      * @see #abort
1790      * @see #abortRequested
1791      */
1792     protected synchronized void clearAbortRequest() {
1793         this.abortFlag = false;
1794     }
1795 
1796     // Listeners
1797 
1798     // Add an element to a list, creating a new list if the
1799     // existing list is null, and return the list.
1800     static List addToList(List l, Object elt) {
1801         if (l == null) {
1802             l = new ArrayList();
1803         }
1804         l.add(elt);
1805         return l;
1806     }
1807 
1808 
1809     // Remove an element from a list, discarding the list if the
1810     // resulting list is empty, and return the list or null.
1811     static List removeFromList(List l, Object elt) {
1812         if (l == null) {
1813             return l;
1814         }
1815         l.remove(elt);
1816         if (l.size() == 0) {
1817             l = null;
1818         }
1819         return l;
1820     }
1821 
1822     /**
1823      * Adds an <code>IIOReadWarningListener</code> to the list of
1824      * registered warning listeners.  If <code>listener</code> is
1825      * <code>null</code>, no exception will be thrown and no action
1826      * will be taken.  Messages sent to the given listener will be
1827      * localized, if possible, to match the current
1828      * <code>Locale</code>.  If no <code>Locale</code> has been set,
1829      * warning messages may be localized as the reader sees fit.
1830      *
1831      * @param listener an <code>IIOReadWarningListener</code> to be registered.


2444         if (keyword == null) {
2445             throw new IllegalArgumentException("keyword == null!");
2446         }
2447         int numListeners = warningListeners.size();
2448         for (int i = 0; i < numListeners; i++) {
2449             IIOReadWarningListener listener =
2450                 warningListeners.get(i);
2451             Locale locale = warningLocales.get(i);
2452             if (locale == null) {
2453                 locale = Locale.getDefault();
2454             }
2455 
2456             /**
2457              * If an applet supplies an implementation of ImageReader and
2458              * resource bundles, then the resource bundle will need to be
2459              * accessed via the applet class loader. So first try the context
2460              * class loader to locate the resource bundle.
2461              * If that throws MissingResourceException, then try the
2462              * system class loader.
2463              */
2464             ClassLoader loader = (ClassLoader)
2465                 java.security.AccessController.doPrivileged(
2466                    new java.security.PrivilegedAction() {
2467                       public Object run() {
2468                         return Thread.currentThread().getContextClassLoader();
2469                       }
2470                 });
2471 
2472             ResourceBundle bundle = null;
2473             try {
2474                 bundle = ResourceBundle.getBundle(baseName, locale, loader);
2475             } catch (MissingResourceException mre) {
2476                 try {
2477                     bundle = ResourceBundle.getBundle(baseName, locale);
2478                 } catch (MissingResourceException mre1) {
2479                     throw new IllegalArgumentException("Bundle not found!");
2480                 }
2481             }
2482 
2483             String warning = null;
2484             try {
2485                 warning = bundle.getString(keyword);
2486             } catch (ClassCastException cce) {
2487                 throw new IllegalArgumentException("Resource is not a String!");




 776      * a document from the returned <code>IIOMetadata</code> object.
 777      * @param nodeNames a <code>Set</code> containing the names of
 778      * nodes that may be contained in a retrieved document.
 779      *
 780      * @return an <code>IIOMetadata</code> object, or <code>null</code>.
 781      *
 782      * @exception IllegalArgumentException if <code>formatName</code>
 783      * is <code>null</code>.
 784      * @exception IllegalArgumentException if <code>nodeNames</code>
 785      * is <code>null</code>.
 786      * @exception IOException if an error occurs during reading.
 787      */
 788     public IIOMetadata getStreamMetadata(String formatName,
 789                                          Set<String> nodeNames)
 790         throws IOException
 791     {
 792         return getMetadata(formatName, nodeNames, true, 0);
 793     }
 794 
 795     private IIOMetadata getMetadata(String formatName,
 796                                     Set<String> nodeNames,
 797                                     boolean wantStream,
 798                                     int imageIndex) throws IOException {
 799         if (formatName == null) {
 800             throw new IllegalArgumentException("formatName == null!");
 801         }
 802         if (nodeNames == null) {
 803             throw new IllegalArgumentException("nodeNames == null!");
 804         }
 805         IIOMetadata metadata =
 806             wantStream
 807             ? getStreamMetadata()
 808             : getImageMetadata(imageIndex);
 809         if (metadata != null) {
 810             if (metadata.isStandardMetadataFormatSupported() &&
 811                 formatName.equals
 812                 (IIOMetadataFormatImpl.standardMetadataFormatName)) {
 813                 return metadata;
 814             }
 815             String nativeName = metadata.getNativeMetadataFormatName();
 816             if (nativeName != null && formatName.equals(nativeName)) {


1048      * set.
1049      * @exception IndexOutOfBoundsException if the supplied index is
1050      * out of bounds.
1051      * @exception IllegalArgumentException if the set of source and
1052      * destination bands specified by
1053      * <code>param.getSourceBands</code> and
1054      * <code>param.getDestinationBands</code> differ in length or
1055      * include indices that are out of bounds.
1056      * @exception IllegalArgumentException if the resulting image
1057      * would have a width or height less than 1.
1058      * @exception IOException if an error occurs during reading.
1059      */
1060     public IIOImage readAll(int imageIndex, ImageReadParam param)
1061         throws IOException {
1062         if (imageIndex < getMinIndex()) {
1063             throw new IndexOutOfBoundsException("imageIndex < getMinIndex()!");
1064         }
1065 
1066         BufferedImage im = read(imageIndex, param);
1067 
1068         ArrayList<BufferedImage> thumbnails = null;
1069         int numThumbnails = getNumThumbnails(imageIndex);
1070         if (numThumbnails > 0) {
1071             thumbnails = new ArrayList<>();
1072             for (int j = 0; j < numThumbnails; j++) {
1073                 thumbnails.add(readThumbnail(imageIndex, j));
1074             }
1075         }
1076 
1077         IIOMetadata metadata = getImageMetadata(imageIndex);
1078         return new IIOImage(im, thumbnails, metadata);
1079     }
1080 
1081     /**
1082      * Returns an <code>Iterator</code> containing all the images,
1083      * thumbnails, and metadata, starting at the index given by
1084      * <code>getMinIndex</code>, from the input source in the form of
1085      * <code>IIOImage</code> objects.  An <code>Iterator</code>
1086      * containing <code>ImageReadParam</code> objects is supplied; one
1087      * element is consumed for each image read from the input source
1088      * until no more images are available.  If the read param
1089      * <code>Iterator</code> runs out of elements, but there are still
1090      * more images available from the input source, default read
1091      * params are used for the remaining images.


1139      * set.
1140      * @exception IllegalArgumentException if any
1141      * non-<code>null</code> element of <code>params</code> is not an
1142      * <code>ImageReadParam</code>.
1143      * @exception IllegalArgumentException if the set of source and
1144      * destination bands specified by
1145      * <code>param.getSourceBands</code> and
1146      * <code>param.getDestinationBands</code> differ in length or
1147      * include indices that are out of bounds.
1148      * @exception IllegalArgumentException if a resulting image would
1149      * have a width or height less than 1.
1150      * @exception IOException if an error occurs during reading.
1151      *
1152      * @see ImageReadParam
1153      * @see IIOImage
1154      */
1155     public Iterator<IIOImage>
1156         readAll(Iterator<? extends ImageReadParam> params)
1157         throws IOException
1158     {
1159         List<IIOImage> output = new ArrayList<>();
1160 
1161         int imageIndex = getMinIndex();
1162 
1163         // Inform IIOReadProgressListeners we're starting a sequence
1164         processSequenceStarted(imageIndex);
1165 
1166         while (true) {
1167             // Inform IIOReadProgressListeners and IIOReadUpdateListeners
1168             // that we're starting a new image
1169 
1170             ImageReadParam param = null;
1171             if (params != null && params.hasNext()) {
1172                 Object o = params.next();
1173                 if (o != null) {
1174                     if (o instanceof ImageReadParam) {
1175                         param = (ImageReadParam)o;
1176                     } else {
1177                         throw new IllegalArgumentException
1178                             ("Non-ImageReadParam supplied as part of params!");
1179                     }
1180                 }
1181             }
1182 
1183             BufferedImage bi = null;
1184             try {
1185                 bi = read(imageIndex, param);
1186             } catch (IndexOutOfBoundsException e) {
1187                 break;
1188             }
1189 
1190             ArrayList<BufferedImage> thumbnails = null;
1191             int numThumbnails = getNumThumbnails(imageIndex);
1192             if (numThumbnails > 0) {
1193                 thumbnails = new ArrayList<>();
1194                 for (int j = 0; j < numThumbnails; j++) {
1195                     thumbnails.add(readThumbnail(imageIndex, j));
1196                 }
1197             }
1198 
1199             IIOMetadata metadata = getImageMetadata(imageIndex);
1200             IIOImage im = new IIOImage(bi, thumbnails, metadata);
1201             output.add(im);
1202 
1203             ++imageIndex;
1204         }
1205 
1206         // Inform IIOReadProgressListeners we're ending a sequence
1207         processSequenceComplete();
1208 
1209         return output.iterator();
1210     }
1211 
1212     /**
1213      * Returns <code>true</code> if this plug-in supports reading


1780     protected synchronized boolean abortRequested() {
1781         return this.abortFlag;
1782     }
1783 
1784     /**
1785      * Clears any previous abort request.  After this method has been
1786      * called, <code>abortRequested</code> will return
1787      * <code>false</code>.
1788      *
1789      * @see #abort
1790      * @see #abortRequested
1791      */
1792     protected synchronized void clearAbortRequest() {
1793         this.abortFlag = false;
1794     }
1795 
1796     // Listeners
1797 
1798     // Add an element to a list, creating a new list if the
1799     // existing list is null, and return the list.
1800     static <T> List<T> addToList(List<T> l, T elt) {
1801         if (l == null) {
1802             l = new ArrayList<>();
1803         }
1804         l.add(elt);
1805         return l;
1806     }
1807 
1808 
1809     // Remove an element from a list, discarding the list if the
1810     // resulting list is empty, and return the list or null.
1811     static <T> List<T> removeFromList(List<T> l, T elt) {
1812         if (l == null) {
1813             return l;
1814         }
1815         l.remove(elt);
1816         if (l.size() == 0) {
1817             l = null;
1818         }
1819         return l;
1820     }
1821 
1822     /**
1823      * Adds an <code>IIOReadWarningListener</code> to the list of
1824      * registered warning listeners.  If <code>listener</code> is
1825      * <code>null</code>, no exception will be thrown and no action
1826      * will be taken.  Messages sent to the given listener will be
1827      * localized, if possible, to match the current
1828      * <code>Locale</code>.  If no <code>Locale</code> has been set,
1829      * warning messages may be localized as the reader sees fit.
1830      *
1831      * @param listener an <code>IIOReadWarningListener</code> to be registered.


2444         if (keyword == null) {
2445             throw new IllegalArgumentException("keyword == null!");
2446         }
2447         int numListeners = warningListeners.size();
2448         for (int i = 0; i < numListeners; i++) {
2449             IIOReadWarningListener listener =
2450                 warningListeners.get(i);
2451             Locale locale = warningLocales.get(i);
2452             if (locale == null) {
2453                 locale = Locale.getDefault();
2454             }
2455 
2456             /**
2457              * If an applet supplies an implementation of ImageReader and
2458              * resource bundles, then the resource bundle will need to be
2459              * accessed via the applet class loader. So first try the context
2460              * class loader to locate the resource bundle.
2461              * If that throws MissingResourceException, then try the
2462              * system class loader.
2463              */
2464             ClassLoader loader =
2465                 java.security.AccessController.doPrivileged(
2466                    new java.security.PrivilegedAction<ClassLoader>() {
2467                       public ClassLoader run() {
2468                         return Thread.currentThread().getContextClassLoader();
2469                       }
2470                 });
2471 
2472             ResourceBundle bundle = null;
2473             try {
2474                 bundle = ResourceBundle.getBundle(baseName, locale, loader);
2475             } catch (MissingResourceException mre) {
2476                 try {
2477                     bundle = ResourceBundle.getBundle(baseName, locale);
2478                 } catch (MissingResourceException mre1) {
2479                     throw new IllegalArgumentException("Bundle not found!");
2480                 }
2481             }
2482 
2483             String warning = null;
2484             try {
2485                 warning = bundle.getString(keyword);
2486             } catch (ClassCastException cce) {
2487                 throw new IllegalArgumentException("Resource is not a String!");