Class IOUtil
- Author:
- Olivier Dedieu
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic intstatic intstatic final Stringprotected static org.apache.oro.text.regex.Patternprotected static org.apache.oro.text.regex.Pattern -
Method Summary
Modifier and TypeMethodDescriptionstatic StringInflates Base64 encoded string.static booleanbelongsToDirectory(File dir, File untrustedFile) Check that the specified file belongs to the specified directory or any of its subdirectory.static booleanTests whether the application can modify to the file denoted by the given file.static booleancheckFileSizeAndMtime(File file, long size, long mtime) Returns true if the given file exists and has the given size and the given mtime.static voidcloseQuietly(Closeable closeable) Close the given Closeable.static voidcloseQuietly(InputStream inputStream) Close the given InputStream.static voidcloseQuietly(OutputStream outputStream) Close the given OutputStream.static voidcloseQuietly(Reader reader) Close the given reader.static voidcloseQuietly(Writer writer) Close the given Writer.static voidconvertCharset(File file, String fromCS, String toCS) Encode the given file with a given charset.static intcopy(InputStream input, OutputStream output) Copy bytes from anInputStreamto anOutputStream.static voidcopy(InputStream input, Writer output, String encoding) Copy bytes from anInputStreamto chars on aWriterusing the specified character encoding, or UTF-8 if null.static voidcopy(Reader input, OutputStream output, String encoding) Copy chars from aReaderto bytes on anOutputStreamusing the specified character encoding, and calling flush.static intCopy chars from aReaderto aWriter.static voidCopy f1 into f2 (mkdirs for f2)static voidCopy filename1 into filename2 (mkdirs for filename2)static FileCreates a new empty directorystatic FilecreateTempDir(String name, String prefix, File parentDir, boolean clear) Creates a new empty directorystatic voidPerform a directory deep copy.static voidPerform a directory deep copy.static voiddeepCopy(File srcDir, File tgtDir, boolean deleteEmptyFile, FileFilter filter) Perform a directory deep copy.static voiddeepDelete(File file) Deletes a whole directory.static voiddeepDelete(File file, ActionListener listener) Deletes a whole directory.static voidDelete recursively all enpty directories under a given directory.static voiddeepDeleteEmptyDirectories(File dir, FileFilter filter) Delete recursively all enpty directories under a given directory.static voiddeepDeleteEmptyDirectories(File dir, FileFilter filter, ActionListener listener) Delete recursively all enpty directories under a given directory.deepListFiles(File dir, FileFilter filter) Return a list of files from the given directory and matching the given filter.deepListFiles(File dir, FileFilter filter, boolean checkDir) Return a list of files from the given directory and matching the given filter.static voidFollow 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 platformstatic StringCompresses (deflates) a string and Base64 encodes the result and returns it as a string.static intdownloadFile(URL url, File file, long lastModified, String ticket) Download the given URLstatic byte[]Emptys an InputStreamstatic voidforceDeepCopy(File srcDir, File tgtDir, boolean deleteEmptyFile, FileFilter filter) Perform a directory deep copy.static StringgetDownloadTicket(File file) Get a download ticket for the given filestatic StringgetExtension(File file) Returns the extension of the given file (without the leading "." char).static StringgetExtension(String filename) Returns the extension of the given file (without the leading "." char).static InputStreamgetInputStream(HttpURLConnection httpConn) Retrieve the input stream of the specified HttpURLConnection, decoded appropriately if gzip was used.static StringGives the mime type of a file.static StringgetNewDirectory(File baseDir, String subDir, long maxFilesPerDir) Returns the relative path of directory to use for the new document to be created.static StringgetQualifiedName(File file) Returns the name of a given file, without its extension.static StringgetQualifiedName(String filename) Returns the name of a given file, without its extension.static StringgetRelativePath(File dir, File file) Return the relative path of the specified file in the specified directory.static Filestatic FilegetTempDir(String name, String prefix, File parentDir) Returns An abstract pathname denoting an empty directory (this directory is not created).static InputStreamgetZipFileStream(String fileName, File zipFile) Returns an InputStream on the File representing by the given fileName in the given zip file.static InputStreamgetZipFileStream(String fileName, File zipFile, boolean regexp) static booleangoToLine(InputStream in, long line) Move the position of the given InputStream to the given line.static booleangoToLine(RandomAccessFile raf, long line) Move the position of the given InputStream to the given line.static booleanCheck if a File is a child of an other File recursively.static booleanisSameContent(File f1, File f2) Returns true if the given files have the same content.static booleanMove f1 into f2 (mkdirs for f2).static HttpURLConnectionopenConnection(URL url, boolean doOutput, boolean doInput, String method) Open a new HttpURLConnection for the specified urlstatic String[]Returns all the lines read on a given Reader.static voidremoveBlankLines(String filename, boolean trim) Remove all blank lines of a file.static voidsearchAndReplace(File file, String[] search, String[] replace) Execute an array of regular expression on a given file content.static voidsearchAndReplace(File file, Map<String, String> map) Execute a map of regular expression on a given file content.static StringsubstringFile(File parentFile, File childFile) If Parent is parent of child then we return the substraction: childPath - parentPath.static String[]Returns the last lines of a file.static FiletranslateFile(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.static voidUnzip a filestatic voidunzip(File file, File dir, FileFilter filter) Unzip a filestatic voidunzip(File file, File dir, FileFilter filter, Charset charset) Unzip a filestatic voidUnzip a filestatic voidunzip(String filename, String tgtDirname, FileFilter filter) 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.shtmlstatic voidunzip(String filename, String tgtDirname, FileFilter filter, Charset charset) 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.shtmlstatic voidwriteArray(Writer writer, String[] array) Writes an array of String on a given Writer.static voidwriteEnumeration(Writer writer, Enumeration<?> enu) Writes an enumeration of object on a given Writer.static voidwriteIterator(Writer writer, Iterator<?> it) Writes an iteration of object on a given Writer.static voidzip(File[] files, String destFile, int[] prefixLength, FileFilter[] filters) Zip an array of files or directories into a file.static voidzip(File file, String zipFile, int prefixLength, FileFilter filter) static StringZip a file or a whole directory.static voidZip a file or a whole directorystatic voidzipWriter(File file, ZipOutputStream zos, int prefixLength, FileFilter filter) Zip all files of a directory that are accepted by the given FileFilter.
-
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
- See Also:
-
-
Method Details
-
readList
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
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 followfileSet- a Set where to put all filesblackSet- a Set of String to identify Files that ends with that String
-
deepListFiles
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 explorefilter- the FileFilter to use (may be null)- Returns:
- a List of java.io.File
- Since:
- jcms-4.1
- See Also:
-
deepListFiles
Return a list of files from the given directory and matching the given filter.- Parameters:
dir- the directory to explorefilter- 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
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 FilechildFile- the child File- Returns:
- childPath - parentPath
-
writeArray
Writes an array of String on a given Writer.- Parameters:
writer- the writerarray- the array of String to write- Throws:
IOException- if an exception occurs
-
writeEnumeration
Writes an enumeration of object on a given Writer. Uses the toString() method.- Parameters:
writer- the writerenu- the enumeration- Throws:
IOException- if an exception occurs
-
writeIterator
Writes an iteration of object on a given Writer. Uses the toString() method.- Parameters:
writer- the writerit- the iterator- Throws:
IOException- if an exception occurs
-
unzip
Unzip a file- Parameters:
file- the file to unzipdir- the target directory- Throws:
IOException- if an exception occurs- See Also:
-
unzip
Unzip a file- Parameters:
file- the file to unzipdir- the target directoryfilter- an optional FileFilter to filter the file to be extracted.- Throws:
IOException- if an exception occurs- See Also:
-
unzip
Unzip a file- Parameters:
filename- the name of the file to unziptgtDirname- the name of the target directory- Throws:
IOException- if an exception occurs- See Also:
-
unzip
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 unziptgtDirname- the name of the target directoryfilter- 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 unzipdir- the target directoryfilter- 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 unziptgtDirname- the name of the target directoryfilter- 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, theCp437charset 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
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 zipisParentDirRemoved- 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 zipzipFile- the name of the zip file to produceisParentDirRemoved- 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 zippeddestFile- the path of the destination zip fileprefixLength- 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 zippedzos- the ZipOutputStream into which the file has to be addedprefixLength- the size of the prefixfilter- the filter to be used- Throws:
IOException- if an exception occurs
-
getZipFileStream
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 seekzipFile- File reprenst the zip- Returns:
- InputStream an InputStream to the given file in the zip
-
getZipFileStream
-
deflateAndBase64EncodeString
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
Deletes a whole directory. Use it with care !- Parameters:
file- the directory to destroy
-
deepDelete
Deletes a whole directory. Use it with care !- Parameters:
file- the directory to destroylistener- a custom ActionListener used to perform custom actino during recursive calls- Since:
- jcms5.7.3
-
deepDeleteEmptyDirectories
Delete recursively all enpty directories under a given directory. THe given directory will also be deleted.- Parameters:
dir- the directory to destroy
-
deepDeleteEmptyDirectories
Delete recursively all enpty directories under a given directory. THe given directory will also be deleted.- Parameters:
dir- the directory to destroyfilter- files to ignore and remove
-
deepDeleteEmptyDirectories
Delete recursively all enpty directories under a given directory. The given directory will also be deleted.- Parameters:
dir- the directory to destroyfilter- FileFilter used to find files to be deleted before checking whether the directory is emptylistener- to perform action during recursive calls
-
translateFile
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 translatesrcDir- the source directorytgtDir- the target directory- Returns:
- the translated file
- Since:
- jcms-4.0.2
-
searchAndReplace
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 parsesearch- an array of regular expression to matchreplace- ann array of regular expression to replace
-
searchAndReplace
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 parsemap- 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 copytgtDir- the target directorydeleteEmptyFile- 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 copytgtDir- the target directorydeleteEmptyFile- if true delete empty files.filter- the filter to be used (may be null)- Since:
- jcms-5.7.4
-
deepCopy
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 copytgtDir- the target directorydeleteEmptyFile- if true delete empty files.- Throws:
IOException- if an exception occurs- Since:
- jcms-4.0.2
-
deepCopy
Perform a directory deep copy.- Parameters:
srcDir- the source directory to copytgtDir- the target directory- Throws:
IOException- if an exception occurs
-
isParent
Check if a File is a child of an other File recursively.- Parameters:
parent- the parent filechild- the child file- Returns:
- boolean
-
copyFile
Copy filename1 into filename2 (mkdirs for filename2)- Parameters:
filename1- the source filefilename2- the target file- Throws:
IOException- if an exception occurs
-
copyFile
Copy f1 into f2 (mkdirs for f2)- Parameters:
f1- the source filef2- the target file- Throws:
IOException- if an exception occurs
-
moveFile
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 filef2- the target file- Returns:
trueif and only if the moving succeeded;falseotherwise.- Since:
- jcms-4.0.2
-
emptyInputStream
Emptys an InputStream- Parameters:
dis- the DataInputStream to empty- Returns:
- the array of byte read.
-
removeBlankLines
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
- Returns:
- the path to the tmp directory
- Since:
- jcms-10.0.8
-
createTempDir
Creates a new empty directory- Returns:
- An abstract pathname denoting a newly-created empty directory
-
createTempDir
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 nameparentDir- The parent directory in which the directory is to be created, or null if the default temporary-file directory is to be usedclear- 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
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 nameparentDir- 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
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 testcheckParent- true if the parent must checked- Returns:
- true if this file can be write
- Since:
- jcms-4.0.2
-
getRelativePath
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.
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 directoryfile- the file- Returns:
- the relative path or null if no relative path can be computed.
- Since:
- jcms-4.1
-
belongsToDirectory
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 directoryuntrustedFile- 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
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
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
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.XMLreturnsxml.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. zigorfoo/bar. éxtreturns empty string.
The regular expression defining the accepted extension can be override using system propertycom.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
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.XMLreturnsxml.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. zigorfoo/bar. éxtreturns empty string.
The regular expression defining the accepted extension can be override using system propertycom.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
Returns the name of a given file, without its extension.Examples :
FileName.ExtreturnsFileNameFile.Name.extreturnsFile.NameFileNamereturnsFileNamepath/to/FileNamereturnsFileName
Dot-files are returned entirely (since JCMS-2732 / jcms-7.0.3).
Example:.bashrcreturns.bashrc.Files with invalid extension (see
getExtension(File)) are returned entirely (since JCMS-8976 / jcms-10.0.6)
Examples:foo/bar. zigreturnsbar. zig,foo/bar. éxtreturnsbar. é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
Returns the name of a given file, without its extension.Examples :
FileName.ExtreturnsFileNameFile.Name.extreturnsFile.NameFileNamereturnsFileNamepath/to/FileNamereturnsFileName
Dot-files are returned entirely (since JCMS-2732 / jcms-7.0.3).
Example:.bashrcreturns.bashrc.Files with invalid extension (see
getExtension(File)) are returned entirely (since JCMS-8976 / jcms-10.0.6)
Examples:foo/bar. zigreturnsbar. zig,foo/bar. éxtreturnsbar. éxt- Parameters:
filename- the file name- Returns:
- String the qualified name of the given File
- Since:
- jcms-5.5.0
- See Also:
-
convertCharset
Encode the given file with a given charset.- Parameters:
file- the file to be encodedfromCS- the original charset of the filetoCS- the new charset of the file- Throws:
IOException- if an exception occurs- Since:
- jcms-5.5.0
-
copy
Copy bytes from anInputStreamto anOutputStream.This method buffers the input internally, so there is no need to use a
BufferedInputStream.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write to- Returns:
- the number of bytes copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- jcms-5.5.0
-
copy
Copy bytes from anInputStreamto chars on aWriterusing 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- theInputStreamto read fromoutput- theWriterto write toencoding- the encoding to use, null means platform default- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- jcms-5.5.0
-
copy
Copy chars from aReaderto aWriter.This method buffers the input internally, so there is no need to use a
BufferedReader.- Parameters:
input- theReaderto read fromoutput- theWriterto write to- Returns:
- the number of characters copied
- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- jcms-5.5.0
-
copy
Copy chars from aReaderto bytes on anOutputStreamusing 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- theReaderto read fromoutput- theOutputStreamto write toencoding- the encoding to use, null means platform default- Throws:
NullPointerException- if the input or output is nullIOException- if an I/O error occurs- Since:
- jcms-5.5.0
-
closeQuietly
Close the given reader. This convenient method is typically used in a finally block.- Parameters:
reader- the Reader.- Since:
- jcms-5.6.0
-
closeQuietly
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
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
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
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
Returns true if the given files have the same content.- Parameters:
f1- the first file to be comparedf2- the second file to be compared- Returns:
- true if the two files have the same content.
- Since:
- jcms-5.7.0
-
getNewDirectory
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 filesize- how many lines to returnsskipBlank- if true skip the blank linesencoding- the file encoding- Returns:
- the last lines of a file.
- Throws:
IOException- if an exception occurs- Since:
- jcms-5.7.2
-
getDownloadTicket
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
Download the given URL- Parameters:
url- the URL to downloadfile- the target filelastModified- if > 0, the last lastModified date of the file will be set to this valueticket- 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 openeddoOutput- seeURLConnection.setDoOutput(boolean)doInput- seeURLConnection.setDoInput(boolean)method- seeHttpURLConnection.setRequestMethod(String)- Returns:
- a new instance of HttpURLConnection
- Throws:
IOException- if an exception occurs- Since:
- jcms-4.1
-
getInputStream
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
Returns true if the given file exists and has the given size and the given mtime.- Parameters:
file- the filesize- the excepted sizemtime- 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
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 :UtfUtilMagicNumberMagicMimeTypeIdentifierMagicStringMagicNumber- Parameters:
file- the file to test- Returns:
- the mime type of the file or null if not found
- Since:
- JCMS-5.7.3
-