|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.codehaus.groovy.runtime.DefaultGroovyMethodsSupport org.codehaus.groovy.runtime.IOGroovyMethods
public class IOGroovyMethods extends DefaultGroovyMethodsSupport
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
.
Method Summary | |
---|---|
static void
|
eachByte(java.io.InputStream is, Closure closure)
|
static void
|
eachByte(java.io.InputStream is, int bufferLen, Closure closure)
|
static java.lang.Object
|
eachLine(java.io.InputStream stream, java.lang.String charset, Closure closure)
Iterates through this stream reading with the provided charset, passing each line to the given 1 or 2 arg closure. |
static java.lang.Object
|
eachLine(java.io.InputStream stream, java.lang.String charset, int firstLine, Closure closure)
Iterates through this stream reading with the provided charset, passing each line to the given 1 or 2 arg closure. |
static java.lang.Object
|
eachLine(java.io.InputStream stream, Closure closure)
Iterates through this stream, passing each line to the given 1 or 2 arg closure. |
static java.lang.Object
|
eachLine(java.io.InputStream stream, int firstLine, Closure closure)
Iterates through this stream, passing each line to the given 1 or 2 arg closure. |
static java.lang.Object
|
eachLine(java.io.Reader self, Closure closure)
Iterates through the given reader line by line. |
static java.lang.Object
|
eachLine(java.io.Reader self, int firstLine, Closure closure)
Iterates through the given reader line by line. |
static void
|
eachObject(java.io.ObjectInputStream ois, Closure closure)
Iterates through the given object stream object by object. |
static void
|
filterLine(java.io.Reader reader, java.io.Writer writer, Closure 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. |
static Writable
|
filterLine(java.io.Reader reader, Closure closure)
Filter the lines from this Reader, and return a Writable which can be used to stream the filtered lines to a destination. |
static Writable
|
filterLine(java.io.InputStream self, Closure predicate)
Filter lines from an input stream using a closure predicate. |
static Writable
|
filterLine(java.io.InputStream self, java.lang.String charset, Closure predicate)
Uses a closure to filter lines from this InputStream and pass them to the given writer. |
static void
|
filterLine(java.io.InputStream self, java.io.Writer writer, Closure predicate)
Uses a closure to filter lines from this InputStream and pass them to the given writer. |
static void
|
filterLine(java.io.InputStream self, java.io.Writer writer, java.lang.String charset, Closure predicate)
|
static byte[]
|
getBytes(java.io.InputStream is)
Read the content of this InputStream and return it as a byte[]. |
static java.lang.String
|
getText(java.io.InputStream is)
Read the content of this InputStream using specified charset and return it as a String. |
static java.lang.String
|
getText(java.io.InputStream is, java.lang.String charset)
Read the content of the Reader and return it as a String. |
static java.lang.String
|
getText(java.io.Reader reader)
Read the content of the BufferedReader and return it as a String. |
static java.lang.String
|
getText(java.io.BufferedReader reader)
|
static java.util.Iterator
|
iterator(java.io.Reader self)
|
static java.util.Iterator
|
iterator(java.io.InputStream self)
Standard iterator for a input stream which iterates through the stream content in a byte-based fashion. |
static java.util.Iterator
|
iterator(java.io.DataInputStream self)
|
static java.io.Writer
|
leftShift(java.io.Writer self, java.lang.Object value)
Overloads the leftShift operator for Writer to allow an object to be written using Groovy's default representation for the object. |
static java.lang.Appendable
|
leftShift(java.lang.Appendable self, java.lang.Object value)
Overloads the leftShift operator for Appendable to allow an object to be appended using Groovy's default representation for the object. |
static java.io.Writer
|
leftShift(java.io.OutputStream self, java.lang.Object value)
Overloads the leftShift operator to provide an append mechanism to add values to a stream. |
static void
|
leftShift(java.io.ObjectOutputStream self, java.lang.Object value)
Overloads the leftShift operator to add objects to an ObjectOutputStream. |
static java.io.OutputStream
|
leftShift(java.io.OutputStream self, java.io.InputStream in)
Pipe an InputStream into an OutputStream for efficient stream copying. |
static java.io.OutputStream
|
leftShift(java.io.OutputStream self, byte[] value)
Overloads the leftShift operator to provide an append mechanism to add bytes to a stream. |
static java.io.ObjectInputStream
|
newObjectInputStream(java.io.InputStream inputStream)
Create an object input stream for this input stream. |
static java.io.ObjectInputStream
|
newObjectInputStream(java.io.InputStream inputStream, java.lang.ClassLoader classLoader)
Create an object input stream for this input stream using the given class loader. |
static java.io.ObjectOutputStream
|
newObjectOutputStream(java.io.OutputStream outputStream)
Create an object output stream for this output stream. |
static java.io.PrintWriter
|
newPrintWriter(java.io.Writer writer)
|
static java.io.BufferedReader
|
newReader(java.io.InputStream self)
Creates a reader for this input stream. |
static java.io.BufferedReader
|
newReader(java.io.InputStream self, java.lang.String charset)
|
static java.lang.String
|
readLine(java.io.Reader self)
Read a single, whole line from the given Reader. |
static java.util.List
|
readLines(java.io.InputStream stream)
Reads the stream into a list, with one element for each line. |
static java.util.List
|
readLines(java.io.InputStream stream, java.lang.String charset)
Reads the reader into a list of Strings, with one entry for each line. |
static java.util.List
|
readLines(java.io.Reader reader)
Read the content of this InputStream and return it as a String. |
static void
|
setBytes(java.io.OutputStream os, byte[] bytes)
Write the text and append a newline (using the platform's line-ending). |
static java.lang.Object
|
splitEachLine(java.io.Reader self, java.lang.String regex, Closure closure)
Iterates through the given reader line by line, splitting each line using the given regex separator. |
static java.lang.Object
|
splitEachLine(java.io.Reader self, java.util.regex.Pattern pattern, Closure closure)
Iterates through the given reader line by line, splitting each line using the given regex separator Pattern. |
static java.lang.Object
|
splitEachLine(java.io.InputStream stream, java.lang.String regex, java.lang.String charset, Closure closure)
Iterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator. |
static java.lang.Object
|
splitEachLine(java.io.InputStream stream, java.util.regex.Pattern pattern, java.lang.String charset, Closure closure)
Iterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator Pattern. |
static java.lang.Object
|
splitEachLine(java.io.InputStream stream, java.lang.String regex, Closure closure)
Iterates through the given InputStream line by line, splitting each line using the given separator. |
static java.lang.Object
|
splitEachLine(java.io.InputStream stream, java.util.regex.Pattern pattern, Closure closure)
Iterates through the given InputStream line by line, splitting each line using the given separator Pattern. |
static void
|
transformChar(java.io.Reader self, java.io.Writer writer, Closure closure)
|
static void
|
transformLine(java.io.Reader reader, java.io.Writer writer, Closure closure)
Transforms the lines from a reader with a Closure and write them to a writer. |
static java.lang.Appendable
|
withFormatter(java.lang.Appendable self, Closure closure)
Invokes a Closure that uses a Formatter taking care of resource handling. |
static java.lang.Appendable
|
withFormatter(java.lang.Appendable self, java.util.Locale locale, Closure closure)
Invokes a Closure that uses a Formatter taking care of resource handling. |
static java.lang.Object
|
withObjectInputStream(java.io.InputStream inputStream, Closure closure)
Create a new ObjectInputStream for this file and pass it to the closure. |
static java.lang.Object
|
withObjectInputStream(java.io.InputStream inputStream, java.lang.ClassLoader classLoader, Closure closure)
Create a new ObjectInputStream for this file and pass it to the closure. |
static java.lang.Object
|
withObjectOutputStream(java.io.OutputStream outputStream, Closure closure)
Create a new ObjectOutputStream for this output stream and then pass it to the closure. |
static java.lang.Object
|
withPrintWriter(java.io.Writer writer, Closure closure)
|
static java.lang.Object
|
withReader(java.io.Reader reader, Closure closure)
|
static java.lang.Object
|
withReader(java.io.InputStream in, Closure closure)
Helper method to create a new Reader for a stream and then passes it into the closure. |
static java.lang.Object
|
withReader(java.io.InputStream in, java.lang.String charset, Closure closure)
Creates a writer from this stream, passing it to the given closure. |
static java.lang.Object
|
withStream(java.io.InputStream stream, Closure closure)
|
static java.lang.Object
|
withStream(java.io.OutputStream os, Closure closure)
|
static java.lang.Object
|
withWriter(java.io.Writer writer, Closure closure)
|
static java.lang.Object
|
withWriter(java.io.OutputStream stream, Closure closure)
|
static java.lang.Object
|
withWriter(java.io.OutputStream stream, java.lang.String charset, Closure closure)
|
static void
|
write(java.io.Writer self, Writable 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. |
static void
|
writeLine(java.io.BufferedWriter writer, java.lang.String line)
Creates an iterator which will traverse through the reader a line at a time. |
Methods inherited from class java.lang.Object | |
---|---|
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Method Detail |
---|
public static void eachByte(java.io.InputStream is, Closure closure)
public static void eachByte(java.io.InputStream is, int bufferLen, Closure closure)
public static java.lang.Object eachLine(java.io.InputStream stream, java.lang.String charset, Closure closure)
stream
- a streamcharset
- opens the stream with a specified charsetclosure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
public static java.lang.Object eachLine(java.io.InputStream stream, java.lang.String charset, int firstLine, Closure closure)
stream
- a streamcharset
- opens the stream with a specified charsetfirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure (arg 1 is line, optional arg 2 is line number)
public static java.lang.Object eachLine(java.io.InputStream stream, Closure closure)
stream
- a streamclosure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
public static java.lang.Object eachLine(java.io.InputStream stream, int firstLine, Closure closure)
stream
- a streamfirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure (arg 1 is line, optional arg 2 is line number)
public static java.lang.Object eachLine(java.io.Reader self, Closure closure)
self
- a Reader, closed after the method returnsclosure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
public static java.lang.Object eachLine(java.io.Reader self, int firstLine, Closure closure)
self
- a Reader, closed after the method returnsfirstLine
- the line number value used for the first line (default is 1, set to 0 to start counting from 0)closure
- a closure which will be passed each line (or for 2 arg closures the line and line count)
public static void eachObject(java.io.ObjectInputStream ois, Closure closure)
ois
- an ObjectInputStream, closed after the operationclosure
- a closure
public static void filterLine(java.io.Reader reader, java.io.Writer writer, Closure closure)
reader
- a reader, closed after the callwriter
- a writer, closed after the callclosure
- the closure which returns booleans
public static Writable filterLine(java.io.Reader reader, Closure closure)
true
if the line should be passed to the writer.
reader
- this readerclosure
- a closure used for filtering
public static Writable filterLine(java.io.InputStream self, Closure predicate)
true
if the line should be passed to the writer.
self
- an input streampredicate
- a closure which returns boolean and takes a line
public static Writable filterLine(java.io.InputStream self, java.lang.String charset, Closure predicate)
true
if the line should be passed to the
writer.self
- the InputStreamwriter
- a writer to write output topredicate
- a closure which returns true if a line should be accepted
public static void filterLine(java.io.InputStream self, java.io.Writer writer, Closure predicate)
true
if the line should be passed to the
writer.self
- the InputStreamwriter
- a writer to write output tocharset
- opens the stream with a specified charsetpredicate
- a closure which returns true if a line should be accepted
public static void filterLine(java.io.InputStream self, java.io.Writer writer, java.lang.String charset, Closure predicate)
public static byte[] getBytes(java.io.InputStream is)
is
- an input stream
public static java.lang.String getText(java.io.InputStream is)
is
- an input streamcharset
- opens the stream with a specified charset
public static java.lang.String getText(java.io.InputStream is, java.lang.String charset)
reader
- a Reader whose content we want to read
public static java.lang.String getText(java.io.Reader reader)
reader
- a BufferedReader whose content we want to read
public static java.lang.String getText(java.io.BufferedReader reader)
public static java.util.Iterator iterator(java.io.Reader self)
public static java.util.Iterator iterator(java.io.InputStream self)
self
- an InputStream object
public static java.util.Iterator iterator(java.io.DataInputStream self)
public static java.io.Writer leftShift(java.io.Writer self, java.lang.Object value)
self
- a Writervalue
- an Object whose default representation will be written to the Writer
public static java.lang.Appendable leftShift(java.lang.Appendable self, java.lang.Object value)
self
- an Appendablevalue
- an Object whose default representation will be appended to the Appendable
public static java.io.Writer leftShift(java.io.OutputStream self, java.lang.Object value)
self
- an OutputStreamvalue
- a value to append
public static void leftShift(java.io.ObjectOutputStream self, java.lang.Object value)
self
- an ObjectOutputStreamvalue
- an object to write to the stream
public static java.io.OutputStream leftShift(java.io.OutputStream self, java.io.InputStream in)
self
- stream on which to writein
- stream to read from
public static java.io.OutputStream leftShift(java.io.OutputStream self, byte[] value)
self
- an OutputStreamvalue
- a value to append
public static java.io.ObjectInputStream newObjectInputStream(java.io.InputStream inputStream)
inputStream
- an input stream
public static java.io.ObjectInputStream newObjectInputStream(java.io.InputStream inputStream, java.lang.ClassLoader classLoader)
inputStream
- an input streamclassLoader
- the class loader to use when loading the class
public static java.io.ObjectOutputStream newObjectOutputStream(java.io.OutputStream outputStream)
outputStream
- an output stream
public static java.io.PrintWriter newPrintWriter(java.io.Writer writer)
public static java.io.BufferedReader newReader(java.io.InputStream self)
self
- an input stream
public static java.io.BufferedReader newReader(java.io.InputStream self, java.lang.String charset)
public static java.lang.String readLine(java.io.Reader self)
self
- a Reader
public static java.util.List readLines(java.io.InputStream stream)
stream
- a streamcharset
- opens the stream with a specified charset
public static java.util.List readLines(java.io.InputStream stream, java.lang.String charset)
reader
- a Reader
public static java.util.List readLines(java.io.Reader reader)
is
- an input stream
public static void setBytes(java.io.OutputStream os, byte[] bytes)
writer
- a BufferedWriterline
- the line to write
public static java.lang.Object splitEachLine(java.io.Reader self, java.lang.String regex, Closure closure)
def s = 'The 3 quick\nbrown 4 fox' def result = '' new StringReader(s).splitEachLine(/\d/){ parts -> result += "${parts[0]}_${parts[1]}|" } assert result == 'The _ quick|brown _ fox|'
self
- a Reader, closed after the method returnsregex
- the delimiting regular expressionclosure
- a closure
public static java.lang.Object splitEachLine(java.io.Reader self, java.util.regex.Pattern pattern, Closure closure)
def s = 'The 3 quick\nbrown 4 fox' def result = '' new StringReader(s).splitEachLine(~/\d/){ parts -> result += "${parts[0]}_${parts[1]}|" } assert result == 'The _ quick|brown _ fox|'
self
- a Reader, closed after the method returnspattern
- the regular expression Pattern for the delimiterclosure
- a closure
public static java.lang.Object splitEachLine(java.io.InputStream stream, java.lang.String regex, java.lang.String charset, Closure closure)
stream
- an InputStreamregex
- the delimiting regular expressioncharset
- opens the stream with a specified charsetclosure
- a closure
public static java.lang.Object splitEachLine(java.io.InputStream stream, java.util.regex.Pattern pattern, java.lang.String charset, Closure closure)
stream
- an InputStreampattern
- the regular expression Pattern for the delimitercharset
- opens the stream with a specified charsetclosure
- a closure
public static java.lang.Object splitEachLine(java.io.InputStream stream, java.lang.String regex, Closure closure)
stream
- an InputStreamregex
- the delimiting regular expressionclosure
- a closure
public static java.lang.Object splitEachLine(java.io.InputStream stream, java.util.regex.Pattern pattern, Closure closure)
stream
- an InputStreampattern
- the regular expression Pattern for the delimiterclosure
- a closure
public static void transformChar(java.io.Reader self, java.io.Writer writer, Closure closure)
public static void transformLine(java.io.Reader reader, java.io.Writer writer, Closure closure)
reader
- Lines of text to be transformed. Reader is closed afterwards.writer
- Where transformed lines are written. Writer is closed afterwards.closure
- Single parameter closure that is called to transform each line of
text from the reader, before writing it to the writer.
public static java.lang.Appendable withFormatter(java.lang.Appendable self, Closure closure)
self
- an Appendableclosure
- a 1-arg Closure which will be called with a Formatter as its argument
public static java.lang.Appendable withFormatter(java.lang.Appendable self, java.util.Locale locale, Closure closure)
self
- an Appendablelocale
- a Locale used when creating the Formatterclosure
- a 1-arg Closure which will be called with a Formatter as its argument
public static java.lang.Object withObjectInputStream(java.io.InputStream inputStream, Closure closure)
inputStream
- an input streamclosure
- a closure
public static java.lang.Object withObjectInputStream(java.io.InputStream inputStream, java.lang.ClassLoader classLoader, Closure closure)
inputStream
- an input streamclassLoader
- the class loader to use when loading the classclosure
- a closure
public static java.lang.Object withObjectOutputStream(java.io.OutputStream outputStream, Closure closure)
outputStream
- am output streamclosure
- a closure
public static java.lang.Object withPrintWriter(java.io.Writer writer, Closure closure)
public static java.lang.Object withReader(java.io.Reader reader, Closure closure)
public static java.lang.Object withReader(java.io.InputStream in, Closure closure)
in
- a streamcharset
- the charset used to decode the streamclosure
- the closure to invoke with the reader
public static java.lang.Object withReader(java.io.InputStream in, java.lang.String charset, Closure closure)
stream
- the stream which is used and then closedclosure
- the closure that the writer is passed into
public static java.lang.Object withStream(java.io.InputStream stream, Closure closure)
public static java.lang.Object withStream(java.io.OutputStream os, Closure closure)
public static java.lang.Object withWriter(java.io.Writer writer, Closure closure)
public static java.lang.Object withWriter(java.io.OutputStream stream, Closure closure)
public static java.lang.Object withWriter(java.io.OutputStream stream, java.lang.String charset, Closure closure)
public static void write(java.io.Writer self, Writable writable)
self
- a Writerwritable
- an object implementing the Writable interface
public static void writeLine(java.io.BufferedWriter writer, java.lang.String line)
self
- a Reader object
Groovy Documentation