Class IOUtil
- java.lang.Object
-
- org.identityconnectors.common.IOUtil
-
public final class IOUtil extends java.lang.Object
IO Utilities.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
checksum(java.io.File file)
Calculates the CRC32 checksum of the specified file.static long
checksum(java.lang.String fileName)
Calculates the CRC32 checksum of the specified file.static boolean
copyFile(java.io.File src, java.io.File dest)
Copies a file to a destination.static long
copyFile(java.io.InputStream fis, java.io.OutputStream fos)
Copies one file to another.static void
delete(java.io.File file)
Recursively delete all the files in a directory and the directory.static void
extractResourceToFile(java.lang.Class<?> clazz, java.lang.String path, java.io.File file)
Extracts the resource to a file.static byte[]
getResourceAsBytes(java.lang.Class<?> clazz, java.lang.String res)
Get the resource as a byte array.static java.util.Properties
getResourceAsProperties(java.lang.ClassLoader loader, java.lang.String path)
Loads the given resource as a properties object.static java.io.InputStream
getResourceAsStream(java.lang.Class<?> clazz, java.lang.String res)
Returns an input stream of the resource specified.static java.lang.String
getResourceAsString(java.lang.Class<?> clazz, java.lang.String res)
Read the entire stream into a String and return it.static java.lang.String
getResourceAsString(java.lang.Class<?> clazz, java.lang.String res, java.nio.charset.Charset charset)
Read the entire stream into a String and return it.static java.lang.String
getResourcePath(java.lang.Class<?> c, java.lang.String res)
Get the path to a resource base on the package of given class.static byte[]
inputStreamToBytes(java.io.InputStream ins)
Takes a 'InputStream' and returns a byte array.static java.lang.String
join(java.lang.Object[] array, char separator)
static java.lang.String
join(java.lang.Object[] array, char separator, int startIndex, int endIndex)
static java.lang.String
join(java.util.Collection<java.lang.String> collection, char separator)
static java.util.Properties
loadPropertiesFile(java.io.File file)
Loads the given file as a Properties file.static java.util.Properties
loadPropertiesFile(java.lang.String string)
Attempt to load file based on a string base filename.static java.net.URL
makeURL(java.io.File dir, java.lang.String path)
Make a URL from a directory and path.static void
quietClose(java.io.InputStream stream)
Quietly closes the stream.static void
quietClose(java.io.OutputStream stream)
Quietly closes the stream.static void
quietClose(java.io.Reader reader)
Quietly closes the reader.static void
quietClose(java.io.Writer writer)
Quietly closes the writer.static void
quietClose(java.sql.Connection conn)
Quietly closes the connection.static void
quietClose(java.sql.ResultSet resultset)
Quietly closes the resultset.static void
quietClose(java.sql.Statement stmt)
Quietly closes the statement.static java.lang.String
readerToString(java.io.Reader rdr)
Takes a 'Reader' and returns the contents as a string.static byte[]
readFileBytes(java.io.File file)
Reads the given file as bytesstatic java.lang.String
readFileUTF8(java.io.File file)
Reads the given file as UTF-8static byte[]
readInputStreamBytes(java.io.InputStream is, boolean close)
Reads an entire file and returns the bytes.static void
storePropertiesFile(java.io.File f, java.util.Properties properties)
Stores the given file as a Properties file.static void
unjar(java.util.jar.JarFile jarFile, java.io.File toDir)
Unjars the given file to the given directory.static void
writeFileUTF8(java.io.File file, java.lang.String contents)
Write the contents of the string out to a file in UTF-8 format.
-
-
-
Field Detail
-
EMPTY
public static final java.lang.String EMPTY
Empty""
string.- See Also:
- Constant Field Values
-
UTF8
public static final java.lang.String UTF8
- See Also:
- Constant Field Values
-
-
Method Detail
-
quietClose
public static void quietClose(java.io.Reader reader)
Quietly closes the reader.This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the reader and catch any
IOException
which may be thrown and ignore it.- Parameters:
reader
- Reader to close
-
quietClose
public static void quietClose(java.io.InputStream stream)
Quietly closes the stream.This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the stream and catch any
IOException
which may be thrown.- Parameters:
stream
- Stream to close
-
quietClose
public static void quietClose(java.io.Writer writer)
Quietly closes the writer.This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the Writer and catch any
IOException
which may be thrown.- Parameters:
writer
- Writer to close
-
quietClose
public static void quietClose(java.io.OutputStream stream)
Quietly closes the stream.This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the stream and catch any
IOException
which may be thrown.- Parameters:
stream
- Stream to close
-
quietClose
public static void quietClose(java.sql.Statement stmt)
Quietly closes the statement.This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the statement and catch any
SQLException
which may be thrown.- Parameters:
stmt
- Statement to close- Since:
- 1.3
-
quietClose
public static void quietClose(java.sql.Connection conn)
Quietly closes the connection.This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the connection and catch any
SQLException
which may be thrown.- Parameters:
conn
- Connection to close- Since:
- 1.3
-
quietClose
public static void quietClose(java.sql.ResultSet resultset)
Quietly closes the resultset.This avoids having to handle exceptions, and then inside of the exception handling have a try catch block to close the connection and catch any
SQLException
which may be thrown.- Parameters:
resultset
- ResultSet to close- Since:
- 1.3
-
getResourcePath
public static java.lang.String getResourcePath(java.lang.Class<?> c, java.lang.String res)
Get the path to a resource base on the package of given class.- Parameters:
c
- Class to get the package path too.res
- Name of the resource to get the path of.- Returns:
- Returns the fully quilified path to a resource.
-
getResourceAsStream
public static java.io.InputStream getResourceAsStream(java.lang.Class<?> clazz, java.lang.String res)
Returns an input stream of the resource specified.- Parameters:
clazz
- The classres
- The resource name- Returns:
- Returns an InputStream to the resource.
-
getResourceAsBytes
public static byte[] getResourceAsBytes(java.lang.Class<?> clazz, java.lang.String res)
Get the resource as a byte array.- Parameters:
clazz
- The classres
- The resource name- Returns:
- a byte array
-
getResourceAsString
public static java.lang.String getResourceAsString(java.lang.Class<?> clazz, java.lang.String res, java.nio.charset.Charset charset)
Read the entire stream into a String and return it.- Parameters:
clazz
- The classres
- The resource name- Returns:
- the resource string
-
getResourceAsString
public static java.lang.String getResourceAsString(java.lang.Class<?> clazz, java.lang.String res)
Read the entire stream into a String and return it.- Parameters:
clazz
- The classres
- The resource name- Returns:
- the resource string
-
inputStreamToBytes
public static byte[] inputStreamToBytes(java.io.InputStream ins)
Takes a 'InputStream' and returns a byte array.- Parameters:
ins
- The InputStream- Returns:
- the resource byte array
-
readerToString
public static java.lang.String readerToString(java.io.Reader rdr)
Takes a 'Reader' and returns the contents as a string.- Parameters:
rdr
- Producer for the string data.- Returns:
- Null if the 'Reader' is broken or empty otherwise the contents as a string.
-
copyFile
public static boolean copyFile(java.io.File src, java.io.File dest) throws java.io.IOException
Copies a file to a destination.- Parameters:
src
- The source must be a filedest
- This can be a directory or a file.- Returns:
- True if succeeded otherwise false.
- Throws:
java.io.IOException
-
copyFile
public static long copyFile(java.io.InputStream fis, java.io.OutputStream fos) throws java.io.IOException
Copies one file to another.NOTE: does not close streams.
- Parameters:
fis
- input streamfos
- output stream- Returns:
- total bytes copied.
- Throws:
java.io.IOException
-
checksum
public static long checksum(java.lang.String fileName) throws java.io.IOException, java.io.FileNotFoundException
Calculates the CRC32 checksum of the specified file.- Parameters:
fileName
- the path to the file on which to calculate the checksum- Returns:
- the checksum
- Throws:
java.io.IOException
java.io.FileNotFoundException
-
checksum
public static long checksum(java.io.File file) throws java.io.IOException, java.io.FileNotFoundException
Calculates the CRC32 checksum of the specified file.- Parameters:
file
- the file on which to calculate the checksum- Returns:
- the checksum
- Throws:
java.io.IOException
java.io.FileNotFoundException
-
readInputStreamBytes
public static byte[] readInputStreamBytes(java.io.InputStream is, boolean close) throws java.io.IOException
Reads an entire file and returns the bytes.- Parameters:
is
- the input streamclose
- if true, close when finished reading.- Returns:
- file bytes.
- Throws:
java.io.IOException
-
delete
public static void delete(java.io.File file) throws java.io.IOException
Recursively delete all the files in a directory and the directory.- Parameters:
file
- the file to delete- Throws:
java.lang.RuntimeException
- if there is file that can not be deleted.java.io.IOException
-
loadPropertiesFile
public static java.util.Properties loadPropertiesFile(java.io.File file) throws java.io.IOException
Loads the given file as a Properties file.- Parameters:
file
- the file to load the propertied from- Returns:
- properties loaded
- Throws:
java.io.IOException
-
storePropertiesFile
public static void storePropertiesFile(java.io.File f, java.util.Properties properties) throws java.io.IOException
Stores the given file as a Properties file.- Throws:
java.io.IOException
-
getResourceAsProperties
public static java.util.Properties getResourceAsProperties(java.lang.ClassLoader loader, java.lang.String path) throws java.io.IOException
Loads the given resource as a properties object.- Parameters:
loader
- The class loaderpath
- The path to the resource- Returns:
- The properties or null if not found
- Throws:
java.io.IOException
- If an error occurs reading it
-
extractResourceToFile
public static void extractResourceToFile(java.lang.Class<?> clazz, java.lang.String path, java.io.File file) throws java.io.IOException
Extracts the resource to a file.- Parameters:
clazz
- The class, relative to which path is resolvedpath
- The path to the resourcefile
- The file to extract to- Throws:
java.io.IOException
- If an error occurs reading it
-
unjar
public static void unjar(java.util.jar.JarFile jarFile, java.io.File toDir) throws java.io.IOException
Unjars the given file to the given directory. Does not close the JarFile when finished.- Parameters:
jarFile
- The file to unjar.toDir
- The directory to unjar to.- Throws:
java.io.IOException
-
readFileUTF8
public static java.lang.String readFileUTF8(java.io.File file) throws java.io.IOException
Reads the given file as UTF-8- Parameters:
file
- The file to read- Returns:
- The contents of the file
- Throws:
java.io.IOException
- if there is an issue reading the file.
-
readFileBytes
public static byte[] readFileBytes(java.io.File file) throws java.io.IOException
Reads the given file as bytes- Parameters:
file
- The file to read- Returns:
- The contents of the file
- Throws:
java.io.IOException
- if there is an issue reading the file.
-
writeFileUTF8
public static void writeFileUTF8(java.io.File file, java.lang.String contents) throws java.io.IOException
Write the contents of the string out to a file in UTF-8 format.- Parameters:
file
- the file to write to.contents
- the contents of the file to write to.- Throws:
java.io.IOException
- if there is an issue writing the file.java.lang.NullPointerException
- if the file parameter is null.
-
makeURL
public static java.net.URL makeURL(java.io.File dir, java.lang.String path) throws java.io.IOException
Make a URL from a directory and path.- Parameters:
dir
- directory to start from.path
- file or path to create the url.- Returns:
- URL based on the parameter provided.
- Throws:
java.io.IOException
- if the URL create from the parameters does not specify a file.
-
loadPropertiesFile
public static java.util.Properties loadPropertiesFile(java.lang.String string) throws java.io.IOException
Attempt to load file based on a string base filename.- Parameters:
string
- represents the file.- Returns:
- a loaded properties file.
- Throws:
java.io.IOException
- if there is an issue.
-
join
public static java.lang.String join(java.util.Collection<java.lang.String> collection, char separator)
- Parameters:
collection
- The String collectionseparator
- the separator character- Returns:
- the joined string
- Since:
- 1.3
-
join
public static java.lang.String join(java.lang.Object[] array, char separator)
- Parameters:
array
- an arrayseparator
- the separator character- Returns:
- the joined string
- Since:
- 1.3
-
join
public static java.lang.String join(java.lang.Object[] array, char separator, int startIndex, int endIndex)
- Parameters:
array
- an arrayseparator
- the separator characterstartIndex
- the start indexendIndex
- the end index- Returns:
- the joined string
- Since:
- 1.3
-
-