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

Print this page


   1 /*
   2  * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 240         return output;
 241     }
 242 
 243     // Localization
 244 
 245     /**
 246      * Returns an array of <code>Locale</code>s that may be used to
 247      * localize warning listeners and compression settings.  A return
 248      * value of <code>null</code> indicates that localization is not
 249      * supported.
 250      *
 251      * <p> The default implementation returns a clone of the
 252      * <code>availableLocales</code> instance variable if it is
 253      * non-<code>null</code>, or else returns <code>null</code>.
 254      *
 255      * @return an array of <code>Locale</code>s that may be used as
 256      * arguments to <code>setLocale</code>, or <code>null</code>.
 257      */
 258     public Locale[] getAvailableLocales() {
 259         return (availableLocales == null) ?
 260             null : (Locale[])availableLocales.clone();
 261     }
 262 
 263     /**
 264      * Sets the current <code>Locale</code> of this
 265      * <code>ImageWriter</code> to the given value.  A value of
 266      * <code>null</code> removes any previous setting, and indicates
 267      * that the writer should localize as it sees fit.
 268      *
 269      * <p> The default implementation checks <code>locale</code>
 270      * against the values returned by
 271      * <code>getAvailableLocales</code>, and sets the
 272      * <code>locale</code> instance variable if it is found.  If
 273      * <code>locale</code> is <code>null</code>, the instance variable
 274      * is set to <code>null</code> without any checking.
 275      *
 276      * @param locale the desired <code>Locale</code>, or
 277      * <code>null</code>.
 278      *
 279      * @exception IllegalArgumentException if <code>locale</code> is
 280      * non-<code>null</code> but is not one of the values returned by


1737      */
1738     public void removeAllIIOWriteProgressListeners() {
1739         this.progressListeners = null;
1740     }
1741 
1742     /**
1743      * Broadcasts the start of an image write to all registered
1744      * <code>IIOWriteProgressListener</code>s by calling their
1745      * <code>imageStarted</code> method.  Subclasses may use this
1746      * method as a convenience.
1747      *
1748      * @param imageIndex the index of the image about to be written.
1749      */
1750     protected void processImageStarted(int imageIndex) {
1751         if (progressListeners == null) {
1752             return;
1753         }
1754         int numListeners = progressListeners.size();
1755         for (int i = 0; i < numListeners; i++) {
1756             IIOWriteProgressListener listener =
1757                 (IIOWriteProgressListener)progressListeners.get(i);
1758             listener.imageStarted(this, imageIndex);
1759         }
1760     }
1761 
1762     /**
1763      * Broadcasts the current percentage of image completion to all
1764      * registered <code>IIOWriteProgressListener</code>s by calling
1765      * their <code>imageProgress</code> method.  Subclasses may use
1766      * this method as a convenience.
1767      *
1768      * @param percentageDone the current percentage of completion,
1769      * as a <code>float</code>.
1770      */
1771     protected void processImageProgress(float percentageDone) {
1772         if (progressListeners == null) {
1773             return;
1774         }
1775         int numListeners = progressListeners.size();
1776         for (int i = 0; i < numListeners; i++) {
1777             IIOWriteProgressListener listener =
1778                 (IIOWriteProgressListener)progressListeners.get(i);
1779             listener.imageProgress(this, percentageDone);
1780         }
1781     }
1782 
1783     /**
1784      * Broadcasts the completion of an image write to all registered
1785      * <code>IIOWriteProgressListener</code>s by calling their
1786      * <code>imageComplete</code> method.  Subclasses may use this
1787      * method as a convenience.
1788      */
1789     protected void processImageComplete() {
1790         if (progressListeners == null) {
1791             return;
1792         }
1793         int numListeners = progressListeners.size();
1794         for (int i = 0; i < numListeners; i++) {
1795             IIOWriteProgressListener listener =
1796                 (IIOWriteProgressListener)progressListeners.get(i);
1797             listener.imageComplete(this);
1798         }
1799     }
1800 
1801     /**
1802      * Broadcasts the start of a thumbnail write to all registered
1803      * <code>IIOWriteProgressListener</code>s by calling their
1804      * <code>thumbnailStarted</code> method.  Subclasses may use this
1805      * method as a convenience.
1806      *
1807      * @param imageIndex the index of the image associated with the
1808      * thumbnail.
1809      * @param thumbnailIndex the index of the thumbnail.
1810      */
1811     protected void processThumbnailStarted(int imageIndex,
1812                                            int thumbnailIndex) {
1813         if (progressListeners == null) {
1814             return;
1815         }
1816         int numListeners = progressListeners.size();
1817         for (int i = 0; i < numListeners; i++) {
1818             IIOWriteProgressListener listener =
1819                 (IIOWriteProgressListener)progressListeners.get(i);
1820             listener.thumbnailStarted(this, imageIndex, thumbnailIndex);
1821         }
1822     }
1823 
1824     /**
1825      * Broadcasts the current percentage of thumbnail completion to
1826      * all registered <code>IIOWriteProgressListener</code>s by calling
1827      * their <code>thumbnailProgress</code> method.  Subclasses may
1828      * use this method as a convenience.
1829      *
1830      * @param percentageDone the current percentage of completion,
1831      * as a <code>float</code>.
1832      */
1833     protected void processThumbnailProgress(float percentageDone) {
1834         if (progressListeners == null) {
1835             return;
1836         }
1837         int numListeners = progressListeners.size();
1838         for (int i = 0; i < numListeners; i++) {
1839             IIOWriteProgressListener listener =
1840                 (IIOWriteProgressListener)progressListeners.get(i);
1841             listener.thumbnailProgress(this, percentageDone);
1842         }
1843     }
1844 
1845     /**
1846      * Broadcasts the completion of a thumbnail write to all registered
1847      * <code>IIOWriteProgressListener</code>s by calling their
1848      * <code>thumbnailComplete</code> method.  Subclasses may use this
1849      * method as a convenience.
1850      */
1851     protected void processThumbnailComplete() {
1852         if (progressListeners == null) {
1853             return;
1854         }
1855         int numListeners = progressListeners.size();
1856         for (int i = 0; i < numListeners; i++) {
1857             IIOWriteProgressListener listener =
1858                 (IIOWriteProgressListener)progressListeners.get(i);
1859             listener.thumbnailComplete(this);
1860         }
1861     }
1862 
1863     /**
1864      * Broadcasts that the write has been aborted to all registered
1865      * <code>IIOWriteProgressListener</code>s by calling their
1866      * <code>writeAborted</code> method.  Subclasses may use this
1867      * method as a convenience.
1868      */
1869     protected void processWriteAborted() {
1870         if (progressListeners == null) {
1871             return;
1872         }
1873         int numListeners = progressListeners.size();
1874         for (int i = 0; i < numListeners; i++) {
1875             IIOWriteProgressListener listener =
1876                 (IIOWriteProgressListener)progressListeners.get(i);
1877             listener.writeAborted(this);
1878         }
1879     }
1880 
1881     /**
1882      * Broadcasts a warning message to all registered
1883      * <code>IIOWriteWarningListener</code>s by calling their
1884      * <code>warningOccurred</code> method.  Subclasses may use this
1885      * method as a convenience.
1886      *
1887      * @param imageIndex the index of the image on which the warning
1888      * occurred.
1889      * @param warning the warning message.
1890      *
1891      * @exception IllegalArgumentException if <code>warning</code>
1892      * is <code>null</code>.
1893      */
1894     protected void processWarningOccurred(int imageIndex,
1895                                           String warning) {
1896         if (warningListeners == null) {
1897             return;
1898         }
1899         if (warning == null) {
1900             throw new IllegalArgumentException("warning == null!");
1901         }
1902         int numListeners = warningListeners.size();
1903         for (int i = 0; i < numListeners; i++) {
1904             IIOWriteWarningListener listener =
1905                 (IIOWriteWarningListener)warningListeners.get(i);
1906 
1907             listener.warningOccurred(this, imageIndex, warning);
1908         }
1909     }
1910 
1911     /**
1912      * Broadcasts a localized warning message to all registered
1913      * <code>IIOWriteWarningListener</code>s by calling their
1914      * <code>warningOccurred</code> method with a string taken
1915      * from a <code>ResourceBundle</code>.  Subclasses may use this
1916      * method as a convenience.
1917      *
1918      * @param imageIndex the index of the image on which the warning
1919      * occurred.
1920      * @param baseName the base name of a set of
1921      * <code>ResourceBundle</code>s containing localized warning
1922      * messages.
1923      * @param keyword the keyword used to index the warning message
1924      * within the set of <code>ResourceBundle</code>s.
1925      *


1933      * not found in the located <code>ResourceBundle</code>.
1934      * @exception IllegalArgumentException if the object retrieved
1935      * from the <code>ResourceBundle</code> is not a
1936      * <code>String</code>.
1937      */
1938     protected void processWarningOccurred(int imageIndex,
1939                                           String baseName,
1940                                           String keyword) {
1941         if (warningListeners == null) {
1942             return;
1943         }
1944         if (baseName == null) {
1945             throw new IllegalArgumentException("baseName == null!");
1946         }
1947         if (keyword == null) {
1948             throw new IllegalArgumentException("keyword == null!");
1949         }
1950         int numListeners = warningListeners.size();
1951         for (int i = 0; i < numListeners; i++) {
1952             IIOWriteWarningListener listener =
1953                 (IIOWriteWarningListener)warningListeners.get(i);
1954             Locale locale = (Locale)warningLocales.get(i);
1955             if (locale == null) {
1956                 locale = Locale.getDefault();
1957             }
1958 
1959             /**
1960              * If an applet supplies an implementation of ImageWriter and
1961              * resource bundles, then the resource bundle will need to be
1962              * accessed via the applet class loader. So first try the context
1963              * class loader to locate the resource bundle.
1964              * If that throws MissingResourceException, then try the
1965              * system class loader.
1966              */
1967             ClassLoader loader = (ClassLoader)
1968                 java.security.AccessController.doPrivileged(
1969                    new java.security.PrivilegedAction() {
1970                       public Object run() {
1971                         return Thread.currentThread().getContextClassLoader();
1972                       }
1973                 });
1974 


   1 /*
   2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 240         return output;
 241     }
 242 
 243     // Localization
 244 
 245     /**
 246      * Returns an array of <code>Locale</code>s that may be used to
 247      * localize warning listeners and compression settings.  A return
 248      * value of <code>null</code> indicates that localization is not
 249      * supported.
 250      *
 251      * <p> The default implementation returns a clone of the
 252      * <code>availableLocales</code> instance variable if it is
 253      * non-<code>null</code>, or else returns <code>null</code>.
 254      *
 255      * @return an array of <code>Locale</code>s that may be used as
 256      * arguments to <code>setLocale</code>, or <code>null</code>.
 257      */
 258     public Locale[] getAvailableLocales() {
 259         return (availableLocales == null) ?
 260             null : availableLocales.clone();
 261     }
 262 
 263     /**
 264      * Sets the current <code>Locale</code> of this
 265      * <code>ImageWriter</code> to the given value.  A value of
 266      * <code>null</code> removes any previous setting, and indicates
 267      * that the writer should localize as it sees fit.
 268      *
 269      * <p> The default implementation checks <code>locale</code>
 270      * against the values returned by
 271      * <code>getAvailableLocales</code>, and sets the
 272      * <code>locale</code> instance variable if it is found.  If
 273      * <code>locale</code> is <code>null</code>, the instance variable
 274      * is set to <code>null</code> without any checking.
 275      *
 276      * @param locale the desired <code>Locale</code>, or
 277      * <code>null</code>.
 278      *
 279      * @exception IllegalArgumentException if <code>locale</code> is
 280      * non-<code>null</code> but is not one of the values returned by


1737      */
1738     public void removeAllIIOWriteProgressListeners() {
1739         this.progressListeners = null;
1740     }
1741 
1742     /**
1743      * Broadcasts the start of an image write to all registered
1744      * <code>IIOWriteProgressListener</code>s by calling their
1745      * <code>imageStarted</code> method.  Subclasses may use this
1746      * method as a convenience.
1747      *
1748      * @param imageIndex the index of the image about to be written.
1749      */
1750     protected void processImageStarted(int imageIndex) {
1751         if (progressListeners == null) {
1752             return;
1753         }
1754         int numListeners = progressListeners.size();
1755         for (int i = 0; i < numListeners; i++) {
1756             IIOWriteProgressListener listener =
1757                 progressListeners.get(i);
1758             listener.imageStarted(this, imageIndex);
1759         }
1760     }
1761 
1762     /**
1763      * Broadcasts the current percentage of image completion to all
1764      * registered <code>IIOWriteProgressListener</code>s by calling
1765      * their <code>imageProgress</code> method.  Subclasses may use
1766      * this method as a convenience.
1767      *
1768      * @param percentageDone the current percentage of completion,
1769      * as a <code>float</code>.
1770      */
1771     protected void processImageProgress(float percentageDone) {
1772         if (progressListeners == null) {
1773             return;
1774         }
1775         int numListeners = progressListeners.size();
1776         for (int i = 0; i < numListeners; i++) {
1777             IIOWriteProgressListener listener =
1778                 progressListeners.get(i);
1779             listener.imageProgress(this, percentageDone);
1780         }
1781     }
1782 
1783     /**
1784      * Broadcasts the completion of an image write to all registered
1785      * <code>IIOWriteProgressListener</code>s by calling their
1786      * <code>imageComplete</code> method.  Subclasses may use this
1787      * method as a convenience.
1788      */
1789     protected void processImageComplete() {
1790         if (progressListeners == null) {
1791             return;
1792         }
1793         int numListeners = progressListeners.size();
1794         for (int i = 0; i < numListeners; i++) {
1795             IIOWriteProgressListener listener =
1796                 progressListeners.get(i);
1797             listener.imageComplete(this);
1798         }
1799     }
1800 
1801     /**
1802      * Broadcasts the start of a thumbnail write to all registered
1803      * <code>IIOWriteProgressListener</code>s by calling their
1804      * <code>thumbnailStarted</code> method.  Subclasses may use this
1805      * method as a convenience.
1806      *
1807      * @param imageIndex the index of the image associated with the
1808      * thumbnail.
1809      * @param thumbnailIndex the index of the thumbnail.
1810      */
1811     protected void processThumbnailStarted(int imageIndex,
1812                                            int thumbnailIndex) {
1813         if (progressListeners == null) {
1814             return;
1815         }
1816         int numListeners = progressListeners.size();
1817         for (int i = 0; i < numListeners; i++) {
1818             IIOWriteProgressListener listener =
1819                 progressListeners.get(i);
1820             listener.thumbnailStarted(this, imageIndex, thumbnailIndex);
1821         }
1822     }
1823 
1824     /**
1825      * Broadcasts the current percentage of thumbnail completion to
1826      * all registered <code>IIOWriteProgressListener</code>s by calling
1827      * their <code>thumbnailProgress</code> method.  Subclasses may
1828      * use this method as a convenience.
1829      *
1830      * @param percentageDone the current percentage of completion,
1831      * as a <code>float</code>.
1832      */
1833     protected void processThumbnailProgress(float percentageDone) {
1834         if (progressListeners == null) {
1835             return;
1836         }
1837         int numListeners = progressListeners.size();
1838         for (int i = 0; i < numListeners; i++) {
1839             IIOWriteProgressListener listener =
1840                 progressListeners.get(i);
1841             listener.thumbnailProgress(this, percentageDone);
1842         }
1843     }
1844 
1845     /**
1846      * Broadcasts the completion of a thumbnail write to all registered
1847      * <code>IIOWriteProgressListener</code>s by calling their
1848      * <code>thumbnailComplete</code> method.  Subclasses may use this
1849      * method as a convenience.
1850      */
1851     protected void processThumbnailComplete() {
1852         if (progressListeners == null) {
1853             return;
1854         }
1855         int numListeners = progressListeners.size();
1856         for (int i = 0; i < numListeners; i++) {
1857             IIOWriteProgressListener listener =
1858                 progressListeners.get(i);
1859             listener.thumbnailComplete(this);
1860         }
1861     }
1862 
1863     /**
1864      * Broadcasts that the write has been aborted to all registered
1865      * <code>IIOWriteProgressListener</code>s by calling their
1866      * <code>writeAborted</code> method.  Subclasses may use this
1867      * method as a convenience.
1868      */
1869     protected void processWriteAborted() {
1870         if (progressListeners == null) {
1871             return;
1872         }
1873         int numListeners = progressListeners.size();
1874         for (int i = 0; i < numListeners; i++) {
1875             IIOWriteProgressListener listener =
1876                 progressListeners.get(i);
1877             listener.writeAborted(this);
1878         }
1879     }
1880 
1881     /**
1882      * Broadcasts a warning message to all registered
1883      * <code>IIOWriteWarningListener</code>s by calling their
1884      * <code>warningOccurred</code> method.  Subclasses may use this
1885      * method as a convenience.
1886      *
1887      * @param imageIndex the index of the image on which the warning
1888      * occurred.
1889      * @param warning the warning message.
1890      *
1891      * @exception IllegalArgumentException if <code>warning</code>
1892      * is <code>null</code>.
1893      */
1894     protected void processWarningOccurred(int imageIndex,
1895                                           String warning) {
1896         if (warningListeners == null) {
1897             return;
1898         }
1899         if (warning == null) {
1900             throw new IllegalArgumentException("warning == null!");
1901         }
1902         int numListeners = warningListeners.size();
1903         for (int i = 0; i < numListeners; i++) {
1904             IIOWriteWarningListener listener =
1905                 warningListeners.get(i);
1906 
1907             listener.warningOccurred(this, imageIndex, warning);
1908         }
1909     }
1910 
1911     /**
1912      * Broadcasts a localized warning message to all registered
1913      * <code>IIOWriteWarningListener</code>s by calling their
1914      * <code>warningOccurred</code> method with a string taken
1915      * from a <code>ResourceBundle</code>.  Subclasses may use this
1916      * method as a convenience.
1917      *
1918      * @param imageIndex the index of the image on which the warning
1919      * occurred.
1920      * @param baseName the base name of a set of
1921      * <code>ResourceBundle</code>s containing localized warning
1922      * messages.
1923      * @param keyword the keyword used to index the warning message
1924      * within the set of <code>ResourceBundle</code>s.
1925      *


1933      * not found in the located <code>ResourceBundle</code>.
1934      * @exception IllegalArgumentException if the object retrieved
1935      * from the <code>ResourceBundle</code> is not a
1936      * <code>String</code>.
1937      */
1938     protected void processWarningOccurred(int imageIndex,
1939                                           String baseName,
1940                                           String keyword) {
1941         if (warningListeners == null) {
1942             return;
1943         }
1944         if (baseName == null) {
1945             throw new IllegalArgumentException("baseName == null!");
1946         }
1947         if (keyword == null) {
1948             throw new IllegalArgumentException("keyword == null!");
1949         }
1950         int numListeners = warningListeners.size();
1951         for (int i = 0; i < numListeners; i++) {
1952             IIOWriteWarningListener listener =
1953                 warningListeners.get(i);
1954             Locale locale = warningLocales.get(i);
1955             if (locale == null) {
1956                 locale = Locale.getDefault();
1957             }
1958 
1959             /**
1960              * If an applet supplies an implementation of ImageWriter and
1961              * resource bundles, then the resource bundle will need to be
1962              * accessed via the applet class loader. So first try the context
1963              * class loader to locate the resource bundle.
1964              * If that throws MissingResourceException, then try the
1965              * system class loader.
1966              */
1967             ClassLoader loader = (ClassLoader)
1968                 java.security.AccessController.doPrivileged(
1969                    new java.security.PrivilegedAction() {
1970                       public Object run() {
1971                         return Thread.currentThread().getContextClassLoader();
1972                       }
1973                 });
1974