Last active
January 5, 2022 17:18
-
-
Save LoneDev6/a5c3d24df0ef6a7bd65ddd415624197c to your computer and use it in GitHub Desktop.
ImageIOFix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.imageio.IIOException; | |
import javax.imageio.ImageIO; | |
import javax.imageio.ImageTypeSpecifier; | |
import javax.imageio.ImageWriter; | |
import javax.imageio.stream.ImageOutputStream; | |
import java.awt.image.RenderedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Iterator; | |
public class ImageIOFix | |
{ | |
/** | |
* This fixed version deletes the existing file only if the output | |
* stream was created without errors. | |
* | |
* Writes an image using an arbitrary {@code ImageWriter} | |
* that supports the given format to a {@code File}. If | |
* there is already a {@code File} present, its contents are | |
* discarded. | |
* | |
* @param im a {@code RenderedImage} to be written. | |
* @param formatName a {@code String} containing the informal | |
* name of the format. | |
* @param output a {@code File} to be written to. | |
* | |
* @return {@code false} if no appropriate writer is found. | |
* | |
* @exception IllegalArgumentException if any parameter is | |
* {@code null}. | |
* @exception IOException if an error occurs during writing or when not | |
* able to create required ImageOutputStream. | |
*/ | |
public static boolean write(RenderedImage im, | |
String formatName, | |
File output) throws IOException | |
{ | |
if (output == null) | |
{ | |
throw new IllegalArgumentException("output == 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; | |
} | |
ImageOutputStream stream = ImageIO.createImageOutputStream(output); | |
if (stream == null) | |
{ | |
throw new IIOException("Can't create an ImageOutputStream!"); | |
} | |
try | |
{ | |
return doWrite(im, writer, stream); | |
} | |
finally | |
{ | |
stream.close(); | |
} | |
} | |
/** | |
* Returns {@code ImageWriter} instance according to given | |
* rendered image and image format or {@code null} if there | |
* is no appropriate writer. | |
*/ | |
private static ImageWriter getWriter(RenderedImage im, | |
String formatName) | |
{ | |
ImageTypeSpecifier type = | |
ImageTypeSpecifier.createFromRenderedImage(im); | |
Iterator<ImageWriter> iter = ImageIO.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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment