|
Groovy 2.2.0 | |||||||
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
.
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.
Method Summary | |
---|---|
static void
|
eachByte(InputStream is, Closure closure)
|
static void
|
eachByte(InputStream is, int bufferLen, Closure closure)
|
static Object
|
eachLine(InputStream stream, 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 Object
|
eachLine(InputStream stream, 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 Object
|
eachLine(InputStream stream, Closure closure)
Iterates through this stream, passing each line to the given 1 or 2 arg closure. |
static Object
|
eachLine(InputStream stream, int firstLine, Closure closure)
Iterates through this stream, passing each line to the given 1 or 2 arg closure. |
static Object
|
eachLine(Reader self, Closure closure)
Iterates through the given reader line by line. |
static Object
|
eachLine(Reader self, int firstLine, Closure closure)
Iterates through the given reader line by line. |
static void
|
eachObject(ObjectInputStream ois, Closure closure)
Iterates through the given object stream object by object. |
static void
|
filterLine(Reader reader, 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(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(InputStream self, Closure predicate)
Filter lines from an input stream using a closure predicate. |
static Writable
|
filterLine(InputStream self, String charset, Closure predicate)
Uses a closure to filter lines from this InputStream and pass them to the given writer. |
static void
|
filterLine(InputStream self, Writer writer, Closure predicate)
Uses a closure to filter lines from this InputStream and pass them to the given writer. |
static void
|
filterLine(InputStream self, Writer writer, String charset, Closure predicate)
|
static byte[]
|
getBytes(InputStream is)
Read the content of this InputStream and return it as a byte[]. |
static String
|
getText(InputStream is)
Read the content of this InputStream using specified charset and return it as a String. |
static String
|
getText(InputStream is, String charset)
Read the content of the Reader and return it as a String. |
static String
|
getText(Reader reader)
Read the content of the BufferedReader and return it as a String. |
static String
|
getText(BufferedReader reader)
|
static Iterator
|
iterator(Reader self)
|
static Iterator
|
iterator(InputStream self)
Standard iterator for a input stream which iterates through the stream content in a byte-based fashion. |
static Iterator
|
iterator(DataInputStream self)
|
static Writer
|
leftShift(Writer self, Object value)
Overloads the leftShift operator for Writer to allow an object to be written using Groovy's default representation for the object. |
static Appendable
|
leftShift(Appendable self, Object value)
Overloads the leftShift operator for Appendable to allow an object to be appended using Groovy's default representation for the object. |
static Writer
|
leftShift(OutputStream self, Object value)
Overloads the leftShift operator to provide an append mechanism to add values to a stream. |
static void
|
leftShift(ObjectOutputStream self, Object value)
Overloads the leftShift operator to add objects to an ObjectOutputStream. |
static OutputStream
|
leftShift(OutputStream self, InputStream in)
Pipe an InputStream into an OutputStream for efficient stream copying. |
static OutputStream
|
leftShift(OutputStream self, byte[] value)
Overloads the leftShift operator to provide an append mechanism to add bytes to a stream. |
static ObjectInputStream
|
newObjectInputStream(InputStream inputStream)
Create an object input stream for this input stream. |
static ObjectInputStream
|
newObjectInputStream(InputStream inputStream, ClassLoader classLoader)
Create an object input stream for this input stream using the given class loader. |
static ObjectOutputStream
|
newObjectOutputStream(OutputStream outputStream)
Create an object output stream for this output stream. |
static PrintWriter
|
newPrintWriter(Writer writer)
|
static PrintWriter
|
newPrintWriter(OutputStream stream)
|
static BufferedReader
|
newReader(InputStream self)
Creates a reader for this input stream. |
static BufferedReader
|
newReader(InputStream self, String charset)
|
static Writer
|
newWriter(OutputStream stream)
|
static Writer
|
newWriter(OutputStream stream, String charset)
|
static String
|
readLine(Reader self)
Read a single, whole line from the given Reader. |
static List
|
readLines(InputStream stream)
Reads the stream into a list, with one element for each line. |
static List
|
readLines(InputStream stream, String charset)
Reads the reader into a list of Strings, with one entry for each line. |
static List
|
readLines(Reader reader)
Read the content of this InputStream and return it as a String. |
static void
|
setBytes(OutputStream os, byte[] bytes)
Write the text and append a newline (using the platform's line-ending). |
static Object
|
splitEachLine(Reader self, String regex, Closure closure)
Iterates through the given reader line by line, splitting each line using the given regex separator. |
static Object
|
splitEachLine(Reader self, Pattern pattern, Closure closure)
Iterates through the given reader line by line, splitting each line using the given regex separator Pattern. |
static Object
|
splitEachLine(InputStream stream, String regex, String charset, Closure closure)
Iterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator. |
static Object
|
splitEachLine(InputStream stream, Pattern pattern, 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 Object
|
splitEachLine(InputStream stream, String regex, Closure closure)
Iterates through the given InputStream line by line, splitting each line using the given separator. |
static Object
|
splitEachLine(InputStream stream, Pattern pattern, Closure closure)
Iterates through the given InputStream line by line, splitting each line using the given separator Pattern. |
static void
|
transformChar(Reader self, Writer writer, Closure closure)
|
static void
|
transformLine(Reader reader, Writer writer, Closure closure)
Transforms the lines from a reader with a Closure and write them to a writer. |
static Appendable
|
withFormatter(Appendable self, Closure closure)
Invokes a Closure that uses a Formatter taking care of resource handling. |
static Appendable
|
withFormatter(Appendable self, Locale locale, Closure closure)
Invokes a Closure that uses a Formatter taking care of resource handling. |
static Object
|
withObjectInputStream(InputStream inputStream, Closure closure)
Create a new ObjectInputStream for this file and pass it to the closure. |
static Object
|
withObjectInputStream(InputStream inputStream, ClassLoader classLoader, Closure closure)
Create a new ObjectInputStream for this file and pass it to the closure. |
static Object
|
withObjectOutputStream(OutputStream outputStream, Closure closure)
Create a new ObjectOutputStream for this output stream and then pass it to the closure. |
static Object
|
withPrintWriter(Writer writer, Closure closure)
|
static Object
|
withPrintWriter(OutputStream stream, Closure closure)
|
static Object
|
withReader(Reader reader, Closure closure)
|
static Object
|
withReader(InputStream in, Closure closure)
Helper method to create a new Reader for a stream and then passes it into the closure. |
static Object
|
withReader(InputStream in, String charset, Closure closure)
Creates a writer from this stream, passing it to the given closure. |
static Object
|
withStream(InputStream stream, Closure closure)
|
static Object
|
withStream(OutputStream os, Closure closure)
|
static Object
|
withWriter(Writer writer, Closure closure)
|
static Object
|
withWriter(OutputStream stream, Closure closure)
|
static Object
|
withWriter(OutputStream stream, String charset, Closure closure)
|
static void
|
write(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(BufferedWriter writer, String line)
Creates an iterator which will traverse through the reader a line at a time. |
Methods inherited from class Object | |
---|---|
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll |
Method Detail |
---|
public static void eachByte(InputStream is, Closure closure)
public static void eachByte(InputStream is, int bufferLen, Closure closure)
public static Object eachLine(InputStream stream, 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 Object eachLine(InputStream stream, 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 Object eachLine(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 Object eachLine(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 Object eachLine(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 Object eachLine(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(ObjectInputStream ois, Closure closure)
ois
- an ObjectInputStream, closed after the operationclosure
- a closure
public static void filterLine(Reader reader, 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(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(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(InputStream self, 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(InputStream self, 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(InputStream self, Writer writer, String charset, Closure predicate)
public static byte[] getBytes(InputStream is)
is
- an input stream
public static String getText(InputStream is)
is
- an input streamcharset
- opens the stream with a specified charset
public static String getText(InputStream is, String charset)
reader
- a Reader whose content we want to read
public static String getText(Reader reader)
reader
- a BufferedReader whose content we want to read
public static String getText(BufferedReader reader)
public static Iterator iterator(Reader self)
public static Iterator iterator(InputStream self)
self
- an InputStream object
public static Iterator iterator(DataInputStream self)
public static Writer leftShift(Writer self, Object value)
self
- a Writervalue
- an Object whose default representation will be written to the Writer
public static Appendable leftShift(Appendable self, Object value)
self
- an Appendablevalue
- an Object whose default representation will be appended to the Appendable
public static Writer leftShift(OutputStream self, Object value)
self
- an OutputStreamvalue
- a value to append
public static void leftShift(ObjectOutputStream self, Object value)
self
- an ObjectOutputStreamvalue
- an object to write to the stream
public static OutputStream leftShift(OutputStream self, InputStream in)
self
- stream on which to writein
- stream to read from
public static OutputStream leftShift(OutputStream self, byte[] value)
self
- an OutputStreamvalue
- a value to append
public static ObjectInputStream newObjectInputStream(InputStream inputStream)
inputStream
- an input stream
public static ObjectInputStream newObjectInputStream(InputStream inputStream, ClassLoader classLoader)
inputStream
- an input streamclassLoader
- the class loader to use when loading the class
public static ObjectOutputStream newObjectOutputStream(OutputStream outputStream)
outputStream
- an output stream
public static PrintWriter newPrintWriter(Writer writer)
public static PrintWriter newPrintWriter(OutputStream stream)
public static BufferedReader newReader(InputStream self)
self
- an input stream
public static BufferedReader newReader(InputStream self, String charset)
public static Writer newWriter(OutputStream stream)
public static Writer newWriter(OutputStream stream, String charset)
public static String readLine(Reader self)
self
- a Reader
public static List readLines(InputStream stream)
stream
- a streamcharset
- opens the stream with a specified charset
public static List readLines(InputStream stream, String charset)
reader
- a Reader
public static List readLines(Reader reader)
is
- an input stream
public static void setBytes(OutputStream os, byte[] bytes)
writer
- a BufferedWriterline
- the line to write
public static Object splitEachLine(Reader self, String regex, Closure closure)
Here is an example:
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 Object splitEachLine(Reader self, Pattern pattern, Closure closure)
Here is an example:
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 Object splitEachLine(InputStream stream, String regex, String charset, Closure closure)
stream
- an InputStreamregex
- the delimiting regular expressioncharset
- opens the stream with a specified charsetclosure
- a closure
public static Object splitEachLine(InputStream stream, Pattern pattern, 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 Object splitEachLine(InputStream stream, String regex, Closure closure)
stream
- an InputStreamregex
- the delimiting regular expressionclosure
- a closure
public static Object splitEachLine(InputStream stream, Pattern pattern, Closure closure)
stream
- an InputStreampattern
- the regular expression Pattern for the delimiterclosure
- a closure
public static void transformChar(Reader self, Writer writer, Closure closure)
public static void transformLine(Reader reader, 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 Appendable withFormatter(Appendable self, Closure closure)
self
- an Appendableclosure
- a 1-arg Closure which will be called with a Formatter as its argument
public static Appendable withFormatter(Appendable self, 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 Object withObjectInputStream(InputStream inputStream, Closure closure)
inputStream
- an input streamclosure
- a closure
public static Object withObjectInputStream(InputStream inputStream, ClassLoader classLoader, Closure closure)
inputStream
- an input streamclassLoader
- the class loader to use when loading the classclosure
- a closure
public static Object withObjectOutputStream(OutputStream outputStream, Closure closure)
outputStream
- am output streamclosure
- a closure
public static Object withPrintWriter(Writer writer, Closure closure)
public static Object withPrintWriter(OutputStream stream, Closure closure)
public static Object withReader(Reader reader, Closure closure)
public static Object withReader(InputStream in, Closure closure)
in
- a streamcharset
- the charset used to decode the streamclosure
- the closure to invoke with the reader
public static Object withReader(InputStream in, String charset, Closure closure)
stream
- the stream which is used and then closedclosure
- the closure that the writer is passed into
public static Object withStream(InputStream stream, Closure closure)
public static Object withStream(OutputStream os, Closure closure)
public static Object withWriter(Writer writer, Closure closure)
public static Object withWriter(OutputStream stream, Closure closure)
public static Object withWriter(OutputStream stream, String charset, Closure closure)
public static void write(Writer self, Writable writable)
self
- a Writerwritable
- an object implementing the Writable interface
public static void writeLine(BufferedWriter writer, String line)
self
- a Reader object
Copyright © 2003-2013 The Codehaus. All rights reserved.