Package com.jalios.io

Class IOUtil

java.lang.Object
com.jalios.io.IOUtil

public class IOUtil extends Object
This class provides a set of static methods which perform various i/o operations.
Author:
Olivier Dedieu
  • Field Details

    • DOWNLOAD_ERROR

      public static int DOWNLOAD_ERROR
    • DOWNLOAD_OK

      public static int DOWNLOAD_OK
    • patternBlank1

      protected static org.apache.oro.text.regex.Pattern patternBlank1
    • patternBlank2

      protected static org.apache.oro.text.regex.Pattern patternBlank2
    • MIMETYPE_PLAINTEXT

      public static final String MIMETYPE_PLAINTEXT
      See Also:
  • Method Details

    • readList

      public static String[] readList(Reader reader) throws IOException
      Returns all the lines read on a given Reader. All the lines are trimmed. Blank line and line which starts with # are skipped.
      Parameters:
      reader - the reader
      Returns:
      an array of String.
      Throws:
      IOException - if an exception occurs
    • deepListFiles

      public static void deepListFiles(File file, Set<File> fileSet, Set<String> blackSet)
      Follow recursively a Path each File that are not Blacklisted (ends with a String of the BlackListSet) are added to the FileSet Compare (convert \\ to /) but store File with \ or / depends of platform
      Parameters:
      file - the directory to follow
      fileSet - a Set where to put all files
      blackSet - a Set of String to identify Files that ends with that String
    • deepListFiles

      public static List<File> deepListFiles(File dir, FileFilter filter)
      Return a list of files from the given directory and matching the given filter. If a filter is provided, the directories (and their content) that do not match the filter are skipped.
      Parameters:
      dir - the directory to explore
      filter - the FileFilter to use (may be null)
      Returns:
      a List of java.io.File
      Since:
      jcms-4.1
      See Also:
    • deepListFiles

      public static List<File> deepListFiles(File dir, FileFilter filter, boolean checkDir)
      Return a list of files from the given directory and matching the given filter.
      Parameters:
      dir - the directory to explore
      filter - the FileFilter to use (may be null)
      checkDir - if true the directories that do not match the filter are skipped.
      Returns:
      a List of java.io.File
      Since:
      jcms-5.6.1
    • substringFile

      public static String substringFile(File parentFile, File childFile)
      If Parent is parent of child then we return the substraction: childPath - parentPath. Else return full child path. WARNING: return / for unix and \ for windows
      Parameters:
      parentFile - the parent File
      childFile - the child File
      Returns:
      childPath - parentPath
    • writeArray

      public static void writeArray(Writer writer, String[] array) throws IOException
      Writes an array of String on a given Writer.
      Parameters:
      writer - the writer
      array - the array of String to write
      Throws:
      IOException - if an exception occurs
    • writeEnumeration

      public static void writeEnumeration(Writer writer, Enumeration<?> enu) throws IOException
      Writes an enumeration of object on a given Writer. Uses the toString() method.
      Parameters:
      writer - the writer
      enu - the enumeration
      Throws:
      IOException - if an exception occurs
    • writeIterator

      public static void writeIterator(Writer writer, Iterator<?> it) throws IOException
      Writes an iteration of object on a given Writer. Uses the toString() method.
      Parameters:
      writer - the writer
      it - the iterator
      Throws:
      IOException - if an exception occurs
    • unzip

      public static void unzip(File file, File dir) throws IOException
      Unzip a file
      Parameters:
      file - the file to unzip
      dir - the target directory
      Throws:
      IOException - if an exception occurs
      See Also:
    • unzip

      public static void unzip(File file, File dir, FileFilter filter) throws IOException
      Unzip a file
      Parameters:
      file - the file to unzip
      dir - the target directory
      filter - an optional FileFilter to filter the file to be extracted.
      Throws:
      IOException - if an exception occurs
      See Also:
    • unzip

      public static void unzip(String filename, String tgtDirname) throws IOException
      Unzip a file
      Parameters:
      filename - the name of the file to unzip
      tgtDirname - the name of the target directory
      Throws:
      IOException - if an exception occurs
      See Also:
    • unzip

      public static void unzip(String filename, String tgtDirname, FileFilter filter) throws IOException
      Unzip a file Adapted from: Working with Java Archive APIs Lawrence Rodrigues Java Report On-line http://archive.javareport.com/9808/html/features/archive/9806/rodrigues.shtml
      Parameters:
      filename - the name of the file to unzip
      tgtDirname - the name of the target directory
      filter - an optional FileFilter to filter the file to be extracted.
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-5.7.0
    • unzip

      public static void unzip(File file, File dir, FileFilter filter, Charset charset) throws IOException
      Unzip a file
      Parameters:
      file - the file to unzip
      dir - the target directory
      filter - an optional FileFilter to filter the file to be extracted.
      charset - The charset to be used to decode the ZIP entry name (ignored if the language encoding bit of the ZIP entry's general purpose bit flag is set).
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-9.0.4
      See Also:
    • unzip

      public static void unzip(String filename, String tgtDirname, FileFilter filter, Charset charset) throws IOException
      Unzip a file Adapted from: Working with Java Archive APIs Lawrence Rodrigues Java Report On-line http://archive.javareport.com/9808/html/features/archive/9806/rodrigues.shtml
      Parameters:
      filename - the name of the file to unzip
      tgtDirname - the name of the target directory
      filter - an optional FileFilter to filter the file to be extracted.
      charset - The charset to be used to decode the ZIP entry name (ignored if the language encoding bit of the ZIP entry's general purpose bit flag is set). Since jcms-10.0.3 / JCMS-7402, the Cp437 charset is used if none is specified (it is the most commonly used and compatible charset for zip file).
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-9.0.4
    • zip

      public static String zip(String filename, boolean isParentDirRemoved) throws IOException
      Zip a file or a whole directory. It produces a zip file which has the same name + ".zip"
      Parameters:
      filename - the name of file or directory to zip
      isParentDirRemoved - true if the parent path prefix of the filename must be removed in the zip file (eg: foo/* instead of a/b/c/foo/* when zipping directory "a/b/c/foo/")
      Returns:
      String the absolute path of the zip file
      Throws:
      IOException - if an exception occurs
    • zip

      public static void zip(String filename, String zipFile, boolean isParentDirRemoved) throws IOException
      Zip a file or a whole directory
      Parameters:
      filename - the name of file or directory to zip
      zipFile - the name of the zip file to produce
      isParentDirRemoved - true if the parent path prefix of the filename must be removed in the zip file (eg: foo/* instead of a/b/c/foo/* when zipping directory "a/b/c/foo/")
      Throws:
      IOException - if an exception occurs
    • zip

      public static void zip(File file, String zipFile, int prefixLength, FileFilter filter) throws IOException
      Throws:
      IOException
    • zip

      public static void zip(File[] files, String destFile, int[] prefixLength, FileFilter[] filters) throws IOException
      Zip an array of files or directories into a file. Files are filtered by an array of FileFilters and are named according to an array of prefix sizes.
      Parameters:
      files - the array of files or directories to be zipped
      destFile - the path of the destination zip file
      prefixLength - an array of prefix size to be used for each element of the files array.
      filters - array of FileFilters to be used for filtering files.
      Throws:
      IOException - if an exception occurs
    • zipWriter

      public static void zipWriter(File file, ZipOutputStream zos, int prefixLength, FileFilter filter) throws IOException
      Zip all files of a directory that are accepted by the given FileFilter.
      Parameters:
      file - the file or directory to be zipped
      zos - the ZipOutputStream into which the file has to be added
      prefixLength - the size of the prefix
      filter - the filter to be used
      Throws:
      IOException - if an exception occurs
    • getZipFileStream

      public static InputStream getZipFileStream(String fileName, File zipFile)
      Returns an InputStream on the File representing by the given fileName in the given zip file. Caller must close the Stream.
      Parameters:
      fileName - the file name of the file to seek
      zipFile - File reprenst the zip
      Returns:
      InputStream an InputStream to the given file in the zip
    • getZipFileStream

      public static InputStream getZipFileStream(String fileName, File zipFile, boolean regexp)
    • deflateAndBase64EncodeString

      public static String deflateAndBase64EncodeString(String str) throws IOException
      Compresses (deflates) a string and Base64 encodes the result and returns it as a string.
      Parameters:
      str - The string to compress and encode.
      Returns:
      Compressed and Base64 encoded result.
      Throws:
      IOException - Will probably not happen, maybe if there is a bug in the code or we run out of memory.
    • base64DecodeAndInflateString

      public static String base64DecodeAndInflateString(String str) throws DataFormatException, UnsupportedEncodingException
      Inflates Base64 encoded string. Basically the reverse of deflateAndBase64EncodeFileContents, except that it does not write to a file.
      Parameters:
      str - String should be compressed and Base64 encoded data.
      Returns:
      The original string (decoded and inflated(uncompressed)).
      Throws:
      DataFormatException - on bad input.
      UnsupportedEncodingException - if an exception occurs
    • deepDelete

      public static void deepDelete(File file)
      Deletes a whole directory. Use it with care !
      Parameters:
      file - the directory to destroy
    • deepDelete

      public static void deepDelete(File file, ActionListener listener)
      Deletes a whole directory. Use it with care !
      Parameters:
      file - the directory to destroy
      listener - a custom ActionListener used to perform custom actino during recursive calls
      Since:
      jcms5.7.3
    • deepDeleteEmptyDirectories

      public static void deepDeleteEmptyDirectories(File dir)
      Delete recursively all enpty directories under a given directory. THe given directory will also be deleted.
      Parameters:
      dir - the directory to destroy
    • deepDeleteEmptyDirectories

      public static void deepDeleteEmptyDirectories(File dir, FileFilter filter)
      Delete recursively all enpty directories under a given directory. THe given directory will also be deleted.
      Parameters:
      dir - the directory to destroy
      filter - files to ignore and remove
    • deepDeleteEmptyDirectories

      public static void deepDeleteEmptyDirectories(File dir, FileFilter filter, ActionListener listener)
      Delete recursively all enpty directories under a given directory. The given directory will also be deleted.
      Parameters:
      dir - the directory to destroy
      filter - FileFilter used to find files to be deleted before checking whether the directory is empty
      listener - to perform action during recursive calls
    • translateFile

      public static File translateFile(File file, File srcDir, File tgtDir)
      Returns the translated path of a given file which belongs to a given source directory into a target directory. Example: translateFile(/a/b/c.txt, /a/, /x/y/) returns /x/y/b/c.txt
      Parameters:
      file - the file to translate
      srcDir - the source directory
      tgtDir - the target directory
      Returns:
      the translated file
      Since:
      jcms-4.0.2
    • searchAndReplace

      public static void searchAndReplace(File file, String[] search, String[] replace)
      Execute an array of regular expression on a given file content. In this implementation search and replace arrays must fullfill String.replaceAll() requirement.
      Parameters:
      file - the file to parse
      search - an array of regular expression to match
      replace - ann array of regular expression to replace
    • searchAndReplace

      public static void searchAndReplace(File file, Map<String,String> map)
      Execute a map of regular expression on a given file content. In this implementation search and replace arrays must fullfill String.replaceAll() requirement.
      Parameters:
      file - the file to parse
      map - a map with regexp as key and substitute string as value.
    • deepCopy

      public static void deepCopy(File srcDir, File tgtDir, boolean deleteEmptyFile, FileFilter filter) throws IOException
      Perform a directory deep copy. If deleteEmptyFile is true, all the empty source files (length = 0) will be used as marker to delete the corresponding target files.
      Parameters:
      srcDir - the source directory to copy
      tgtDir - the target directory
      deleteEmptyFile - if true delete empty files.
      filter - the filter to be used (may be null)
      Throws:
      IOException - if it append for one file.
      Since:
      jcms-4.1.1
    • forceDeepCopy

      public static void forceDeepCopy(File srcDir, File tgtDir, boolean deleteEmptyFile, FileFilter filter)
      Perform a directory deep copy. If deleteEmptyFile is true, all the empty source files (length = 0) will be used as marker to delete the corresponding target files. The operation is performed for every file, even if an IOException has been thrown previously for a file.
      Parameters:
      srcDir - the source directory to copy
      tgtDir - the target directory
      deleteEmptyFile - if true delete empty files.
      filter - the filter to be used (may be null)
      Since:
      jcms-5.7.4
    • deepCopy

      public static void deepCopy(File srcDir, File tgtDir, boolean deleteEmptyFile) throws IOException
      Perform a directory deep copy. If deleteEmptyFile is true, all the empty source files (length = 0) will be used as marker to delete the corresponding target files.
      Parameters:
      srcDir - the source directory to copy
      tgtDir - the target directory
      deleteEmptyFile - if true delete empty files.
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-4.0.2
    • deepCopy

      public static void deepCopy(File srcDir, File tgtDir) throws IOException
      Perform a directory deep copy.
      Parameters:
      srcDir - the source directory to copy
      tgtDir - the target directory
      Throws:
      IOException - if an exception occurs
    • isParent

      public static boolean isParent(File parent, File child)
      Check if a File is a child of an other File recursively.
      Parameters:
      parent - the parent file
      child - the child file
      Returns:
      boolean
    • copyFile

      public static void copyFile(String filename1, String filename2) throws IOException
      Copy filename1 into filename2 (mkdirs for filename2)
      Parameters:
      filename1 - the source file
      filename2 - the target file
      Throws:
      IOException - if an exception occurs
    • copyFile

      public static void copyFile(File f1, File f2) throws IOException
      Copy f1 into f2 (mkdirs for f2)
      Parameters:
      f1 - the source file
      f2 - the target file
      Throws:
      IOException - if an exception occurs
    • moveFile

      public static boolean moveFile(File f1, File f2)
      Move f1 into f2 (mkdirs for f2). Try with renameTo. If it fails (may occurs on Windows on just created temp file), calls copyFile() and delete().
      Parameters:
      f1 - the source file
      f2 - the target file
      Returns:
      true if and only if the moving succeeded; false otherwise.
      Since:
      jcms-4.0.2
    • emptyInputStream

      public static byte[] emptyInputStream(DataInputStream dis)
      Emptys an InputStream
      Parameters:
      dis - the DataInputStream to empty
      Returns:
      the array of byte read.
    • removeBlankLines

      public static void removeBlankLines(String filename, boolean trim) throws IOException
      Remove all blank lines of a file. Uses UTF-8 encoding for reading and writing the file.
      Parameters:
      filename - the name of the file from which to remove blank lines.
      trim - If false, remove only blank lines without space (i.e. matching /^$/) otherwise remove blank lines and space lines (i.e. matching /^\s*$/).
      Throws:
      IOException - if an exception occurs
    • getTempDir

      public static File getTempDir()
      Returns:
      the path to the tmp directory
      Since:
      jcms-10.0.8
    • createTempDir

      public static File createTempDir()
      Creates a new empty directory
      Returns:
      An abstract pathname denoting a newly-created empty directory
    • createTempDir

      public static File createTempDir(String name, String prefix, File parentDir, boolean clear)
      Creates a new empty directory
      Parameters:
      name - The name of the directory to create. If null, generate a name based on prefix and current time.
      prefix - The prefix string to be used in generating the file's name
      parentDir - The parent directory in which the directory is to be created, or null if the default temporary-file directory is to be used
      clear - if true and if this directory already exists the content is deep deleted
      Returns:
      An abstract pathname denoting a newly-created empty directory
      Since:
      jcms-4.0.2
    • getTempDir

      public static File getTempDir(String name, String prefix, File parentDir)
      Returns An abstract pathname denoting an empty directory (this directory is not created).
      Parameters:
      name - The name of the directory to create. If null, generate a name.
      prefix - The prefix string to be used in generating the file's name
      parentDir - The parent directory in which the directory is to be created, or null if the default temporary-file directory is to be used
      Returns:
      An abstract pathname denoting an empty directory
      Since:
      jcms-4.0.2
    • canWrite

      public static boolean canWrite(File file, boolean checkParent)
      Tests whether the application can modify to the file denoted by the given file. If file does not exists and checkParent is true, the test is extended to the parent (recursively).
      Parameters:
      file - the file to test
      checkParent - true if the parent must checked
      Returns:
      true if this file can be write
      Since:
      jcms-4.0.2
    • getRelativePath

      public static String getRelativePath(File dir, File file)
      Return the relative path of the specified file in the specified directory.

      The returned path is platform independent :

      • "/" is used as file separator.
      • characters are encoded or escaped according to specification indicated in java class URI.
      Returns null if the specified file is not inside the given directory or any of its subdirectory.

      Examples :

       
         IOUtil.getRelativePath(new File("/foo/"), new File("/foo/bar/out.txt"));
         => "bar/out.txt"
         
         IOUtil.getRelativePath(new File("c:\\foo"), new File("c:\\foo\\bar\\out.txt"));
         => "bar/out.txt"
         
         IOUtil.getRelativePath(new File("/dir1/"), new File("/dir2/file.txt"));
         => null
         
         IOUtil.getRelativePath(new File("/foo/"), new File("/foo/bar baz/out.txt"));
         => "bar%20baz/out.txt"
       
       
      Parameters:
      dir - the directory
      file - the file
      Returns:
      the relative path or null if no relative path can be computed.
      Since:
      jcms-4.1
    • belongsToDirectory

      public static boolean belongsToDirectory(File dir, File untrustedFile)
      Check that the specified file belongs to the specified directory or any of its subdirectory.

      Exemple :

        File dir = new File("/some/directory/");
        File fileOk = new File("/some/directory/subdirectory/ok.txt");
        File fileKo = new File("/etc/passwd");
        assertTrue(IOUtil.belongsToDirectory(dir, fileOk));
        assertFalse(IOUtil.belongsToDirectory(dir, fileKo));
       
      Parameters:
      dir - the directory
      untrustedFile - the file to check
      Returns:
      true if the file is part of the directory, false otherwise
      Throws:
      IllegalArgumentException - if any of the parameter is null
      Since:
      jcms-7.1
    • goToLine

      public static boolean goToLine(InputStream in, long line) throws IOException
      Move the position of the given InputStream to the given line.
      Parameters:
      in - the InputStream to move the position.
      line - the line to be reached
      Returns:
      true if the line has been reached, false otherwite (line out of file size)
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-5.0.1
    • goToLine

      public static boolean goToLine(RandomAccessFile raf, long line) throws IOException
      Move the position of the given InputStream to the given line.
      Parameters:
      raf - the RandomAccessFile to move the position.
      line - the line to be reached
      Returns:
      true if the line has been reached, false otherwite (line out of file size)
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-5.0.1
    • getExtension

      public static String getExtension(File file)
      Returns the extension of the given file (without the leading "." char).

      Extension is always returned in lowercase (since JCMS-2732 / jcms-7.0.3).
      Examples: foo/bar.XML returns xml.

      Extension is accepted only if it contains exclusivel alpha numeric characters "a-zA-Z0-9" (since JCMS-8976 / jcms-10.0.6).
      Examples: foo/bar. zig or foo/bar. éxt returns empty string.
      The regular expression defining the accepted extension can be override using system property com.jalios.io.IOUtil.EXTENSION_AUTHORIZED_PATTERN

      Parameters:
      file - the file to get the extension
      Returns:
      the extension of the given file (in lowercase) or the empty string if no valid extension could be identified
      Since:
      jcms-5.5.0
    • getExtension

      public static String getExtension(String filename)
      Returns the extension of the given file (without the leading "." char).

      Extension is always returned in lowercase (since JCMS-2732 / jcms-7.0.3).
      Example: foo/bar.XML returns xml.

      Extension is accepted only if it contains exclusivel alpha numeric characters "a-zA-Z0-9" (since JCMS-8976 / jcms-10.0.6).
      Examples: foo/bar. zig or foo/bar. éxt returns empty string.
      The regular expression defining the accepted extension can be override using system property com.jalios.io.IOUtil.EXTENSION_AUTHORIZED_PATTERN

      Parameters:
      filename - the filename to get the extension
      Returns:
      the extension of the given filename
      Since:
      jcms-5.5.0
      See Also:
    • getQualifiedName

      public static String getQualifiedName(File file)
      Returns the name of a given file, without its extension.

      Examples :

      • FileName.Ext returns FileName
      • File.Name.ext returns File.Name
      • FileName returns FileName
      • path/to/FileName returns FileName

      Dot-files are returned entirely (since JCMS-2732 / jcms-7.0.3).
      Example: .bashrc returns .bashrc.

      Files with invalid extension (see getExtension(File)) are returned entirely (since JCMS-8976 / jcms-10.0.6)
      Examples: foo/bar. zig returns bar. zig, foo/bar. éxt returns bar. éxt

      Parameters:
      file - the File of which to retrieve qualified name
      Returns:
      String the qualified name of the given File
      Since:
      jcms-5.5.0
    • getQualifiedName

      public static String getQualifiedName(String filename)
      Returns the name of a given file, without its extension.

      Examples :

      • FileName.Ext returns FileName
      • File.Name.ext returns File.Name
      • FileName returns FileName
      • path/to/FileName returns FileName

      Dot-files are returned entirely (since JCMS-2732 / jcms-7.0.3).
      Example: .bashrc returns .bashrc.

      Files with invalid extension (see getExtension(File)) are returned entirely (since JCMS-8976 / jcms-10.0.6)
      Examples: foo/bar. zig returns bar. zig, foo/bar. éxt returns bar. éxt

      Parameters:
      filename - the file name
      Returns:
      String the qualified name of the given File
      Since:
      jcms-5.5.0
      See Also:
    • convertCharset

      public static void convertCharset(File file, String fromCS, String toCS) throws IOException
      Encode the given file with a given charset.
      Parameters:
      file - the file to be encoded
      fromCS - the original charset of the file
      toCS - the new charset of the file
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-5.5.0
    • copy

      public static int copy(InputStream input, OutputStream output) throws IOException
      Copy bytes from an InputStream to an OutputStream.

      This method buffers the input internally, so there is no need to use a BufferedInputStream.

      Parameters:
      input - the InputStream to read from
      output - the OutputStream to write to
      Returns:
      the number of bytes copied
      Throws:
      NullPointerException - if the input or output is null
      IOException - if an I/O error occurs
      Since:
      jcms-5.5.0
    • copy

      public static void copy(InputStream input, Writer output, String encoding) throws IOException
      Copy bytes from an InputStream to chars on a Writer using the specified character encoding, or UTF-8 if null.

      This method buffers the input internally, so there is no need to use a BufferedInputStream.

      Character encoding names can be found at IANA.

      This method uses InputStreamReader.

      Parameters:
      input - the InputStream to read from
      output - the Writer to write to
      encoding - the encoding to use, null means platform default
      Throws:
      NullPointerException - if the input or output is null
      IOException - if an I/O error occurs
      Since:
      jcms-5.5.0
    • copy

      public static int copy(Reader input, Writer output) throws IOException
      Copy chars from a Reader to a Writer.

      This method buffers the input internally, so there is no need to use a BufferedReader.

      Parameters:
      input - the Reader to read from
      output - the Writer to write to
      Returns:
      the number of characters copied
      Throws:
      NullPointerException - if the input or output is null
      IOException - if an I/O error occurs
      Since:
      jcms-5.5.0
    • copy

      public static void copy(Reader input, OutputStream output, String encoding) throws IOException
      Copy chars from a Reader to bytes on an OutputStream using the specified character encoding, and calling flush.

      This method buffers the input internally, so there is no need to use a BufferedReader.

      Character encoding names can be found at IANA.

      Due to the implementation of OutputStreamWriter, this method performs a flush.

      This method uses OutputStreamWriter.

      Parameters:
      input - the Reader to read from
      output - the OutputStream to write to
      encoding - the encoding to use, null means platform default
      Throws:
      NullPointerException - if the input or output is null
      IOException - if an I/O error occurs
      Since:
      jcms-5.5.0
    • closeQuietly

      public static void closeQuietly(Reader reader)
      Close the given reader. This convenient method is typically used in a finally block.
      Parameters:
      reader - the Reader.
      Since:
      jcms-5.6.0
    • closeQuietly

      public static void closeQuietly(Writer writer)
      Close the given Writer. This convenient method is intended to be used in a finally block.
      Parameters:
      writer - the Writer.
      Since:
      jcms-5.6.0
    • closeQuietly

      public static void closeQuietly(InputStream inputStream)
      Close the given InputStream. This convenient method is intended to be used in a finally block.
      Parameters:
      inputStream - the InputStream.
      Since:
      jcms-5.6.0
    • closeQuietly

      public static void closeQuietly(OutputStream outputStream)
      Close the given OutputStream. This convenient method is intended to be used in a finally block.
      Parameters:
      outputStream - the OutputStream.
      Since:
      jcms-5.6.0
    • closeQuietly

      public static void closeQuietly(Closeable closeable)
      Close the given Closeable. This convenient method is intended to be used in a finally block.
      Parameters:
      closeable - the Closeable.
      Since:
      jcms-10.0.1
    • isSameContent

      public static boolean isSameContent(File f1, File f2)
      Returns true if the given files have the same content.
      Parameters:
      f1 - the first file to be compared
      f2 - the second file to be compared
      Returns:
      true if the two files have the same content.
      Since:
      jcms-5.7.0
    • getNewDirectory

      public static String getNewDirectory(File baseDir, String subDir, long maxFilesPerDir)
      Returns the relative path of directory to use for the new document to be created. The directories are created.
      Parameters:
      baseDir - the base directory (eg /usr/local/tomcat/webapps/jcms/)
      subDir - an optional sub directory, may be null (eg upload/docs/image/gif/)
      maxFilesPerDir - how many files are allowed per directory.
      Returns:
      String the directory path, relative to the webapp, with the trailing slash ("/"). e.g: upload/docs/image/gif/2006/11/
      Since:
      jcms-5.7.0
    • tail

      public static String[] tail(File file, int size, boolean skipBlank, String encoding) throws IOException
      Returns the last lines of a file.
      Parameters:
      file - the file
      size - how many lines to returns
      skipBlank - if true skip the blank lines
      encoding - the file encoding
      Returns:
      the last lines of a file.
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-5.7.2
    • getDownloadTicket

      public static String getDownloadTicket(File file)
      Get a download ticket for the given file
      Parameters:
      file - the file to get a ticket for
      Returns:
      a String containing the ticket
      Since:
      jcms-4.1
    • downloadFile

      public static int downloadFile(URL url, File file, long lastModified, String ticket)
      Download the given URL
      Parameters:
      url - the URL to download
      file - the target file
      lastModified - if > 0, the last lastModified date of the file will be set to this value
      ticket - the ticket to be used for the download
      Returns:
      the status code
      Since:
      jcms-4.1
    • openConnection

      public static HttpURLConnection openConnection(URL url, boolean doOutput, boolean doInput, String method) throws IOException
      Open a new HttpURLConnection for the specified url
      Parameters:
      url - the URL for which HttpURLConnection is opened
      doOutput - see URLConnection.setDoOutput(boolean)
      doInput - see URLConnection.setDoInput(boolean)
      method - see HttpURLConnection.setRequestMethod(String)
      Returns:
      a new instance of HttpURLConnection
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-4.1
    • getInputStream

      public static InputStream getInputStream(HttpURLConnection httpConn) throws IOException
      Retrieve the input stream of the specified HttpURLConnection, decoded appropriately if gzip was used.
      Parameters:
      httpConn - the HttpUrlConnection being used
      Returns:
      a input stream
      Throws:
      IOException - if an exception occurs
      Since:
      jcms-4.1
    • checkFileSizeAndMtime

      public static boolean checkFileSizeAndMtime(File file, long size, long mtime)
      Returns true if the given file exists and has the given size and the given mtime.
      Parameters:
      file - the file
      size - the excepted size
      mtime - the expected mtime
      Returns:
      true if the given file exists and has the given size and the given mtime.
      Since:
      jcms-5.7.2
    • getMimeTypeFromMagicNumber

      public static String getMimeTypeFromMagicNumber(File file)
      Gives the mime type of a file. Licence concern : This method is based upon Aperture MagicMimeTypeIdentifier. The code was modified at the margin to be compatible with JRE 1.4.2. Given the licence of Aperture : Open Software Licence (OSL), this modified code of Aperture classes are held at the disposal on demand at support@jalios.com Concerned classes : UtfUtil MagicNumber MagicMimeTypeIdentifier MagicString MagicNumber
      Parameters:
      file - the file to test
      Returns:
      the mime type of the file or null if not found
      Since:
      JCMS-5.7.3