Top Description Fields Constructors Methods
org.codehaus.groovy.runtime

public Class IOGroovyMethods

extends DefaultGroovyMethodsSupport
Class Inheritance
Imports
groovy.io.GroovyPrintWriter, groovy.lang.Closure, .StringWriterIOException, .Writable, groovy.transform.stc.ClosureParams, .FirstParam, .FromString, .PickFirstResolver, .SimpleType, org.apache.groovy.io.StringBuilderWriter, org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper, java.io.BufferedReader, .BufferedWriter, .ByteArrayOutputStream, .Closeable, .DataInputStream, .EOFException, .IOException, .InputStream, .InputStreamReader, .ObjectInputStream, .ObjectOutputStream, .ObjectStreamClass, .OutputStream, .OutputStreamWriter, .PrintWriter, .Reader, .UnsupportedEncodingException, .Writer, java.util.Arrays, .Formatter, .Iterator, .List, .Locale, java.util.logging.Logger, java.util.regex.Pattern

This class defines new groovy methods for Files, URLs, URIs which appear on normal JDK classes inside the Groovy environment. Static methods are used with the first parameter being the destination class, i.e. public static long size(File self) provides a size() method for File.

Note

While this class contains many 'public' static methods, it is primarily regarded as an internal class (its internal package name suggests this also). We value backwards compatibility of these methods when used within Groovy but value less backwards compatibility at the Java method call level. I.e. future versions of Groovy may remove or move a method call in this file but would normally aim to keep the method available from within Groovy.

Field Summary

Modifier and TypeField and Description
private static int
private static final int
private static int
private static int
private static final Logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
callWithFormatter(Closure<V> closure, Formatter formatter)

public static void
eachByte(InputStream
stream to iterate over, closed after the method call
is
,
Closure<V>
closure to apply to each byte
closure
)

Traverse through each byte of the specified stream.

public static void
eachByte(InputStream
stream to iterate over, closed after the method call.
is
,
int
the length of the buffer to use.
bufferLen
,
Closure<V>
a 2 parameter closure which is passed the byte[] and a number of bytes successfully read.
closure
)

Traverse through each the specified stream reading bytes into a buffer and calling the 2 parameter closure with this buffer and the number of bytes.

public static <T> T

Returns:

the last value returned by the closure
eachLine
(InputStream
a stream
stream
,
String
opens the stream with a specified charset
charset
,
Closure<T>
a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
closure
)

Iterates through this stream reading with the provided charset, passing each line to the given 1 or 2 arg closure.

public static <T> T

Returns:

the last value returned by the closure
eachLine
(InputStream
a stream
stream
,
String
opens the stream with a specified charset
charset
,
int
the line number value used for the first line (default is 1, set to 0 to start counting from 0)
firstLine
,
Closure<T>
a closure (arg 1 is line, optional arg 2 is line number)
closure
)

Iterates through this stream reading with the provided charset, passing each line to the given 1 or 2 arg closure.

public static <T> T

Returns:

the last value returned by the closure
eachLine
(InputStream
a stream
stream
,
Closure<T>
a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
closure
)

Iterates through this stream, passing each line to the given 1 or 2 arg closure.

public static <T> T

Returns:

the last value returned by the closure
eachLine
(InputStream
a stream
stream
,
int
the line number value used for the first line (default is 1, set to 0 to start counting from 0)
firstLine
,
Closure<T>
a closure (arg 1 is line, optional arg 2 is line number)
closure
)

Iterates through this stream, passing each line to the given 1 or 2 arg closure.

public static <T> T

Returns:

the last value returned by the closure
eachLine
(Reader
a Reader, closed after the method returns
self
,
Closure<T>
a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
closure
)

Iterates through the given reader line by line.

public static <T> T

Returns:

the last value returned by the closure
eachLine
(Reader
a Reader, closed after the method returns
self
,
int
the line number value used for the first line (default is 1, set to 0 to start counting from 0)
firstLine
,
Closure<T>
a closure which will be passed each line (or for 2 arg closures the line and line count)
closure
)

Iterates through the given reader line by line.

public static void
eachObject(ObjectInputStream
an ObjectInputStream, closed after the operation
ois
,
Closure<V>
a closure
closure
)

Iterates through the given object stream object by object.

public static void
filterLine(Reader
a reader, closed after the call
reader
,
Writer
a writer, closed after the call
writer
,
Closure<V>
the closure which returns booleans
closure
)

Filter the lines from a reader and write them on the writer, according to a closure which returns true if the line should be included.

public static Writable

Returns:

a Writable which will use the closure to filter each line from the reader when the Writable#writeTo(Writer) is called.
filterLine
(Reader
this reader
reader
,
final Closure<V>
a closure used for filtering
closure
)

