1489 throw new IllegalArgumentException("formatName == null!"); 1490 } 1491 if (output == null) { 1492 throw new IllegalArgumentException("output == null!"); 1493 } 1494 1495 return doWrite(im, getWriter(im, formatName), output); 1496 } 1497 1498 /** 1499 * Writes an image using an arbitrary <code>ImageWriter</code> 1500 * that supports the given format to a <code>File</code>. If 1501 * there is already a <code>File</code> present, its contents are 1502 * discarded. 1503 * 1504 * @param im a <code>RenderedImage</code> to be written. 1505 * @param formatName a <code>String</code> containing the informal 1506 * name of the format. 1507 * @param output a <code>File</code> to be written to. 1508 * 1509 * @return <code>false</code> if no appropriate writer is found. 1510 * 1511 * @exception IllegalArgumentException if any parameter is 1512 * <code>null</code>. 1513 * @exception IOException if an error occurs during writing. 1514 */ 1515 public static boolean write(RenderedImage im, 1516 String formatName, 1517 File output) throws IOException { 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 1598 if (iter.hasNext()) { 1599 return iter.next(); 1600 } else { 1601 return null; 1602 } 1603 } 1604 1605 /** 1606 * Writes image to output stream using given image writer. 1607 */ 1608 private static boolean doWrite(RenderedImage im, ImageWriter writer, 1609 ImageOutputStream output) throws IOException { 1610 if (writer == null) { 1611 return false; 1612 } 1613 writer.setOutput(output); 1614 try { 1615 writer.write(im); 1616 } finally { 1617 writer.dispose(); 1618 output.flush(); 1619 } 1620 return true; 1621 } 1622 } | 1489 throw new IllegalArgumentException("formatName == null!"); 1490 } 1491 if (output == null) { 1492 throw new IllegalArgumentException("output == null!"); 1493 } 1494 1495 return doWrite(im, getWriter(im, formatName), output); 1496 } 1497 1498 /** 1499 * Writes an image using an arbitrary <code>ImageWriter</code> 1500 * that supports the given format to a <code>File</code>. If 1501 * there is already a <code>File</code> present, its contents are 1502 * discarded. 1503 * 1504 * @param im a <code>RenderedImage</code> to be written. 1505 * @param formatName a <code>String</code> containing the informal 1506 * name of the format. 1507 * @param output a <code>File</code> to be written to. 1508 * 1509 * @return <code>false</code> if no appropriate writer is found or 1510 * not able to create required ImageOutputStream. 1511 * 1512 * @exception IllegalArgumentException if any parameter is 1513 * <code>null</code>. 1514 * @exception IOException if an error occurs during writing. 1515 */ 1516 public static boolean write(RenderedImage im, 1517 String formatName, 1518 File output) throws IOException { 1519 if (output == null) { 1520 throw new IllegalArgumentException("output == null!"); 1521 } 1522 ImageOutputStream stream = null; 1523 1524 ImageWriter writer = getWriter(im, formatName); 1525 if (writer == null) { 1526 /* Do not make changes in the file system if we have 1527 * no appropriate writer. 1528 */ 1529 return false; 1530 } 1531 1532 try { 1533 output.delete(); 1534 stream = createImageOutputStream(output); 1535 } catch (IOException e) { 1536 throw new IIOException("Can't create output stream!", e); 1537 } 1538 1539 try { 1540 return doWrite(im, writer, stream); 1541 } finally { 1542 if (stream != null) 1543 stream.close(); 1544 } 1545 } 1546 1547 /** 1548 * Writes an image using an arbitrary <code>ImageWriter</code> 1549 * that supports the given format to an <code>OutputStream</code>. 1550 * 1551 * <p> This method <em>does not</em> close the provided 1552 * <code>OutputStream</code> after the write operation has completed; 1553 * it is the responsibility of the caller to close the stream, if desired. 1554 * 1555 * <p> The current cache settings from <code>getUseCache</code>and 1556 * <code>getCacheDirectory</code> will be used to control caching. 1557 * 1558 * @param im a <code>RenderedImage</code> to be written. 1559 * @param formatName a <code>String</code> containing the informal 1560 * name of the format. 1561 * @param output an <code>OutputStream</code> to be written to. 1562 * 1563 * @return <code>false</code> if no appropriate writer is found or 1564 * not able to create required ImageOutputStream. 1565 * 1566 * @exception IllegalArgumentException if any parameter is 1567 * <code>null</code>. 1568 * @exception IOException if an error occurs during writing. 1569 */ 1570 public static boolean write(RenderedImage im, 1571 String formatName, 1572 OutputStream output) throws IOException { 1573 if (output == null) { 1574 throw new IllegalArgumentException("output == null!"); 1575 } 1576 ImageOutputStream stream = null; 1577 try { 1578 stream = createImageOutputStream(output); 1579 } catch (IOException e) { 1580 throw new IIOException("Can't create output stream!", e); 1581 } 1582 1583 try { 1584 return doWrite(im, getWriter(im, formatName), stream); 1585 } finally { 1586 if (stream != null) 1587 stream.close(); 1588 } 1589 } 1590 1591 /** 1592 * Returns <code>ImageWriter</code> instance according to given 1593 * rendered image and image format or <code>null</code> if there 1594 * is no appropriate writer. 1595 */ 1596 private static ImageWriter getWriter(RenderedImage im, 1597 String formatName) { 1598 ImageTypeSpecifier type = 1599 ImageTypeSpecifier.createFromRenderedImage(im); 1600 Iterator<ImageWriter> iter = getImageWriters(type, formatName); 1601 1602 if (iter.hasNext()) { 1603 return iter.next(); 1604 } else { 1605 return null; 1606 } 1607 } 1608 1609 /** 1610 * Writes image to output stream using given image writer. 1611 */ 1612 private static boolean doWrite(RenderedImage im, ImageWriter writer, 1613 ImageOutputStream output) throws IOException { 1614 if (writer == null) { 1615 return false; 1616 } 1617 if (output == null) { 1618 return false; 1619 } 1620 writer.setOutput(output); 1621 try { 1622 writer.write(im); 1623 } finally { 1624 writer.dispose(); 1625 output.flush(); 1626 } 1627 return true; 1628 } 1629 } |