* @param im a <code>RenderedImage</code> to be written. * @param formatName a <code>String</code> containg the informal * name of the format. * @param output a <code>File</code> to be written to. * * @return <code>false</code> if no appropriate writer is found. * * @exception IllegalArgumentException if any parameter is * <code>null</code>. * @exception IOException if an error occurs during writing. */ public static boolean write(RenderedImage im, String formatName, File output) throws IOException { if (output == null) { throw new IllegalArgumentException("output == null!"); } ImageOutputStream stream = null; ImageWriter writer = getWriter(im, formatName); if (writer == null) { /* Do not make changes in the file system if we have * no appropriate writer. */ return false; } try { output.delete(); stream = createImageOutputStream(output); } catch (IOException e) { throw new IIOException("Can't create output stream!", e); } try { return doWrite(im, writer, stream); } finally { stream.close(); } } /** * Writes an image using an arbitrary <code>ImageWriter</code> * that supports the given format to an <code>OutputStream</code>. * * <p> This method <em>does not</em> close the provided * <code>OutputStream</code> after the write operation has completed; * it is the responsibility of the caller to close the stream, if desired. * * <p> The current cache settings from <code>getUseCache</code>and * <code>getCacheDirectory</code> will be used to control caching. * * @param im a <code>RenderedImage</code> to be written. * @param formatName a <code>String</code> containg the informal * name of the format. * @param output an <code>OutputStream</code> to be written to. * * @return <code>false</code> if no appropriate writer is found. * * @exception IllegalArgumentException if any parameter is * <code>null</code>. * @exception IOException if an error occurs during writing. */ public static boolean write(RenderedImage im, String formatName, OutputStream output) throws IOException { if (output == null) { throw new IllegalArgumentException("output == null!"); } ImageOutputStream stream = null; try { stream = createImageOutputStream(output); } catch (IOException e) { throw new IIOException("Can't create output stream!", e); } try { return doWrite(im, getWriter(im, formatName), stream); } finally { stream.close(); } } /** * Returns <code>ImageWriter</code> instance according to given * rendered image and image format or <code>null</code> if there * is no appropriate writer. */ private static ImageWriter getWriter(RenderedImage im, String formatName) { ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(im); Iterator<ImageWriter> iter = getImageWriters(type, formatName); if (iter.hasNext()) { return iter.next(); } else { return null; } } /** * Writes image to output stream using given image writer. */ private static boolean doWrite(RenderedImage im, ImageWriter writer, ImageOutputStream output) throws IOException { if (writer == null) { return false; } writer.setOutput(output); try { writer.write(im); } finally { writer.dispose(); output.flush(); } return true; }}Lines 1501–1619 of 1619. Previous lines
* @param im a <code>RenderedImage</code> to be written. * @param formatName a <code>String</code> containg the informal * name of the format. * @param output a <code>File</code> to be written to. * * @return <code>false</code> if no appropriate writer is found. * * @exception IllegalArgumentException if any parameter is * <code>null</code>. * @exception IOException if an error occurs during writing. */ public static boolean write(RenderedImage im, String formatName, File output) throws IOException { if (output == null) { throw new IllegalArgumentException("output == null!"); } ImageOutputStream stream = null; ImageWriter writer = getWriter(im, formatName); if (writer == null) { /* Do not make changes in the file system if we have * no appropriate writer. */ return false; } try { output.delete(); stream = createImageOutputStream(output); } catch (IOException e) { throw new IIOException("Can't create output stream!", e); } try { return doWrite(im, writer, stream); } finally { stream.close(); } } /** * Writes an image using an arbitrary <code>ImageWriter</code> * that supports the given format to an <code>OutputStream</code>. * * <p> This method <em>does not</em> close the provided * <code>OutputStream</code> after the write operation has completed; * it is the responsibility of the caller to close the stream, if desired. * * <p> The current cache settings from <code>getUseCache</code>and * <code>getCacheDirectory</code> will be used to control caching. * * @param im a <code>RenderedImage</code> to be written. * @param formatName a <code>String</code> containg the informal * name of the format. * @param output an <code>OutputStream</code> to be written to. * * @return <code>false</code> if no appropriate writer is found. * * @exception IllegalArgumentException if any parameter is * <code>null</code>. * @exception IOException if an error occurs during writing. */ public static boolean write(RenderedImage im, String formatName, OutputStream output) throws IOException { if (output == null) { throw new IllegalArgumentException("output == null!"); } ImageOutputStream stream = null; try { stream = createImageOutputStream(output); } catch (IOException e) { throw new IIOException("Can't create output stream!", e); } try { return doWrite(im, getWriter(im, formatName), stream); } finally { stream.close(); } } /** * Returns <code>ImageWriter</code> instance according to given * rendered image and image format or <code>null</code> if there * is no appropriate writer. */ private static ImageWriter getWriter(RenderedImage im, String formatName) { ImageTypeSpecifier type = ImageTypeSpecifier.createFromRenderedImage(im); Iterator<ImageWriter> iter = getImageWriters(type, formatName); if (iter.hasNext()) { return iter.next(); } else { return null; } } /** * Writes image to output stream using given image writer. */ private static boolean doWrite(RenderedImage im, ImageWriter writer, ImageOutputStream output) throws IOException { if (writer == null) { return false; } writer.setOutput(output); try { writer.write(im); } finally { writer.dispose(); output.flush(); } return true; } }