Filter the lines from this Reader, and return a Writable which can be used to stream the filtered lines to a destination.

public static Writable

Returns:

a writable which writes out the filtered lines
filterLine
(InputStream
an input stream
self
,
Closure<V>
a closure which returns boolean and takes a line
predicate
)

Filter lines from an input stream using a closure predicate.

public static Writable

Returns:

a writable which writes out the filtered lines
filterLine
(InputStream
an input stream
self
,
String
opens the stream with a specified charset
charset
,
Closure<V>
a closure which returns boolean and takes a line
predicate
)

Filter lines from an input stream using a closure predicate.

public static void
filterLine(InputStream
the InputStream
self
,
Writer
a writer to write output to
writer
,
Closure<V>
a closure which returns true if a line should be accepted
predicate
)

Uses a closure to filter lines from this InputStream and pass them to the given writer.

public static void
filterLine(InputStream
the InputStream
self
,
Writer
a writer to write output to
writer
,
String
opens the stream with a specified charset
charset
,
Closure<V>
a closure which returns true if a line should be accepted
predicate
)

Uses a closure to filter lines from this InputStream and pass them to the given writer.

public static byte[]

Returns:

the byte[] from that InputStream
getBytes
(InputStream
an input stream
is
)

Read the content of this InputStream and return it as a byte[].

public static String

Returns:

the text from that URL
getText
(InputStream
an input stream
is
)

Read the content of this InputStream and return it as a String.

public static String

Returns:

the text from that URL
getText
(InputStream
an input stream
is
,
String
opens the stream with a specified charset
charset
)

Read the content of this InputStream using specified charset and return it as a String.

public static String

Returns:

a String containing the content of the buffered reader
getText
(Reader
a Reader whose content we want to read
reader
)

Read the content of the Reader and return it as a String.

public static String

Returns:

a String containing the content of the buffered reader
getText
(BufferedReader
a BufferedReader whose content we want to read
reader
)

Read the content of the BufferedReader and return it as a String.

private static boolean
public static Iterator<String>

Returns:

an Iterator for the Reader
iterator
(Reader
a Reader object
self
)

Creates an iterator which will traverse through the reader a line at a time.

public static Iterator<Byte>

Returns:

an Iterator for the InputStream
iterator
(InputStream
an InputStream object
self
)

Standard iterator for a input stream which iterates through the stream content in a byte-based fashion.

public static Iterator<Byte>

Returns:

an Iterator for the DataInputStream
iterator
(final DataInputStream
a DataInputStream object
self
)

Standard iterator for a data input stream which iterates through the stream content a Byte at a time.

public static Writer

Returns:

the writer on which this operation was invoked
leftShift
(Writer
a Writer
self
,
Object
an Object whose default representation will be written to the Writer
value
)

Overloads the leftShift operator for Writer to allow an object to be written using Groovy's default representation for the object.

public static Appendable

Returns:

the Appendable on which this operation was invoked
leftShift
(Appendable
an Appendable
self
,
Object
an Object whose default representation will be appended to the Appendable
value
)

Overloads the leftShift operator for Appendable to allow an object to be appended using Groovy's default representation for the object.

public static Writer

Returns:

a Writer
leftShift
(OutputStream
an OutputStream
self
,
Object
a value to append
value
)

Overloads the leftShift operator to provide an append mechanism to add values to a stream.

public static void
leftShift(ObjectOutputStream
an ObjectOutputStream
self
,
Object
an object to write to the stream
value
)

Overloads the leftShift operator to add objects to an ObjectOutputStream.

public static OutputStream

Returns:

the outputstream itself
leftShift
(OutputStream
stream on which to write
self
,
InputStream
stream to read from
in
)

Pipe an InputStream into an OutputStream for efficient stream copying.

public static OutputStream

Returns:

an OutputStream
leftShift
(OutputStream
an OutputStream
self
,
byte[]
a value to append
value
)

Overloads the leftShift operator to provide an append mechanism to add bytes to a stream.

private static int
lineSeparatorIndex(char[] array, int length)

public static ObjectInputStream

Returns:

an object input stream
newObjectInputStream
(InputStream
an input stream
inputStream
)

Create an object input stream for this input stream.

public static ObjectInputStream

Returns:

an object input stream
newObjectInputStream
(InputStream
an input stream
inputStream
,
final ClassLoader
the class loader to use when loading the class
classLoader
)

Create an object input stream for this input stream using the given class loader.

public static ObjectOutputStream

Returns:

an object output stream
newObjectOutputStream
(OutputStream
an output stream
outputStream
)

Create an object output stream for this output stream.

public static PrintWriter

Returns:

a PrintWriter
newPrintWriter
(Writer
a Writer
writer
)

