< prev index next >

src/java.desktop/share/classes/javax/imageio/ImageIO.java

Print this page




1335      *
1336      * <p> This method <em>does not</em> close the provided
1337      * <code>InputStream</code> after the read operation has completed;
1338      * it is the responsibility of the caller to close the stream, if desired.
1339      *
1340      * @param input an <code>InputStream</code> to read from.
1341      *
1342      * @return a <code>BufferedImage</code> containing the decoded
1343      * contents of the input, or <code>null</code>.
1344      *
1345      * @exception IllegalArgumentException if <code>input</code> is
1346      * <code>null</code>.
1347      * @exception IOException if an error occurs during reading.
1348      */
1349     public static BufferedImage read(InputStream input) throws IOException {
1350         if (input == null) {
1351             throw new IllegalArgumentException("input == null!");
1352         }
1353 
1354         ImageInputStream stream = createImageInputStream(input);





1355         BufferedImage bi = read(stream);
1356         if (bi == null) {
1357             stream.close();
1358         }
1359         return bi;
1360     }
1361 
1362     /**
1363      * Returns a <code>BufferedImage</code> as the result of decoding
1364      * a supplied <code>URL</code> with an <code>ImageReader</code>
1365      * chosen automatically from among those currently registered.  An
1366      * <code>InputStream</code> is obtained from the <code>URL</code>,
1367      * which is wrapped in an <code>ImageInputStream</code>.  If no
1368      * registered <code>ImageReader</code> claims to be able to read
1369      * the resulting stream, <code>null</code> is returned.
1370      *
1371      * <p> The current cache settings from <code>getUseCache</code>and
1372      * <code>getCacheDirectory</code> will be used to control caching in the
1373      * <code>ImageInputStream</code> that is created.
1374      *


1381      *
1382      * @return a <code>BufferedImage</code> containing the decoded
1383      * contents of the input, or <code>null</code>.
1384      *
1385      * @exception IllegalArgumentException if <code>input</code> is
1386      * <code>null</code>.
1387      * @exception IOException if an error occurs during reading.
1388      */
1389     public static BufferedImage read(URL input) throws IOException {
1390         if (input == null) {
1391             throw new IllegalArgumentException("input == null!");
1392         }
1393 
1394         InputStream istream = null;
1395         try {
1396             istream = input.openStream();
1397         } catch (IOException e) {
1398             throw new IIOException("Can't get input stream from URL!", e);
1399         }
1400         ImageInputStream stream = createImageInputStream(istream);





1401         BufferedImage bi;
1402         try {
1403             bi = read(stream);
1404             if (bi == null) {
1405                 stream.close();
1406             }
1407         } finally {
1408             istream.close();
1409         }
1410         return bi;
1411     }
1412 
1413     /**
1414      * Returns a <code>BufferedImage</code> as the result of decoding
1415      * a supplied <code>ImageInputStream</code> with an
1416      * <code>ImageReader</code> chosen automatically from among those
1417      * currently registered.  If no registered
1418      * <code>ImageReader</code> claims to be able to read the stream,
1419      * <code>null</code> is returned.
1420      *


1518         if (output == null) {
1519             throw new IllegalArgumentException("output == null!");
1520         }
1521         ImageOutputStream stream = null;
1522 
1523         ImageWriter writer = getWriter(im, formatName);
1524         if (writer == null) {
1525             /* Do not make changes in the file system if we have
1526              * no appropriate writer.
1527              */
1528             return false;
1529         }
1530 
1531         try {
1532             output.delete();
1533             stream = createImageOutputStream(output);
1534         } catch (IOException e) {
1535             throw new IIOException("Can't create output stream!", e);
1536         }
1537 




