@CompileStatic class IOUtils extends Object
Utility methods for performing I/O operations.
| Modifiers | Name | Description |
|---|---|---|
static int |
BUFFER_SIZE |
The default buffer size used when copying bytes. |
static String |
RESOURCE_JAR_PREFIX |
|
static String |
RESOURCE_WAR_PREFIX |
| Constructor and description |
|---|
IOUtils() |
| Type Params | Return Type | Name and description |
|---|---|---|
|
static void |
closeQuietly(Closeable closeable)Closes a closeable gracefully without throwing exceptions etc. |
|
static void |
copy(InputStream input, Writer output, String encoding)Copy an InputStream to the given writer with the given encoding |
|
static int |
copy(Resource resource, File out)Copy the contents of the given input File to the given output File. |
|
static int |
copy(Reader reader, Writer writer)Copy the contents of the given Reader to the given Writer. |
|
static int |
copy(InputStream is, OutputStream os)Copy the contents of the given InputStream to the given OutputStream. |
|
static String |
copyToString(Reader reader)Convert a reader to a String, reading the data from the reader |
|
static String |
copyToString(InputStream stream, String encoding)Convert a stream to a String, reading the data from the stream |
|
static XmlSlurper |
createXmlSlurper() |
|
static String |
findApplicationDirectory()Finds the directory where the Application class is contained |
|
static File |
findApplicationDirectoryFile() |
|
static File |
findApplicationDirectoryFile(Class targetClass)Finds the application directory for the given class |
|
static URL |
findClassResource(Class targetClass)Returns the URL resource for the location on disk of the given class or null if it cannot be found |
|
static File |
findJarFile(Class targetClass)Finds a JAR file for the given class |
|
static File |
findJarFile(Resource resource)Finds a JAR for the given resource |
|
static File |
findJarFile(URL resource)Finds a JAR for the given resource |
|
static URL |
findJarResource(Class targetClass)Returns the URL resource for the location on disk of the given class or null if it cannot be found |
|
static URL |
findResourceRelativeToClass(Class targetClass, String path)Finds a URL within a JAR relative (from the root) to the given class |
|
static URL |
findRootResource(Class targetClass)Returns a URL that represents the root classpath resource where the given class was loaded from |
|
static URL |
findRootResourcesURL(Class targetClass)This method differs from findRootResource(java.lang.Class) in that it will find the root URL where to load resources defined in src/main/resources |
|
static File |
findSourceFile(String className)Finds a source file for the given class name |
|
static boolean |
isWithinBinary(URL url)Whether the given URL is within a binary like a JAR or WAR file |
|
static javax.xml.parsers.SAXParser |
newSAXParser() |
|
static BufferedInputStream |
openStream(File file)Gracefully opens a stream for a file, throwing exceptions where appropriate. |
|
static String |
toString(Reader reader)Convert a reader to a String, reading the data from the reader |
|
static String |
toString(InputStream stream, String encoding)Convert a stream to a String, reading the data from the stream |
The default buffer size used when copying bytes.
Closes a closeable gracefully without throwing exceptions etc.
closeable - The closeableCopy an InputStream to the given writer with the given encoding
input - The inputoutput - The writerencoding - The encodingCopy the contents of the given input File to the given output File.
resource - the resource to copy fromout - the file to copy toCopy the contents of the given Reader to the given Writer. Closes both when done.
reader - the Reader to copy fromwriter - the Writer to copy toCopy the contents of the given InputStream to the given OutputStream. Closes both streams when done.
is - the stream to copy fromos - the stream to copy toConvert a reader to a String, reading the data from the reader
reader - The readerConvert a stream to a String, reading the data from the stream
stream - The streamFinds the directory where the Application class is contained
Finds the application directory for the given class
targetClass - The target classReturns the URL resource for the location on disk of the given class or null if it cannot be found
targetClass - The target classFinds a JAR file for the given class
targetClass - The target classFinds a JAR for the given resource
resource - The resourceFinds a JAR for the given resource
resource - The resourceReturns the URL resource for the location on disk of the given class or null if it cannot be found
targetClass - The target classFinds a URL within a JAR relative (from the root) to the given class @return
Returns a URL that represents the root classpath resource where the given class was loaded from
targetClass - The target classThis method differs from findRootResource(java.lang.Class) in that it will find the root URL where to load resources defined in src/main/resources At development time this with be build/main/resources, but in production it will be relative to the class. @return
Finds a source file for the given class name
className - The class nameWhether the given URL is within a binary like a JAR or WAR file
url - The URLGracefully opens a stream for a file, throwing exceptions where appropriate. Based off the commons-io method
file - The fileConvert a reader to a String, reading the data from the reader
reader - The readerConvert a stream to a String, reading the data from the stream
stream - The stream