Class FileImageSink
- java.lang.Object
-
- net.coobird.thumbnailator.tasks.io.FileImageSink
-
- All Implemented Interfaces:
ImageSink<java.io.File>
public class FileImageSink extends java.lang.Object implements ImageSink<java.io.File>
AnImageSinkwhich writes the resulting thumbnail to a file.Under certain circumstances, the destination file can change in the course of processing.
This can occur in cases where the file extension does not match the output format set by the
setOutputFormatName(String)method. In this case, the file name will have a file extension corresponding to the output format set in the above method to be appended to the file name originally provided when instantiating theFileImageSinkobject.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classFileImageSink.UninitializedImageSinkTemporary placeholderImageSinkwhich will be used before the#read()method is used.
-
Field Summary
Fields Modifier and Type Field Description private booleanallowOverwriteprivate java.io.FiledestinationFileThe file to which the thumbnail is written to.private ImageSink<?>imageSinkAnImageSinkwhich actually performs the image writing operations.private java.lang.StringoutputFormatprivate ThumbnailParameterparam
-
Constructor Summary
Constructors Constructor Description FileImageSink(java.io.File destinationFile)Instantiates aFileImageSinkwith the file to which the thumbnail should be written to.FileImageSink(java.io.File destinationFile, boolean allowOverwrite)Instantiates aFileImageSinkwith the file to which the thumbnail should be written to.FileImageSink(java.lang.String destinationFilePath)Instantiates aFileImageSinkwith the file to which the thumbnail should be written to.FileImageSink(java.lang.String destinationFilePath, boolean allowOverwrite)Instantiates aFileImageSinkwith the file to which the thumbnail should be written to.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) java.io.OutputStreamcreateOutputStream(java.io.File destinationFile)private static java.lang.StringgetExtension(java.io.File f)Returns the file extension of the givenFile.java.io.FilegetSink()Returns the destination file of the thumbnail image.private static booleanisMatchingFormat(java.lang.String formatName, java.lang.String fileExtension)Determines whether an specified format name and file extension are for the same format.java.lang.StringpreferredOutputFormatName()Returns the output format to use from information provided for the output image.voidsetOutputFormatName(java.lang.String format)Sets the output format of the resulting image.voidsetThumbnailParameter(ThumbnailParameter param)Sets theThumbnailParameterfrom which to retrieve parameters to use when storing the image.voidwrite(java.awt.image.BufferedImage img)Writes the resulting image to a file.
-
-
-
Field Detail
-
destinationFile
private java.io.File destinationFile
The file to which the thumbnail is written to.Under certain circumstances, the
Fileobject can be replaced in the course of processing. This can occur in cases where the file extension has been changed due to incongruence between the extension and the desired output format.
-
allowOverwrite
private final boolean allowOverwrite
-
outputFormat
private java.lang.String outputFormat
-
param
private ThumbnailParameter param
-
imageSink
private ImageSink<?> imageSink
AnImageSinkwhich actually performs the image writing operations. ThisImageSinkcan change during the lifecycle of theFileImageSinkclass.
-
-
Constructor Detail
-
FileImageSink
public FileImageSink(java.io.File destinationFile)
Instantiates aFileImageSinkwith the file to which the thumbnail should be written to.The output format to use will be determined from the file extension. If another format should be used, then the
setOutputFormatName(String)should be called with the desired output format name.When the destination file exists, then this
FileImageSinkwill overwrite the existing file.- Parameters:
destinationFile- The destination file.- Throws:
java.lang.NullPointerException- If the specified file isnull.
-
FileImageSink
public FileImageSink(java.io.File destinationFile, boolean allowOverwrite)Instantiates aFileImageSinkwith the file to which the thumbnail should be written to.The output format to use will be determined from the file extension. If another format should be used, then the
setOutputFormatName(String)should be called with the desired output format name.- Parameters:
destinationFile- The destination file.allowOverwrite- Whether or not theFileImageSinkshould overwrite the destination file if it already exists.- Throws:
java.lang.NullPointerException- If the specified file isnull.
-
FileImageSink
public FileImageSink(java.lang.String destinationFilePath)
Instantiates aFileImageSinkwith the file to which the thumbnail should be written to.The output format to use will be determined from the file extension. If another format should be used, then the
setOutputFormatName(String)should be called with the desired output format name.When the destination file exists, then this
FileImageSinkwill overwrite the existing file.- Parameters:
destinationFilePath- The destination file path.- Throws:
java.lang.NullPointerException- If the specified file path isnull.
-
FileImageSink
public FileImageSink(java.lang.String destinationFilePath, boolean allowOverwrite)Instantiates aFileImageSinkwith the file to which the thumbnail should be written to.The output format to use will be determined from the file extension. If another format should be used, then the
setOutputFormatName(String)should be called with the desired output format name.- Parameters:
destinationFilePath- The destination file path.allowOverwrite- Whether or not theFileImageSinkshould overwrite the destination file if it already exists.- Throws:
java.lang.NullPointerException- If the specified file path isnull.
-
-
Method Detail
-
isMatchingFormat
private static boolean isMatchingFormat(java.lang.String formatName, java.lang.String fileExtension) throws UnsupportedFormatExceptionDetermines whether an specified format name and file extension are for the same format.- Parameters:
formatName- Format name.fileExtension- File extension.- Returns:
- Returns
trueif the specified file extension is valid for the specified format. - Throws:
UnsupportedFormatException
-
getExtension
private static java.lang.String getExtension(java.io.File f)
Returns the file extension of the givenFile.- Parameters:
f- The file.- Returns:
- The extension of the file.
-
preferredOutputFormatName
public java.lang.String preferredOutputFormatName()
Description copied from interface:ImageSinkReturns the output format to use from information provided for the output image.
If the output format cannot be determined, then
ThumbnailParameter.ORIGINAL_FORMATshould be returned.- Specified by:
preferredOutputFormatNamein interfaceImageSink<java.io.File>- Returns:
- Name of the preferred output format.
-
write
public void write(java.awt.image.BufferedImage img) throws java.io.IOExceptionWrites the resulting image to a file.- Specified by:
writein interfaceImageSink<java.io.File>- Parameters:
img- The image to write.- Throws:
UnsupportedFormatException- When an unsupported format has been specified by thesetOutputFormatName(String)method, or if the output format has not been set and cannot be determined from the file name.java.io.IOException- When a problem occurs while writing the image.java.lang.NullPointerException- If the image isnull.java.lang.IllegalArgumentException- If thisFileImageSinkdoes not permit overwriting the destination file and the destination file already exists.
-
createOutputStream
java.io.OutputStream createOutputStream(java.io.File destinationFile) throws java.io.IOException- Throws:
java.io.IOException
-
getSink
public java.io.File getSink()
Returns the destination file of the thumbnail image.If the final destination of the thumbnail changes in the course of writing the thumbnail. (For example, if the file extension for the given destination did not match the destination file format, then the correct file extension could be appended.)
-
setOutputFormatName
public void setOutputFormatName(java.lang.String format)
Description copied from interface:ImageSinkSets the output format of the resulting image.For
ImageSinks which stores raw images, the format name specified by this method may be ignored.- Specified by:
setOutputFormatNamein interfaceImageSink<java.io.File>- Parameters:
format- File format with which to store the image.
-
setThumbnailParameter
public void setThumbnailParameter(ThumbnailParameter param)
Description copied from interface:ImageSinkSets theThumbnailParameterfrom which to retrieve parameters to use when storing the image.- Specified by:
setThumbnailParameterin interfaceImageSink<java.io.File>- Parameters:
param- TheThumbnailParameterwith image writing parameters.
-
-