Create a new PrintWriter for this Writer.

public static PrintWriter

Returns:

a PrintWriter
newPrintWriter
(OutputStream
an OutputStream
stream
)

Create a new PrintWriter for this OutputStream.

public static BufferedReader

Returns:

a reader
newReader
(InputStream
an input stream
self
)

Creates a reader for this input stream.

public static BufferedReader

Returns:

a reader
newReader
(InputStream
an input stream
self
,
String
the charset for this input stream
charset
)

Creates a reader for this input stream, using the specified charset as the encoding.

public static Writer

Returns:

the newly created Writer
newWriter
(OutputStream
the stream which is used and then closed
stream
)

Creates a writer for this stream.

public static Writer

Returns:

the newly created Writer
newWriter
(OutputStream
the stream which is used and then closed
stream
,
String
the charset used
charset
)

Creates a writer for this stream using the given charset.

public static String

Returns:

a line
readLine
(Reader
a Reader
self
)

Read a single, whole line from the given Reader.

private static String
private static String
public static List<String>

Returns:

a List of lines
readLines
(InputStream
a stream
stream
)

Reads the stream into a list, with one element for each line.

public static List<String>

Returns:

a List of lines
readLines
(InputStream
a stream
stream
,
String
opens the stream with a specified charset
charset
)

Reads the stream into a list, with one element for each line.

public static List<String>

Returns:

a List of lines
readLines
(Reader
a Reader
reader
)

Reads the reader into a list of Strings, with one entry for each line.

public static void
setBytes(OutputStream
an output stream
os
,
byte[]
the byte[] to write to the output stream
bytes
)

Write the byte[] to the output stream.

public static <T> T

Returns:

the last value returned by the closure
splitEachLine
(Reader
a Reader, closed after the method returns
self
,
String
the delimiting regular expression
regex
,
Closure<T>
a closure
closure
)

Iterates through the given reader line by line, splitting each line using the given regex separator.

public static <T> T

Returns:

the last value returned by the closure
splitEachLine
(Reader
a Reader, closed after the method returns
self
,
Pattern
the regular expression Pattern for the delimiter
pattern
,
Closure<T>
a closure
closure
)

Iterates through the given reader line by line, splitting each line using the given regex separator Pattern.

public static <T> T

Returns:

the last value returned by the closure
splitEachLine
(InputStream
an InputStream
stream
,
String
the delimiting regular expression
regex
,
String
opens the stream with a specified charset
charset
,
Closure<T>
a closure
closure
)

Iterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator.

public static <T> T

Returns:

the last value returned by the closure
splitEachLine
(InputStream
an InputStream
stream
,
Pattern
the regular expression Pattern for the delimiter
pattern
,
String
opens the stream with a specified charset
charset
,
Closure<T>
a closure
closure
)

Iterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator Pattern.

public static <T> T

Returns:

the last value returned by the closure
splitEachLine
(InputStream
an InputStream
stream
,
String
the delimiting regular expression
regex
,
Closure<T>
a closure
closure
)

Iterates through the given InputStream line by line, splitting each line using the given separator.

public static <T> T

Returns:

the last value returned by the closure
splitEachLine
(InputStream
an InputStream
stream
,
Pattern
the regular expression Pattern for the delimiter
pattern
,
Closure<T>
a closure
closure
)

Iterates through the given InputStream line by line, splitting each line using the given separator Pattern.

public static void
transformChar(Reader
a Reader object
self
,
Writer
a Writer to receive the transformed characters
writer
,
Closure<V>
a closure that performs the required transformation
closure
)

Transforms each character from this reader by passing it to the given closure.

public static void
transformLine(Reader
Lines of text to be transformed. Reader is closed afterwards.
reader
,
Writer
Where transformed lines are written. Writer is closed afterwards.
writer
,
Closure<V>
Single parameter closure that is called to transform each line of text from the reader, before writing it to the writer.
closure
)

Transforms the lines from a reader with a Closure and write them to a writer.

public static <T, U extends Closeable> T

Returns:

the value returned by the closure
withCloseable
(U
the Closeable
self
,
Closure<T>
the closure taking the Closeable as parameter
action
)

Allows this closeable to be used within the closure, ensuring that it is closed once the closure has been executed and before this method returns.

public static <T, U extends AutoCloseable> T

Returns:

the value returned by the closure
withCloseable
(U
the AutoCloseable
self
,
Closure<T>
the closure taking the AutoCloseable as parameter
action
)

Allows this AutoCloseable to be used within the closure, ensuring that it is closed once the closure has been executed and before this method returns.

public static Appendable

Returns:

the Appendable on which this operation was invoked
withFormatter
(Appendable
an Appendable
self
,
Closure<V>
a 1-arg Closure which will be called with a Formatter as its argument
closure
)