1538         try {
1539             return doWrite(im, writer, stream);
1540         } finally {
1541             stream.close();
1542         }
1543     }
1544 
1545     /**
1546      * Writes an image using an arbitrary <code>ImageWriter</code>
1547      * that supports the given format to an <code>OutputStream</code>.
1548      *
1549      * <p> This method <em>does not</em> close the provided
1550      * <code>OutputStream</code> after the write operation has completed;
1551      * it is the responsibility of the caller to close the stream, if desired.
1552      *
1553      * <p> The current cache settings from <code>getUseCache</code>and
1554      * <code>getCacheDirectory</code> will be used to control caching.
1555      *
1556      * @param im a <code>RenderedImage</code> to be written.
1557      * @param formatName a <code>String</code> containing the informal
1558      * name of the format.
1559      * @param output an <code>OutputStream</code> to be written to.
1560      *
1561      * @return <code>false</code> if no appropriate writer is found.
1562      *
1563      * @exception IllegalArgumentException if any parameter is
1564      * <code>null</code>.
1565      * @exception IOException if an error occurs during writing.
1566      */
1567     public static boolean write(RenderedImage im,
1568                                 String formatName,
1569                                 OutputStream output) throws IOException {
1570         if (output == null) {
1571             throw new IllegalArgumentException("output == null!");
1572         }
1573         ImageOutputStream stream = null;
1574         try {
1575             stream = createImageOutputStream(output);
1576         } catch (IOException e) {
1577             throw new IIOException("Can't create output stream!", e);




1578         }
1579 
1580         try {
1581             return doWrite(im, getWriter(im, formatName), stream);
1582         } finally {
1583             stream.close();
1584         }
1585     }
1586 
1587     /**
1588      * Returns <code>ImageWriter</code> instance according to given
1589      * rendered image and image format or <code>null</code> if there
1590      * is no appropriate writer.
1591      */
1592     private static ImageWriter getWriter(RenderedImage im,
1593                                          String formatName) {
1594         ImageTypeSpecifier type =
1595             ImageTypeSpecifier.createFromRenderedImage(im);
1596         Iterator<ImageWriter> iter = getImageWriters(type, formatName);
1597 




1335      *
1336      * <p> This method <em>does not</em> close the provided
1337      * <code>InputStream</code> after the read operation has completed;
1338      * it is the responsibility of the caller to close the stream, if desired.
1339      *
1340      * @param input an <code>InputStream</code> to read from.
1341      *
1342      * @return a <code>BufferedImage</code> containing the decoded
1343      * contents of the input, or <code>null</code>.
1344      *
1345      * @exception IllegalArgumentException if <code>input</code> is
1346      * <code>null</code>.
1347      * @exception IOException if an error occurs during reading.
1348      */
1349     public static BufferedImage read(InputStream input) throws IOException {
1350         if (input == null) {
1351             throw new IllegalArgumentException("input == null!");
1352         }
1353 
1354         ImageInputStream stream = createImageInputStream(input);
1355 
1356         if (stream == null) {
1357             throw new IIOException("Can't create an ImageInputStream!");
1358         }
1359 
1360         BufferedImage bi = read(stream);
1361         if (bi == null) {
1362             stream.close();
1363         }
1364         return bi;
1365     }
1366 
1367     /**
1368      * Returns a <code>BufferedImage</code> as the result of decoding
1369      * a supplied <code>URL</code> with an <code>ImageReader</code>
1370      * chosen automatically from among those currently registered.  An
1371      * <code>InputStream</code> is obtained from the <code>URL</code>,
1372      * which is wrapped in an <code>ImageInputStream</code>.  If no
1373      * registered <code>ImageReader</code> claims to be able to read
1374      * the resulting stream, <code>null</code> is returned.
1375      *
1376      * <p> The current cache settings from <code>getUseCache</code>and
1377      * <code>getCacheDirectory</code> will be used to control caching in the
1378      * <code>ImageInputStream</code> that is created.
1379      *


1386      *
1387      * @return a <code>BufferedImage</code> containing the decoded
1388      * contents of the input, or <code>null</code>.
1389      *
1390      * @exception IllegalArgumentException if <code>input</code> is
1391      * <code>null</code>.
1392      * @exception IOException if an error occurs during reading.
1393      */
1394     public static BufferedImage read(URL input) throws IOException {
1395         if (input == null) {
1396             throw new IllegalArgumentException("input == null!");
1397         }
1398 
1399         InputStream istream = null;
1400         try {
1401             istream = input.openStream();
1402         } catch (IOException e) {
1403             throw new IIOException("Can't get input stream from URL!", e);
1404         }
1405         ImageInputStream stream = createImageInputStream(istream);
1406 
1407         if (stream == null) {
1408             throw new IIOException("Can't create an ImageInputStream!");
1409         }
1410 
1411         BufferedImage bi;
1412         try {
1413             bi = read(stream);
1414             if (bi == null) {
1415                 stream.close();
1416             }
1417         } finally {
1418             istream.close();
1419         }
1420         return bi;
1421     }
1422 
1423     /**
1424      * Returns a <code>BufferedImage</code> as the result of decoding
1425      * a supplied <code>ImageInputStream</code> with an
1426      * <code>ImageReader</code> chosen automatically from among those
1427      * currently registered.  If no registered
1428      * <code>ImageReader</code> claims to be able to read the stream,
1429      * <code>null</code> is returned.
1430      *


1528         if (output == null) {
1529             throw new IllegalArgumentException("output == null!");
1530         }
1531         ImageOutputStream stream = null;
1532 
1533         ImageWriter writer = getWriter(im, formatName);
1534         if (writer == null) {
1535             /* Do not make changes in the file system if we have
1536              * no appropriate writer.
1537              */
1538             return false;
1539         }
1540 
1541         try {
1542             output.delete();
1543             stream = createImageOutputStream(output);
1544         } catch (IOException e) {
1545             throw new IIOException("Can't create output stream!", e);
1546         }
1547 
1548         if (stream == null) {
1549             throw new IIOException("Can't create an ImageOutputStream!");
1550         }
1551 
1552         try {
1553             return doWrite(im, writer, stream);
1554         } finally {
1555             stream.close();
1556         }
1557     }
1558 
1559     /**
1560      * Writes an image using an arbitrary <code>ImageWriter</code>
1561      * that supports the given format to an <code>OutputStream</code>.
1562      *
1563      * <p> This method <em>does not</em> close the provided
1564      * <code>OutputStream</code> after the write operation has completed;
1565      * it is the responsibility of the caller to close the stream, if desired.
1566      *
1567      * <p> The current cache settings from <code>getUseCache</code>and
1568      * <code>getCacheDirectory</code> will be used to control caching.
1569      *
1570      * @param im a <code>RenderedImage</code> to be written.
1571      * @param formatName a <code>String</code> containing the informal
1572      * name of the format.
1573      * @param output an <code>OutputStream</code> to be written to.
1574      *
1575      * @return <code>false</code> if no appropriate writer is found.
1576      *
1577      * @exception IllegalArgumentException if any parameter is
1578      * <code>null</code>.
1579      * @exception IOException if an error occurs during writing.
1580      */
1581     public static boolean write(RenderedImage im,
1582                                 String formatName,
1583                                 OutputStream output) throws IOException {
1584         if (output == null) {
1585             throw new IllegalArgumentException("output == null!");
1586         }
1587         ImageOutputStream stream = null;
1588         try {
1589             stream = createImageOutputStream(output);
1590         } catch (IOException e) {
1591             throw new IIOException("Can't create output stream!", e);
1592         }
1593 
1594         if (stream == null) {
1595             throw new IIOException("Can't create an ImageOutputStream!");
1596         }
1597 
1598         try {
1599             return doWrite(im, getWriter(im, formatName), stream);
1600         } finally {
1601             stream.close();
1602         }
1603     }
1604 
1605     /**
1606      * Returns <code>ImageWriter</code> instance according to given
1607      * rendered image and image format or <code>null</code> if there
1608      * is no appropriate writer.
1609      */
1610     private static ImageWriter getWriter(RenderedImage im,
1611                                          String formatName) {
1612         ImageTypeSpecifier type =
1613             ImageTypeSpecifier.createFromRenderedImage(im);
1614         Iterator<ImageWriter> iter = getImageWriters(type, formatName);
1615 


< prev index next >