Class FileImageSink

  • All Implemented Interfaces:
    ImageSink<java.io.File>

    public class FileImageSink
    extends java.lang.Object
    implements ImageSink<java.io.File>
    An ImageSink which 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 the FileImageSink object.

    • Constructor Summary

      Constructors 
      Constructor Description
      FileImageSink​(java.io.File destinationFile)
      Instantiates a FileImageSink with the file to which the thumbnail should be written to.
      FileImageSink​(java.io.File destinationFile, boolean allowOverwrite)
      Instantiates a FileImageSink with the file to which the thumbnail should be written to.
      FileImageSink​(java.lang.String destinationFilePath)
      Instantiates a FileImageSink with the file to which the thumbnail should be written to.
      FileImageSink​(java.lang.String destinationFilePath, boolean allowOverwrite)
      Instantiates a FileImageSink with 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.OutputStream createOutputStream​(java.io.File destinationFile)  
      private static java.lang.String getExtension​(java.io.File f)
      Returns the file extension of the given File.
      java.io.File getSink()
      Returns the destination file of the thumbnail image.
      private static boolean isMatchingFormat​(java.lang.String formatName, java.lang.String fileExtension)
      Determines whether an specified format name and file extension are for the same format.
      java.lang.String preferredOutputFormatName()
      Returns the output format to use from information provided for the output image.
      void setOutputFormatName​(java.lang.String format)
      Sets the output format of the resulting image.
      void setThumbnailParameter​(ThumbnailParameter param)
      Sets the ThumbnailParameter from which to retrieve parameters to use when storing the image.
      void write​(java.awt.image.BufferedImage img)
      Writes the resulting image to a file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • destinationFile

        private java.io.File destinationFile
        The file to which the thumbnail is written to.

        Under certain circumstances, the File object 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
    • Constructor Detail

      • FileImageSink

        public FileImageSink​(java.io.File destinationFile)
        Instantiates a FileImageSink with 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 FileImageSink will overwrite the existing file.

        Parameters:
        destinationFile - The destination file.
        Throws:
        java.lang.NullPointerException - If the specified file is null.
      • FileImageSink

        public FileImageSink​(java.io.File destinationFile,
                             boolean allowOverwrite)
        Instantiates a FileImageSink with 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 the FileImageSink should overwrite the destination file if it already exists.
        Throws:
        java.lang.NullPointerException - If the specified file is null.
      • FileImageSink

        public FileImageSink​(java.lang.String destinationFilePath)
        Instantiates a FileImageSink with 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 FileImageSink will overwrite the existing file.

        Parameters:
        destinationFilePath - The destination file path.
        Throws:
        java.lang.NullPointerException - If the specified file path is null.
      • FileImageSink

        public FileImageSink​(java.lang.String destinationFilePath,
                             boolean allowOverwrite)
        Instantiates a FileImageSink with 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 the FileImageSink should overwrite the destination file if it already exists.
        Throws:
        java.lang.NullPointerException - If the specified file path is null.
    • Method Detail

      • isMatchingFormat

        private static boolean isMatchingFormat​(java.lang.String formatName,
                                                java.lang.String fileExtension)
                                         throws UnsupportedFormatException
        Determines whether an specified format name and file extension are for the same format.
        Parameters:
        formatName - Format name.
        fileExtension - File extension.
        Returns:
        Returns true if 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 given File.
        Parameters:
        f - The file.
        Returns:
        The extension of the file.
      • preferredOutputFormatName

        public java.lang.String preferredOutputFormatName()
        Description copied from interface: ImageSink

        Returns the output format to use from information provided for the output image.

        If the output format cannot be determined, then ThumbnailParameter.ORIGINAL_FORMAT should be returned.

        Specified by:
        preferredOutputFormatName in interface ImageSink<java.io.File>
        Returns:
        Name of the preferred output format.
      • write

        public void write​(java.awt.image.BufferedImage img)
                   throws java.io.IOException
        Writes the resulting image to a file.
        Specified by:
        write in interface ImageSink<java.io.File>
        Parameters:
        img - The image to write.
        Throws:
        UnsupportedFormatException - When an unsupported format has been specified by the setOutputFormatName(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 is null.
        java.lang.IllegalArgumentException - If this FileImageSink does 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.)

        Specified by:
        getSink in interface ImageSink<java.io.File>
        Returns:
        the destinationFile
      • setOutputFormatName

        public void setOutputFormatName​(java.lang.String format)
        Description copied from interface: ImageSink
        Sets 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:
        setOutputFormatName in interface ImageSink<java.io.File>
        Parameters:
        format - File format with which to store the image.