Invokes a Closure that uses a Formatter taking care of resource handling.

public static Appendable

Returns:

the Appendable on which this operation was invoked
withFormatter
(Appendable
an Appendable
self
,
Locale
a Locale used when creating the Formatter
locale
,
Closure<V>
a 1-arg Closure which will be called with a Formatter as its argument
closure
)

Invokes a Closure that uses a Formatter taking care of resource handling.

public static <T> T

Returns:

the value returned by the closure
withObjectInputStream
(InputStream
an input stream
inputStream
,
Closure<T>
a closure
closure
)

Create a new ObjectInputStream for this file and pass it to the closure.

public static <T> T

Returns:

the value returned by the closure
withObjectInputStream
(InputStream
an input stream
inputStream
,
ClassLoader
the class loader to use when loading the class
classLoader
,
Closure<T>
a closure
closure
)

Create a new ObjectInputStream for this file and pass it to the closure.

public static <T> T

Returns:

the value returned by the closure
withObjectOutputStream
(OutputStream
am output stream
outputStream
,
Closure<T>
a closure
closure
)

Create a new ObjectOutputStream for this output stream and then pass it to the closure.

public static <T> T

Returns:

the value returned by the closure
withPrintWriter
(Writer
a writer
writer
,
Closure<T>
the closure to invoke with the PrintWriter
closure
)

Create a new PrintWriter for this Writer.

public static <T> T

Returns:

the value returned by the closure
withPrintWriter
(OutputStream
an OutputStream
stream
,
Closure<T>
the closure to invoke with the PrintWriter
closure
)

Create a new PrintWriter for this OutputStream.

public static <T> T

Returns:

the value returned by the closure
withReader
(Reader
the reader which is used and then closed
reader
,
Closure<T>
the closure that the writer is passed into
closure
)

Allows this reader to be used within the closure, ensuring that it is closed before this method returns.

public static <T> T

Returns:

the value returned by the closure
withReader
(InputStream
a stream
in
,
Closure<T>
the closure to invoke with the InputStream
closure
)

Helper method to create a new Reader for a stream and then passes it into the closure.

public static <T> T

Returns:

the value returned by the closure
withReader
(InputStream
a stream
in
,
String
the charset used to decode the stream
charset
,
Closure<T>
the closure to invoke with the reader
closure
)

Helper method to create a new Reader for a stream and then passes it into the closure.

public static <T, U extends InputStream> T

Returns:

the value returned by the closure
withStream
(U
the stream which is used and then closed
stream
,
Closure<T>
the closure that the stream is passed into
closure
)

Allows this input stream to be used within the closure, ensuring that it is flushed and closed before this method returns.

public static <T, U extends OutputStream> T

Returns:

the value returned by the closure
withStream
(U
the stream which is used and then closed
os
,
Closure<T>
the closure that the stream is passed into
closure
)

Passes this OutputStream to the closure, ensuring that the stream is closed after the closure returns, regardless of errors.

public static <T> T

Returns:

the value returned by the closure
withWriter
(Writer
the writer which is used and then closed
writer
,
Closure<T>
the closure that the writer is passed into
closure
)

Allows this writer to be used within the closure, ensuring that it is flushed and closed before this method returns.

public static <T> T

Returns:

the value returned by the closure
withWriter
(OutputStream
the stream which is used and then closed
stream
,
Closure<T>
the closure that the writer is passed into
closure
)

Creates a writer from this stream, passing it to the given closure.

public static <T> T

Returns:

the value returned by the closure
withWriter
(OutputStream
the stream which is used and then closed
stream
,
String
the charset used
charset
,
Closure<T>
the closure that the writer is passed into
closure
)

Creates a writer from this stream, passing it to the given closure.

public static void
write(Writer
a Writer
self
,
Writable
an object implementing the Writable interface
writable
)

A helper method so that dynamic dispatch of the writer.write(object) method will always use the more efficient Writable.writeTo(writer) mechanism if the object implements the Writable interface.

public static void
writeLine(BufferedWriter
a BufferedWriter
writer
,
String
the line to write
line
)

Write the text and append a newline (using the platform's line-ending).

Inherited from org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport:
cloneSimilarCollectioncloneSimilarMapcloseQuietlycloseWithWarningcreateSimilarArraycreateSimilarCollectioncreateSimilarCollectioncreateSimilarCollectioncreateSimilarListcreateSimilarMapcreateSimilarOrDefaultCollectioncreateSimilarQueuecreateSimilarSetnormaliseIndexsameTypesubListBorderssubListBorderstryClosewriteUTF16BomIfRequiredwriteUTF16BomIfRequiredwriteUTF16BomIfRequiredwriteUTF16BomIfRequired