|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.jalios.io.IOUtil
public class IOUtil
This class provides a set of static methods which perform various i/o operations.
Field Summary | |
---|---|
static int |
DOWNLOAD_ERROR
|
static int |
DOWNLOAD_OK
|
static String |
MIMETYPE_PLAINTEXT
|
protected static org.apache.oro.text.regex.Pattern |
patternBlank1
|
protected static org.apache.oro.text.regex.Pattern |
patternBlank2
|
static String |
REVISION
|
Method Summary | |
---|---|
static String |
base64DecodeAndInflateString(String str)
Inflates Base64 encoded string. |
static boolean |
canWrite(File file,
boolean checkParent)
Tests whether the application can modify to the file denoted by the given file. |
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. |
static void |
closeQuietly(InputStream inputStream)
Close the given inputStream. |
static void |
closeQuietly(OutputStream outputStream)
Close the given writer. |
static void |
closeQuietly(Reader reader)
Close the given reader. |
static void |
closeQuietly(Writer writer)
Close the given writer. |
static void |
convertCharset(File file,
String fromCS,
String toCS)
Encode the given file with a given charset. |
static int |
copy(InputStream input,
OutputStream output)
Copy bytes from an InputStream to an
OutputStream . |
static void |
copy(InputStream input,
Writer output,
String encoding)
Copy bytes from an InputStream to chars on a
Writer using the specified character encoding, or UTF-8 if null. |
static void |
copy(Reader input,
OutputStream output,
String encoding)
Copy chars from a Reader to bytes on an
OutputStream using the specified character encoding, and
calling flush. |
static int |
copy(Reader input,
Writer output)
Copy chars from a Reader to a Writer . |
static void |
copyFile(File f1,
File f2)
Copy f1 into f2 (mkdirs for f2) |
static void |
copyFile(String filename1,
String filename2)
Copy filename1 into filename2 (mkdirs for filename2) |
static File |
createTempDir()
Creates a new empty directory |
static File |
createTempDir(String name,
String prefix,
File parentDir,
boolean clear)
Creates a new empty directory |
static void |
deepCopy(File srcDir,
File tgtDir)
Perform a directory deep copy. |
static void |
deepCopy(File srcDir,
File tgtDir,
boolean deleteEmptyFile)
Perform a directory deep copy. |
static void |
deepCopy(File srcDir,
File tgtDir,
boolean deleteEmptyFile,
FileFilter filter)
Perform a directory deep copy. |
static void |
deepDelete(File file)
Deletes a whole directory. |
static void |
deepDelete(File file,
ActionListener listener)
Deletes a whole directory. |
static void |
deepDeleteEmptyDirectories(File dir)
Delete recursively all enpty directories under a given directory. |
static void |
deepDeleteEmptyDirectories(File dir,
FileFilter filter)
Delete recursively all enpty directories under a given directory. |
static void |
deepDeleteEmptyDirectories(File dir,
FileFilter filter,
ActionListener listener)
Delete recursively all enpty directories under a given directory. |
static List<File> |
deepListFiles(File dir,
FileFilter filter)
Return a list of files from the given directory and matching the given filter. |
static List<File> |
deepListFiles(File dir,
FileFilter filter,
boolean checkDir)
Return a list of files from the given directory and matching the given filter. |
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 |
static String |
deflateAndBase64EncodeString(String str)
Compresses (deflates) a string and Base64 encodes the result and returns it as a string. |
static int |
downloadFile(URL url,
File file,
long lastModified,
String ticket)
Download the given URL |
static byte[] |
emptyInputStream(DataInputStream dis)
Emptys an InputStream |
static void |
forceDeepCopy(File srcDir,
File tgtDir,
boolean deleteEmptyFile,
FileFilter filter)
Perform a directory deep copy. |
static String |
getDownloadTicket(File file)
Get a download ticket for the given file |
static String |
getExtension(File file)
Returns the extension of the given file (without the leading "." char). |
static String |
getExtension(String filename)
Returns the extension of the given file (without the leading "." char). |
static InputStream |
getInputStream(HttpURLConnection httpConn)
Retrieve the input stream of the specified HttpURLConnection, decoded appropriately if gzip was used. |
static String |
getMimeTypeFromMagicNumber(File file)
Gives the mime type of a file. |
static String |
getNewDirectory(File baseDir,
String subDir,
long maxFilesPerDir)
Returns the relative path of directory to use for the new document to be created. |
static String |
getQualifiedName(File file)
This method return the name of a given file it return string before last '.' of the filename. |
static String |
getQualifiedName(String filename)
This method return the name of a given file name it return string before last '.' of the filename. |
static String |
getRelativePath(File dir,
File file)
Return the relative path of the given file according the given directory. |
static File |
getTempDir(String name,
String prefix,
File parentDir)
Returns An abstract pathname denoting an empty directory (this directory is not created). |
static InputStream |
getZipFileStream(String fileName,
File zipFile)
Returns an InputStream on the File representing by the given fileName in the given zip file. |
static boolean |
goToLine(InputStream in,
long line)
Move the position of the given InputStream to the given line. |
static boolean |
goToLine(RandomAccessFile raf,
long line)
Move the position of the given InputStream to the given line. |
static boolean |
isParent(File parent,
File child)
Check if a File is a child of an other File recursively. |
static boolean |
isSameContent(File f1,
File f2)
Returns true if the given files have the same content. |
static boolean |
moveFile(File f1,
File f2)
Move f1 into f2 (mkdirs for f2). |
static HttpURLConnection |
openConnection(URL url,
boolean doOutput,
boolean doInput,
String method)
Open a new HttpURLConnection for the specified url |
static String[] |
readList(Reader reader)
Returns all the lines read on a given Reader. |
static void |
removeBlankLines(String filename,
boolean trim)
Remove all blank lines of a file. |
static void |
searchAndReplace(File file,
Map<String,String> map)
Execute a map of regular expression on a given file content. |
static void |
searchAndReplace(File file,
String[] search,
String[] replace)
Execute an array of regular expression on a given file content. |
static String |
substringFile(File parentFile,
File childFile)
If Parent is parent of child then we return the substraction: childPath - parentPath. |
static String[] |
tail(File file,
int size,
boolean skipBlank,
String encoding)
Returns the last lines of a file. |
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. |
static void |
unzip(File file,
File dir)
Unzip a file |
static void |
unzip(File file,
File dir,
FileFilter filter)
Unzip a file |
static void |
unzip(String filename,
String tgtDirname)
Unzip a file |
static void |
unzip(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.shtml |
static void |
writeArray(Writer writer,
String[] array)
Writes an array of String on a given Writer. |
static void |
writeEnumeration(Writer writer,
Enumeration<?> enu)
Writes an enumeration of object on a given Writer. |
static void |
writeIterator(Writer writer,
Iterator<?> it)
Writes an iteration of object on a given Writer. |
static void |
zip(File[] files,
String destFile,
int[] prefixLength,
FileFilter[] filters)
Zip an array of files or directories into a file. |
static void |
zip(File file,
String zipFile,
int prefixLength,
FileFilter filter)
|
static String |
zip(String filename,
boolean isParentDirRemoved)
Zip a file or a whole directory. |
static void |
zip(String filename,
String zipFile,
boolean isParentDirRemoved)
Zip a file or a whole directory |
static void |
zipWriter(File file,
ZipOutputStream zos,
int prefixLength,
FileFilter filter)
Zip all files of a directory that are accepted by the given FileFilter. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String REVISION
public static int DOWNLOAD_ERROR
public static int DOWNLOAD_OK
protected static org.apache.oro.text.regex.Pattern patternBlank1
protected static org.apache.oro.text.regex.Pattern patternBlank2
public static final String MIMETYPE_PLAINTEXT
Method Detail |
---|
public static String[] readList(Reader reader) throws IOException
reader
- the reader
IOException
public static void deepListFiles(File file, Set<File> fileSet, Set<String> blackSet)
file
- the directory to followfileSet
- a Set where to put all filesblackSet
- a Set of String to identify Files that ends with that Stringpublic static List<File> deepListFiles(File dir, FileFilter filter)
dir
- the directory to explorefilter
- the FileFilter to use (may be null)
deepListFiles(File, FileFilter, boolean)
public static List<File> deepListFiles(File dir, FileFilter filter, boolean checkDir)
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.
public static String substringFile(File parentFile, File childFile)
parentFile
- the parent FilechildFile
- the child File
public static void writeArray(Writer writer, String[] array) throws IOException
writer
- the writerarray
- the array of String to write
IOException
public static void writeEnumeration(Writer writer, Enumeration<?> enu) throws IOException
writer
- the writerenu
- the enumeration
IOException
public static void writeIterator(Writer writer, Iterator<?> it) throws IOException
writer
- the writerit
- the iterator
IOException
public static void unzip(File file, File dir) throws IOException
file
- the file to unzipdir
- the target directory
IOException
unzip(String, String, FileFilter)
public static void unzip(File file, File dir, FileFilter filter) throws IOException
file
- the file to unzipdir
- the target directoryfilter
- an optional FileFilter to filter the file to be extracted.
IOException
unzip(String, String, FileFilter)
public static void unzip(String filename, String tgtDirname) throws IOException
filename
- the name of the file to unziptgtDirname
- the name of the target directory
IOException
unzip(String, String, FileFilter)
public static void unzip(String filename, String tgtDirname, FileFilter filter) throws IOException
filename
- the name of the file to unziptgtDirname
- the name of the target directoryfilter
- an optional FileFilter to filter the file to be extracted.
IOException
public static String zip(String filename, boolean isParentDirRemoved) throws IOException
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/")
IOException
public static void zip(String filename, String zipFile, boolean isParentDirRemoved) throws IOException
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/")
IOException
public static void zip(File file, String zipFile, int prefixLength, FileFilter filter) throws IOException
IOException
public static void zip(File[] files, String destFile, int[] prefixLength, FileFilter[] filters) throws IOException
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.
IOException
public static void zipWriter(File file, ZipOutputStream zos, int prefixLength, FileFilter filter) throws IOException
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
IOException
public static InputStream getZipFileStream(String fileName, File zipFile)
fileName
- the file name of the file to seekzipFile
- File reprenst the zip
public static String deflateAndBase64EncodeString(String str) throws IOException
str
- The string to compress and encode.
IOException
- Will probably not happen, maybe if there is a bug in the
code or we run out of memory.public static String base64DecodeAndInflateString(String str) throws DataFormatException, UnsupportedEncodingException
str
- String should be compressed and Base64 encoded data.
DataFormatException
- on bad input.
UnsupportedEncodingException
public static void deepDelete(File file)
file
- the directory to destroypublic static void deepDelete(File file, ActionListener listener)
file
- the directory to destroylistener
- a custom ActionListener used to perform custom actino during recursive callspublic static void deepDeleteEmptyDirectories(File dir)
dir
- the directory to destroypublic static void deepDeleteEmptyDirectories(File dir, FileFilter filter)
dir
- the directory to destroyfilter
- files to ignore and removepublic static void deepDeleteEmptyDirectories(File dir, FileFilter filter, ActionListener listener)
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 callspublic static File translateFile(File file, File srcDir, File tgtDir)
file
- the file to translatesrcDir
- the source directorytgtDir
- the target directory
public static void searchAndReplace(File file, String[] search, String[] replace)
file
- the file to parsesearch
- an array of regular expression to matchreplace
- ann array of regular expression to replacepublic static void searchAndReplace(File file, Map<String,String> map)
file
- the file to parsemap
- a map with regexp as key and substitute string as value.public static void deepCopy(File srcDir, File tgtDir, boolean deleteEmptyFile, FileFilter filter) throws IOException
srcDir
- the source directory to copytgtDir
- the target directorydeleteEmptyFile
- if true delete empty files.filter
- the filter to be used (may be null)
IOException
- if it append for one file.public static void forceDeepCopy(File srcDir, File tgtDir, boolean deleteEmptyFile, FileFilter filter)
srcDir
- the source directory to copytgtDir
- the target directorydeleteEmptyFile
- if true delete empty files.filter
- the filter to be used (may be null)public static void deepCopy(File srcDir, File tgtDir, boolean deleteEmptyFile) throws IOException
srcDir
- the source directory to copytgtDir
- the target directorydeleteEmptyFile
- if true delete empty files.
IOException
public static void deepCopy(File srcDir, File tgtDir) throws IOException
srcDir
- the source directory to copytgtDir
- the target directory
IOException
public static boolean isParent(File parent, File child)
parent
- the parent filechild
- the child file
public static void copyFile(String filename1, String filename2) throws IOException
filename1
- the source filefilename2
- the target file
IOException
public static void copyFile(File f1, File f2) throws IOException
f1
- the source filef2
- the target file
IOException
public static boolean moveFile(File f1, File f2)
f1
- the source filef2
- the target file
true
if and only if the moving succeeded; false
otherwise.public static byte[] emptyInputStream(DataInputStream dis)
dis
- the DataInputStream to empty
public static void removeBlankLines(String filename, boolean trim) throws IOException
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*$/).
IOException
public static File createTempDir()
public static File createTempDir(String name, String prefix, File parentDir, boolean clear)
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
public static File getTempDir(String name, String prefix, File parentDir)
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
public static boolean canWrite(File file, boolean checkParent)
file
- the file to testcheckParent
- true if the parent must checked
public static String getRelativePath(File dir, File file)
getRelativePath(new File("/foo/"), new File("/foo/bar/out.txt"))
will return bar/out.txt
. The path is plateform independant. "/"
is used as file separator.
dir
- the directoryfile
- the file
public static boolean goToLine(InputStream in, long line) throws IOException
in
- the InputStream to move the position.line
- the line to be reached
IOException
public static boolean goToLine(RandomAccessFile raf, long line) throws IOException
raf
- the RandomAccessFile to move the position.line
- the line to be reached
IOException
public static String getExtension(File file)
foo/bar.xml
it returns xml
.
file
- the file to get the extension
public static String getExtension(String filename)
foo/bar.xml
it returns xml
.
filename
- the filename to get the extension
getExtension(File)
public static String getQualifiedName(File file)
file
- the File of which to retrieve qualified name
public static String getQualifiedName(String filename)
filename
- the file name
getQualifiedName(File)
public static void convertCharset(File file, String fromCS, String toCS) throws IOException
file
- the file to be encodedfromCS
- the original charset of the filetoCS
- the new charset of the file
IOException
public static int copy(InputStream input, OutputStream output) throws IOException
InputStream
to an
OutputStream
.
This method buffers the input internally, so there is no need to use a
BufferedInputStream
.
input
- the InputStream
to read fromoutput
- the OutputStream
to write to
NullPointerException
- if the input or output is null
IOException
- if an I/O error occurspublic static void copy(InputStream input, Writer output, String encoding) throws IOException
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
.
input
- the InputStream
to read fromoutput
- the Writer
to write toencoding
- the encoding to use, null means platform default
NullPointerException
- if the input or output is null
IOException
- if an I/O error occurspublic static int copy(Reader input, Writer output) throws IOException
Reader
to a Writer
.
This method buffers the input internally, so there is no need to use a
BufferedReader
.
input
- the Reader
to read fromoutput
- the Writer
to write to
NullPointerException
- if the input or output is null
IOException
- if an I/O error occurspublic static void copy(Reader input, OutputStream output, String encoding) throws IOException
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
.
input
- the Reader
to read fromoutput
- the OutputStream
to write toencoding
- the encoding to use, null means platform default
NullPointerException
- if the input or output is null
IOException
- if an I/O error occurspublic static void closeQuietly(Reader reader)
reader
- public static void closeQuietly(Writer writer)
writer
- public static void closeQuietly(InputStream inputStream)
inputStream
- public static void closeQuietly(OutputStream outputStream)
outputStream
- public static boolean isSameContent(File f1, File f2)
f1
- the first file to be comparedf2
- the second file to be compared
public static String getNewDirectory(File baseDir, String subDir, long maxFilesPerDir)
baseDir
- the base directory (eg /usr/local/tomcat/webapps/jcms/)subDir
- an optionnal sub directory, may be null (eg upload/docs/image/gif/)maxFilesPerDir
- how many files are allowed per directory.
public static String[] tail(File file, int size, boolean skipBlank, String encoding) throws IOException
file
- the filesize
- how many lines to returnsskipBlank
- if true skip the blank linesencoding
- the file encoding
IOException
public static String getDownloadTicket(File file)
file
- the file to get a ticket for
public static int downloadFile(URL url, File file, long lastModified, String ticket)
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
public static HttpURLConnection openConnection(URL url, boolean doOutput, boolean doInput, String method) throws IOException
url
- the URL for which HttpURLConnection is openeddoOutput
- see URLConnection.setDoOutput(boolean)
doInput
- see URLConnection.setDoInput(boolean)
method
- see HttpURLConnection.setRequestMethod(String)
IOException
public static InputStream getInputStream(HttpURLConnection httpConn) throws IOException
httpConn
- the HttpUrlConnection being used
IOException
public static boolean checkFileSizeAndMtime(File file, long size, long mtime)
file
- the filesize
- the excepted sizemtime
- the expected mtime
public static String getMimeTypeFromMagicNumber(File file)
UtfUtil
MagicNumber
MagicMimeTypeIdentifier
MagicString
MagicNumber
file
- the file to test
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |