Groovy Documentation

org.linkedin.groovy.util.io
[Groovy] Class GroovyIOUtils

java.lang.Object
  org.linkedin.util.io.IOUtils
      org.linkedin.groovy.util.io.GroovyIOUtils

class GroovyIOUtils
extends IOUtils

IO related utilities

Authors:
ypujante@linkedin.com


Constructor Summary
protected GroovyIOUtils()

 
Method Summary
static java.lang.String cat(java.lang.Object location)

Returns the content of the location as a String

static Resource eachChildRecurse(Resource resource, groovy.lang.Closure closure)

The closure will be called for every child (recursively) of the provided resource

static void eachLine(java.io.Reader reader, groovy.lang.Closure closure)

The difference between reader.eachLine() and this method is that as soon as the closure returns false then the iteration is stopped.

static void eachLine(java.net.URL url, groovy.lang.Closure closure)

Convenient call which calls {@link #eachLine(Reader, Closure).

static java.io.File fetchContent(java.lang.Object location, java.io.File destination)

Fetches the file pointed to by the location.

static java.lang.Object findAll(Resource resource, groovy.lang.Closure closure)

Every child resource of this resource (recursively) is being passed to the closure.

static boolean isChild(java.io.File parent, java.io.File child)

true if child is really a child of parent or in other words if child is located in a subpath of parent (handle canonical path properly)

static java.io.File makeRelativeToParent(java.io.File parent, java.io.File child)

Ex: parent=/a/b/c child='/a/b/c/d/e'... would return d/e

static java.io.File mkdirs(java.io.File dir)

Creates the directory and parents of the provided directory.

static java.lang.Object safeOverwrite(java.io.File toFile, groovy.lang.Closure closure)

This convenient call takes a file you want to (over)write to and a closure.

static java.lang.Object safeOverwrite(java.io.File toFile, groovy.lang.Closure tempFileFactory, groovy.lang.Closure closure)

This variant takes a tempFileFactory if you want to control precisely where and how the temporary file is created (note that if the tempoary file is not created in the same folder as toFile then the rename operation may actually be a copy/delete instead of just a rename thus defeating the purpose of this method!)

static java.io.File toFile(java.lang.Object s)

returns a file... handles File, URI, URL, string, null

static java.io.File toFile(java.lang.Object s, java.io.File tempFolder)

returns a file... handles File, URI, URL, string, null

static java.util.Map toFileWithTempStatus(java.lang.Object s, java.io.File tempFolder)

handles File, URI, URL, string, null returns a map with file and tempStatus (boolean)

static java.lang.Object withFile(java.lang.Object s, groovy.lang.Closure closure)

Will convert s into a File object (if possible) and call the closure with it.

 

Constructor Detail

GroovyIOUtils

protected GroovyIOUtils()


 
Method Detail

cat

static java.lang.String cat(java.lang.Object location)
Returns the content of the location as a String
throws:
IOException if the file is not reachable


eachChildRecurse

static Resource eachChildRecurse(Resource resource, groovy.lang.Closure closure)
The closure will be called for every child (recursively) of the provided resource
Returns:
the resource passed it


eachLine

static void eachLine(java.io.Reader reader, groovy.lang.Closure closure)
The difference between reader.eachLine() and this method is that as soon as the closure returns false then the iteration is stopped. There is no way to stop the iteration with reader.eachLine().


eachLine

static void eachLine(java.net.URL url, groovy.lang.Closure closure)
Convenient call which calls {
link:
#eachLine(Reader, Closure).


fetchContent

static java.io.File fetchContent(java.lang.Object location, java.io.File destination)
Fetches the file pointed to by the location. The location can be File, a String or URI and must contain a scheme. Example of locations: http://locahost:8080/file.txt', file:/tmp/file.txt, ivy:/org.linkedin/util-core/1.0.0.
Parameters:
location - where is the content you want to retrieve locally
destination - where to store the content locally
Returns:
destination


findAll

static java.lang.Object findAll(Resource resource, groovy.lang.Closure closure)
Every child resource of this resource (recursively) is being passed to the closure. If the closure returns true then it will be part of the result.


isChild

static boolean isChild(java.io.File parent, java.io.File child)
true if child is really a child of parent or in other words if child is located in a subpath of parent (handle canonical path properly)


makeRelativeToParent

static java.io.File makeRelativeToParent(java.io.File parent, java.io.File child)
Ex: parent=/a/b/c child='/a/b/c/d/e'... would return d/e
Returns:
a file object which contains the relative part from the child to the parent. null if child is not a child of parent! If child is relative the return child


mkdirs

static java.io.File mkdirs(java.io.File dir)
Creates the directory and parents of the provided directory. Returns dir.


safeOverwrite

static java.lang.Object safeOverwrite(java.io.File toFile, groovy.lang.Closure closure)
This convenient call takes a file you want to (over)write to and a closure. The closure is called back with another file in the same folder that you can write to and then rename the file to the one you wanted. The fact that it is in the same folder ensures that the rename should be quick and not really require any copy thus is less likely to fail. If the rename fails it throws an exception, thus ensuring that if there was an original file it won't be in a partial state. Note that this method automatically creates the parent folders if they don't exist.
Parameters:
toFile - the final file where you want your output to be
closure - takes a File as a parameter that you should use
Returns:
whatever the closure returns


safeOverwrite

static java.lang.Object safeOverwrite(java.io.File toFile, groovy.lang.Closure tempFileFactory, groovy.lang.Closure closure)
This variant takes a tempFileFactory if you want to control precisely where and how the temporary file is created (note that if the tempoary file is not created in the same folder as toFile then the rename operation may actually be a copy/delete instead of just a rename thus defeating the purpose of this method!)
Parameters:
toFile - the final file where you want your output to be
tempFileFactory - closure which takes toFile as an argument and returns a temporary file
closure - takes a File as a parameter that you should use
Returns:
whatever the closure returns


toFile

static java.io.File toFile(java.lang.Object s)
returns a file... handles File, URI, URL, string, null


toFile

static java.io.File toFile(java.lang.Object s, java.io.File tempFolder)
returns a file... handles File, URI, URL, string, null
Parameters:
tempFolder - if the file is not local, where to store it temporarilly (null means standard java vm temp folder)


toFileWithTempStatus

static java.util.Map toFileWithTempStatus(java.lang.Object s, java.io.File tempFolder)
handles File, URI, URL, string, null returns a map with file and tempStatus (boolean)
Parameters:
tempFolder - if the file is not local, where to store it temporarilly (null means standard java vm temp folder)


withFile

static java.lang.Object withFile(java.lang.Object s, groovy.lang.Closure closure)
Will convert s into a File object (if possible) and call the closure with it. Once the closure is over, if the file was not local (and as a result was copied locally for the duration of the closure) it will be deleted. As a result the closure should *not* store the File object around for later use or assume that the underlying file will still exist at a later point. Use toFile(Object) instead if this is what you want to do.
Parameters:
s - handles File, URI, URL, string, null
closure - will be called back with a File object (or null)
Returns:
whatever the closure returns


 

Groovy Documentation