Class FileUtils


  • public class FileUtils
    extends Object
    Author:
    XIMA MEDIA GmbH
    • Constructor Detail

      • FileUtils

        public FileUtils()
    • Method Detail

      • createEmptyStreamedContent

        public static org.primefaces.model.StreamedContent createEmptyStreamedContent()
        Generates StreamedContent with an empty InputStream, which is used in various Beans related to File Download or Upload, when somehow the sources are empty or equal null
        Returns:
        the empty StreamedContent
      • createZipFile

        public static <E extends IFileDataEntity<?>,​T extends IFileProviding<?,​E>> File createZipFile​(List<T> entities,
                                                                                                                  File outDir)
                                                                                                           throws IOException
        Packs the files of the given entities into one zip file which will be placed in the specified output directory
        Parameters:
        entities - IFileProviding entities that should be packed into one zip archive
        outDir - File output directory where the resulting zip file will be placed
        Returns:
        File in zip format containing all the files of the entities
        Throws:
        IOException - on errors during creating the zip file
      • createZipFile

        public static <E extends IFileDataEntity<?>,​T extends IFileProviding<?,​E>> File createZipFile​(List<T> entities,
                                                                                                                  File outDir,
                                                                                                                  String zipFileName)
                                                                                                           throws IOException
        Packs the files of the given entities into one zip file which will be placed in the specified output directory
        Parameters:
        entities - IFileProviding entities that should be packed into one zip archive
        outDir - File output directory where the resulting zip file will be placed
        zipFileName - String name of the resulting zip file
        Returns:
        File in zip format containing all the files of the entities
        Throws:
        IOException - on errors during creating the zip file
      • createZipFileFromDataSources

        public static <E extends IFileDataEntity<?>,​T extends IFileProviding<?,​E>> File createZipFileFromDataSources​(List<Datenquelle> sources,
                                                                                                                                 File outDir,
                                                                                                                                 String zipFileName)
                                                                                                                          throws IOException
        Packs the contents of the given data sources into one zip file which will be placed in the specified output directory
        Parameters:
        sources - IFileProviding data sources that should be packed into one zip archive
        outDir - File output directory where the resulting zip file will be placed
        zipFileName - String name of the resulting zip file
        Returns:
        File in zip format containing all the content files of the data sources
        Throws:
        IOException - on errors during creating the zip file
      • createZipFileFromFiles

        public static File createZipFileFromFiles​(List<File> files,
                                                  File outDir,
                                                  String zipFileName)
                                           throws IOException
        Packs the given files one zip file which will be placed in the specified output directory
        Parameters:
        files - List files that should be packed into one zip archive
        outDir - File output directory where the resulting zip file will be placed
        zipFileName - String name of the resulting zip file
        Returns:
        File in zip format containing all the files
        Throws:
        IOException - on errors during creating the zip file
      • generateFileName

        public static String generateFileName​(Datenquelle source)
        Generates a file name for the given data source
        Parameters:
        source - Datenquelle
        Returns:
        String file name of the given data source
      • getContentType

        public static String getContentType​(File file)
        Returns the content type of the given file
        Parameters:
        file - File
        Returns:
        String content type of the file
      • getContentType

        public static String getContentType​(String fileName)
        Returns the content type of the file by its filename
        Parameters:
        fileName - String of the file
        Returns:
        String content type of the file
      • getData

        public static byte[] getData​(org.primefaces.model.file.UploadedFile upload)
                              throws Exception,
                                     OutOfMemoryError
        Returns the data of the UploadedFile as a byte array
        Parameters:
        upload - UploadedFile holding the data
        Returns:
        byte[] of the data
        Throws:
        Exception - on errors during reading the UploadedFile
        OutOfMemoryError - if Java Virtual Machine is out of memory
      • getExtension

        public static String getExtension​(Datenquelle source)
        Returns the file extension for the given data source
        Parameters:
        source - Datenquelle
        Returns:
        String file extension
      • getFile

        public static File getFile​(Datenquelle source,
                                   File outDir)
                            throws IOException
        Generates a File for a given data source
        Parameters:
        source - Datenquelle for which a file should be generated
        outDir - File output directory where resulting file should be placed
        Returns:
        File of the given data source
        Throws:
        IOException - on errors during writing the file
      • getMostRecentFile

        public static Optional<Path> getMostRecentFile​(Iterable<Path> files)
        Given a set of files, finds the file or directory that was modified most recently. Files or directories that do not exist are omitted. Returns an empty optional if no paths were given or none of the files or directories exist.
        Parameters:
        files - Files to process.
        Returns:
        The file or directory that was modified most recently
      • getStreamedContent

        public static <F extends IFileEntity<?,​?>> org.primefaces.model.StreamedContent getStreamedContent​(F fileEntity)
        Gets a StreamedContent for the given IFileEntity file entity
        Type Parameters:
        F - Type of the file data entity.
        Parameters:
        fileEntity - for which to return a StreamedContent
        Returns:
        StreamedContent of the given IFileProviding entity or null if the entity is null
      • getStreamedContent

        public static org.primefaces.model.StreamedContent getStreamedContent​(List<Datenquelle> sources,
                                                                              String zipFileName)
                                                                       throws Exception
        Generates StreamedContent from the given List of data sources. If this list contains only a single entry the StreamedContent for this data source will be returned. If the list contains more than one entry the data sources will be packed into a zip file with the specified filename and the generated StreamedContent of the zip file will be returned.
        Parameters:
        sources - List of the data sources to generate StreamedContent for
        zipFileName - String name of the resulting zip file if more than one data source is contained within the given data source list
        Returns:
        the StreamedContent of all the marked model data sources
        Throws:
        Exception - on errors during creating the StreamedContent
      • getStreamedContent

        public static <E extends IFileDataEntity<?>,​T extends IFileProviding<?,​E>> org.primefaces.model.StreamedContent getStreamedContent​(T entity)
        Gets a StreamedContent for the given IFileProviding entity
        Type Parameters:
        E - Type of the file data entity.
        T - Type of the class that provides the file.
        Parameters:
        entity - for which to return a StreamedContent
        Returns:
        StreamedContent of the given IFileProviding entity or null if the entity is null
      • getStreamedContentEmpty

        public static org.primefaces.model.StreamedContent getStreamedContentEmpty​(String fileName,
                                                                                   String contentType,
                                                                                   Charset encoding)
        Parameters:
        fileName - File name for the download file.
        contentType - Content type for the downloaded file.
        encoding - Encoding of the file, should only be set for text files (mainly mime type text/*, otherwise pass null.
        Returns:
        A streamed content instance with no data.
      • getStreamedContentFile

        public static org.primefaces.model.StreamedContent getStreamedContentFile​(Path file,
                                                                                  String fileName,
                                                                                  String contentType,
                                                                                  Charset encoding)
        Creates a streamed content instance that returns the given file.
        Parameters:
        file - File to stream.
        fileName - Name of the file.
        contentType - Content type of the file.
        encoding - Encoding of the file, should only be set for text files (mainly mime type text/*, otherwise pass null.
        Returns:
        A streamed content for the given file.
      • getStreamedContentFileOrZip

        public static org.primefaces.model.StreamedContent getStreamedContentFileOrZip​(Iterable<Path> files,
                                                                                       String fileName,
                                                                                       String contentType,
                                                                                       Charset encoding)
        Creates a streamed content instance that either contains a single file directly, or a ZIP file when the given files contain more than one file or no file at all.
        Parameters:
        files - Files to stream.
        fileName - Name of the file.
        contentType - Content type of the file.
        encoding - Encoding of the file, should only be set for text files (mainly mime type text/*, otherwise pass null.
        Returns:
        A streamed content with either a single file or a ZIP file.
      • getStreamedContentZip

        public static org.primefaces.model.StreamedContent getStreamedContentZip​(Iterable<Path> files,
                                                                                 String fileName)
        Creates a streamed content instance that returns a ZIP file with all given files.
        Parameters:
        files - File to stream.
        fileName - Name of the file.
        Returns:
        A streamed content with a ZIP file contain all given files.
      • getStyleClass

        public static String getStyleClass​(String fileName)
      • isImageFile

        public static boolean isImageFile​(String fileName)
      • newDeleteFileOnCloseInputStream

        public static InputStream newDeleteFileOnCloseInputStream​(Path file)
                                                           throws IOException
        Creates an input stream for reading the given file that deletes the file once the stream is closed. If any methods of the input stream throw an IOException, the file is deleted immediately.
        Parameters:
        file - Path to the file.
        Returns:
        An input stream for reading the file.
        Throws:
        IOException - When the input stream could not be opened. The file is still deleted.
      • newStreamedContentBuilder

        public static org.primefaces.model.DefaultStreamedContent.Builder newStreamedContentBuilder​(String fileName,
                                                                                                    String contentType,
                                                                                                    Charset encoding)
        Creates a new streamed content builder with the given meta data set, but no binary data yet.
        Parameters:
        fileName - Name of the file.
        contentType - Content type of the file.
        encoding - Encoding of the file, should only be set for text files (mainly mime type text/*, otherwise pass null.
        Returns:
        A new builder with the given data.