public class IOGroovyMethods extends DefaultGroovyMethodsSupport
This class defines new groovy methods for Readers, Writers, InputStreams and
OutputStreams 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 T eachLine(InputStream self, Closure c)
provides a eachLine(Closure c)
method for InputStream
.
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.
Type Params | Return Type | Name and description |
---|---|---|
|
public static void |
eachByte(InputStream is, Closure closure) Traverse through each byte of the specified stream. |
|
public static void |
eachByte(InputStream is, int bufferLen, Closure 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. |
<T> |
public static T |
eachLine(InputStream stream, String charset, Closure<T> closure) Iterates through this stream reading with the provided charset, passing each line to the given 1 or 2 arg closure. |
<T> |
public static T |
eachLine(InputStream stream, String charset, int firstLine, Closure<T> closure) Iterates through this stream reading with the provided charset, passing each line to the given 1 or 2 arg closure. |
<T> |
public static T |
eachLine(InputStream stream, Closure<T> closure) Iterates through this stream, passing each line to the given 1 or 2 arg closure. |
<T> |
public static T |
eachLine(InputStream stream, int firstLine, Closure<T> closure) Iterates through this stream, passing each line to the given 1 or 2 arg closure. |
<T> |
public static T |
eachLine(Reader self, Closure<T> closure) Iterates through the given reader line by line. |
<T> |
public static T |
eachLine(Reader self, int firstLine, Closure<T> closure) Iterates through the given reader line by line. |
|
public static void |
eachObject(ObjectInputStream ois, Closure closure) Iterates through the given object stream object by object. |
|
public 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. |
|
public 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. |
|
public static Writable |
filterLine(InputStream self, Closure predicate) Filter lines from an input stream using a closure predicate. |
|
public static Writable |
filterLine(InputStream self, String charset, Closure predicate) Filter lines from an input stream using a closure predicate. |
|
public 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. |
|
public static void |
filterLine(InputStream self, Writer writer, String charset, Closure predicate) Uses a closure to filter lines from this InputStream and pass them to the given writer. |
|
public static byte[] |
getBytes(InputStream is) Read the content of this InputStream and return it as a byte[]. |
|
public static String |
getText(InputStream is) Read the content of this InputStream and return it as a String. |
|
public static String |
getText(InputStream is, String charset) Read the content of this InputStream using specified charset and return it as a String. |
|
public static String |
getText(Reader reader) Read the content of the Reader and return it as a String. |
|
public static String |
getText(BufferedReader reader) Read the content of the BufferedReader and return it as a String. |
|
public boolean |
hasNext() |
|
public boolean |
hasNext() |
|
public static Iterator<String> |
iterator(Reader self) Creates an iterator which will traverse through the reader a line at a time. |
|
public static Iterator<Byte> |
iterator(InputStream self) Standard iterator for an input stream which iterates through the stream content in a byte-based fashion. |
|
public static Iterator<Byte> |
iterator(DataInputStream self) Standard iterator for a data input stream which iterates through the stream content a Byte at a time. |
|
public 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. |
|
public 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. |
|
public static Writer |
leftShift(OutputStream self, Object value) |
|
public static void |
leftShift(ObjectOutputStream self, Object value) Overloads the leftShift operator to add objects to an ObjectOutputStream. |
|
public static OutputStream |
leftShift(OutputStream self, InputStream in) Pipe an InputStream into an OutputStream for efficient stream copying. |
|
public static OutputStream |
leftShift(OutputStream self, byte[] value) Overloads the leftShift operator to provide an append mechanism to add bytes to a stream. |
|
public static ObjectInputStream |
newObjectInputStream(InputStream inputStream) Create an object input stream for this input stream. |
|
public static ObjectInputStream |
newObjectInputStream(InputStream inputStream, ClassLoader classLoader) Create an object input stream for this input stream using the given class loader. |
|
public static ObjectOutputStream |
newObjectOutputStream(OutputStream outputStream) Create an object output stream for this output stream. |
|
public static PrintWriter |
newPrintWriter(Writer writer) Create a new PrintWriter for this Writer. |
|
public static PrintWriter |
newPrintWriter(OutputStream stream) Create a new PrintWriter for this OutputStream. |
|
public static BufferedReader |
newReader(InputStream self) Creates a reader for this input stream. |
|
public static BufferedReader |
newReader(InputStream self, String charset) Creates a reader for this input stream, using the specified charset as the encoding. |
|
public static Writer |
newWriter(OutputStream stream) Creates a writer for this stream. |
|
public static Writer |
newWriter(OutputStream stream, String charset) Creates a writer for this stream using the given charset. |
|
public String |
next() |
|
public Byte |
next() |
|
public static String |
readLine(Reader self) Read a single, whole line from the given Reader. |
|
public static List<String> |
readLines(InputStream stream) Reads the stream into a list, with one element for each line. |
|
public static List<String> |
readLines(InputStream stream, String charset) Reads the stream into a list, with one element for each line. |
|
public static List<String> |
readLines(Reader reader) Reads the reader into a list of Strings, with one entry for each line. |
|
public void |
remove() |
|
public void |
remove() |
|
protected Class<?> |
resolveClass(ObjectStreamClass desc) |
|
public static void |
setBytes(OutputStream os, byte[] bytes) Write the byte[] to the output stream. |
<T> |
public static T |
splitEachLine(Reader self, String regex, Closure<T> closure) Iterates through the given reader line by line, splitting each line using the given regex separator. |
<T> |
public static T |
splitEachLine(Reader self, Pattern pattern, Closure<T> closure) Iterates through the given reader line by line, splitting each line using the given regex separator Pattern. |
<T> |
public static T |
splitEachLine(InputStream stream, String regex, String charset, Closure<T> closure) Iterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator. |
<T> |
public static T |
splitEachLine(InputStream stream, Pattern pattern, String charset, Closure<T> closure) Iterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator Pattern. |
<T> |
public static T |
splitEachLine(InputStream stream, String regex, Closure<T> closure) Iterates through the given InputStream line by line, splitting each line using the given separator. |
<T> |
public static T |
splitEachLine(InputStream stream, Pattern pattern, Closure<T> closure) Iterates through the given InputStream line by line, splitting each line using the given separator Pattern. |
|
public String |
toString() |
|
public static void |
transformChar(Reader self, Writer writer, Closure closure) Transforms each character from this reader by passing it to the given closure. |
|
public static void |
transformLine(Reader reader, Writer writer, Closure closure) Transforms the lines from a reader with a Closure and write them to a writer. |
<T, U extends Closeable> |
public static T |
withCloseable(U self, Closure<T> 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. |
<T, U extends AutoCloseable> |
public static T |
withCloseable(U self, Closure<T> 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 |
withFormatter(Appendable self, Closure closure) Invokes a Closure that uses a Formatter taking care of resource handling. |
|
public static Appendable |
withFormatter(Appendable self, Locale locale, Closure closure) Invokes a Closure that uses a Formatter taking care of resource handling. |
<T> |
public static T |
withObjectInputStream(InputStream inputStream, Closure<T> closure) Create a new ObjectInputStream for this input stream and pass it to the closure. |
<T> |
public static T |
withObjectInputStream(InputStream inputStream, ClassLoader classLoader, Closure<T> closure) Create a new ObjectInputStream for this input stream and pass it to the closure. |
<T> |
public static T |
withObjectOutputStream(OutputStream outputStream, Closure<T> closure) Create a new ObjectOutputStream for this output stream and then pass it to the closure. |
<T> |
public static T |
withPrintWriter(Writer writer, Closure<T> closure) Create a new PrintWriter for this Writer. |
<T> |
public static T |
withPrintWriter(OutputStream stream, Closure<T> closure) Create a new PrintWriter for this OutputStream. |
<T> |
public static T |
withReader(Reader reader, Closure<T> closure) Allows this reader to be used within the closure, ensuring that it is closed before this method returns. |
<T> |
public static T |
withReader(InputStream in, Closure<T> closure) Helper method to create a new Reader for a stream and then passes it into the closure. |
<T> |
public static T |
withReader(InputStream in, String charset, Closure<T> closure) Helper method to create a new Reader for a stream and then passes it into the closure. |
<T, U extends InputStream> |
public static T |
withStream(U stream, Closure<T> closure) Allows this input stream to be used within the closure, ensuring that it is flushed and closed before this method returns. |
<T, U extends OutputStream> |
public static T |
withStream(U os, Closure<T> closure) Passes this OutputStream to the closure, ensuring that the stream is closed after the closure returns, regardless of errors. |
<T> |
public static T |
withWriter(Writer writer, Closure<T> closure) Allows this writer to be used within the closure, ensuring that it is flushed and closed before this method returns. |
<T> |
public static T |
withWriter(OutputStream stream, Closure<T> closure) Creates a writer from this stream, passing it to the given closure. |
<T> |
public static T |
withWriter(OutputStream stream, String charset, Closure<T> closure) Creates a writer from this stream, passing it to the given closure. |
|
public 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. |
|
public static void |
writeLine(BufferedWriter writer, String line) Write the text and append a newline (using the platform's line-ending). |
|
public Writer |
writeTo(Writer out) |
Traverse through each byte of the specified stream. The stream is closed after the closure returns.
is
- stream to iterate over, closed after the method callclosure
- closure to apply to each byteTraverse through each the specified stream reading bytes into a buffer and calling the 2 parameter closure with this buffer and the number of bytes.
is
- stream to iterate over, closed after the method call.bufferLen
- the length of the buffer to use.closure
- a 2 parameter closure which is passed the byte[] and a number of bytes successfully read.Iterates through this stream reading with the provided charset, passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.
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)Iterates through this stream reading with the provided charset, passing each line to the given 1 or 2 arg closure. The stream is closed after this method returns.
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)Iterates through this stream, passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.
stream
- a streamclosure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)Iterates through this stream, passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.
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)Iterates through the given reader line by line. Each line is passed to the given 1 or 2 arg closure. If the closure has two arguments, the line count is passed as the second argument. The Reader is closed before this method returns.
self
- a Reader, closed after the method returnsclosure
- a closure (arg 1 is line, optional arg 2 is line number starting at line 1)Iterates through the given reader line by line. Each line is passed to the given 1 or 2 arg closure. If the closure has two arguments, the line count is passed as the second argument. The Reader is closed before this method returns.
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)Iterates through the given object stream object by object. The ObjectInputStream is closed afterwards.
ois
- an ObjectInputStream, closed after the operationclosure
- a closureFilter the lines from a reader and write them on the writer, according to a closure which returns true if the line should be included. Both Reader and Writer are closed after the operation.
reader
- a reader, closed after the callwriter
- a writer, closed after the callclosure
- the closure which returns booleans Filter the lines from this Reader, and return a Writable which can be
used to stream the filtered lines to a destination. The closure should
return true
if the line should be passed to the writer.
reader
- this readerclosure
- a closure used for filtering Filter lines from an input stream using a closure predicate. The closure
will be passed each line as a String, and it should return
true
if the line should be passed to the writer.
self
- an input streampredicate
- a closure which returns boolean and takes a line Filter lines from an input stream using a closure predicate. The closure
will be passed each line as a String, and it should return
true
if the line should be passed to the writer.
self
- an input streamcharset
- opens the stream with a specified charsetpredicate
- a closure which returns boolean and takes a line Uses a closure to filter lines from this InputStream and pass them to
the given writer. The closure will be passed each line as a String, and
it should return 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 Uses a closure to filter lines from this InputStream and pass them to
the given writer. The closure will be passed each line as a String, and
it should return 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 acceptedRead the content of this InputStream and return it as a byte[]. The stream is closed before this method returns.
is
- an input streamRead the content of this InputStream and return it as a String. The stream is closed before this method returns.
is
- an input streamRead the content of this InputStream using specified charset and return it as a String. The stream is closed before this method returns.
is
- an input streamcharset
- opens the stream with a specified charsetRead the content of the Reader and return it as a String. The reader is closed before this method returns.
reader
- a Reader whose content we want to readRead the content of the BufferedReader and return it as a String. The BufferedReader is closed afterwards.
reader
- a BufferedReader whose content we want to readCreates an iterator which will traverse through the reader a line at a time.
self
- a Reader objectStandard iterator for an input stream which iterates through the stream content in a byte-based fashion.
self
- an InputStream objectStandard iterator for a data input stream which iterates through the stream content a Byte at a time.
self
- a DataInputStream objectOverloads the leftShift operator for Writer to allow an object to be written using Groovy's default representation for the object.
self
- a Writervalue
- an Object whose default representation will be written to the WriterOverloads the leftShift operator for Appendable to allow an object to be appended using Groovy's default representation for the object.
self
- an Appendablevalue
- an Object whose default representation will be appended to the AppendableOverloads the leftShift operator to add objects to an ObjectOutputStream.
self
- an ObjectOutputStreamvalue
- an object to write to the streamPipe an InputStream into an OutputStream for efficient stream copying.
self
- stream on which to writein
- stream to read fromOverloads the leftShift operator to provide an append mechanism to add bytes to a stream.
self
- an OutputStreamvalue
- a value to appendCreate an object input stream for this input stream.
inputStream
- an input streamCreate an object input stream for this input stream using the given class loader.
inputStream
- an input streamclassLoader
- the class loader to use when loading the classCreate an object output stream for this output stream.
outputStream
- an output streamCreate a new PrintWriter for this Writer.
writer
- a WriterCreate a new PrintWriter for this OutputStream.
stream
- an OutputStreamCreates a reader for this input stream.
self
- an input streamCreates a reader for this input stream, using the specified charset as the encoding.
self
- an input streamcharset
- the charset for this input streamCreates a writer for this stream.
stream
- the stream which is used and then closedCreates a writer for this stream using the given charset.
stream
- the stream which is used and then closedcharset
- the charset used Read a single, whole line from the given Reader. This method is designed for use with
Readers that support the mark()
operation like BufferReader. It has a fallback
behavior for Readers that don't support mark() but the behavior doesn't correctly
detect multi-character line termination (e.g. carriage return followed by linefeed).
We recommend for Readers that don't support mark() you consider using one of the
following methods instead: eachLine, readLines, or iterator.
self
- a ReaderReads the stream into a list, with one element for each line.
stream
- a streamReads the stream into a list, with one element for each line.
stream
- a streamcharset
- opens the stream with a specified charsetReads the reader into a list of Strings, with one entry for each line. The reader is closed before this method returns.
reader
- a ReaderWrite the byte[] to the output stream. The stream is closed before this method returns.
os
- an output streambytes
- the byte[] to write to the output streamIterates through the given reader line by line, splitting each line using the given regex separator. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. The Reader is closed afterwards.
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 closureIterates through the given reader line by line, splitting each line using the given regex separator Pattern. For each line, the given closure is called with a single parameter being the list of strings computed by splitting the line around matches of the given regular expression. The Reader is closed afterwards.
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 closureIterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator. The list of tokens for each line is then passed to the given closure. Finally, the stream is closed.
stream
- an InputStreamregex
- the delimiting regular expressioncharset
- opens the stream with a specified charsetclosure
- a closureIterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator Pattern. The list of tokens for each line is then passed to the given closure. Finally, the stream is closed.
stream
- an InputStreampattern
- the regular expression Pattern for the delimitercharset
- opens the stream with a specified charsetclosure
- a closureIterates through the given InputStream line by line, splitting each line using the given separator. The list of tokens for each line is then passed to the given closure. The stream is closed before the method returns.
stream
- an InputStreamregex
- the delimiting regular expressionclosure
- a closureIterates through the given InputStream line by line, splitting each line using the given separator Pattern. The list of tokens for each line is then passed to the given closure. The stream is closed before the method returns.
stream
- an InputStreampattern
- the regular expression Pattern for the delimiterclosure
- a closureTransforms each character from this reader by passing it to the given closure. The Closure should return each transformed character, which will be passed to the Writer. The reader and writer will both be closed before this method returns.
self
- a Reader objectwriter
- a Writer to receive the transformed charactersclosure
- a closure that performs the required transformationTransforms the lines from a reader with a Closure and write them to a writer. Both Reader and Writer are closed after the operation.
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.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.
As with the try-with-resources statement, if multiple exceptions are thrown the exception from the closure will be returned and the exception from closing will be added as a suppressed exception.
self
- the Closeableaction
- the closure taking the Closeable as parameterAllows this AutoCloseable to be used within the closure, ensuring that it is closed once the closure has been executed and before this method returns.
As with the try-with-resources statement, if multiple exceptions are thrown the exception from the closure will be returned and the exception from closing will be added as a suppressed exception.
self
- the AutoCloseableaction
- the closure taking the AutoCloseable as parameterInvokes a Closure that uses a Formatter taking care of resource handling. A Formatter is created and passed to the Closure as its argument. After the Closure executes, the Formatter is flushed and closed releasing any associated resources.
self
- an Appendableclosure
- a 1-arg Closure which will be called with a Formatter as its argumentInvokes a Closure that uses a Formatter taking care of resource handling. A Formatter is created using the given Locale and passed to the Closure as its argument. After the Closure executes, the Formatter is flushed and closed releasing any associated resources.
self
- an Appendablelocale
- a Locale used when creating the Formatterclosure
- a 1-arg Closure which will be called with a Formatter as its argumentCreate a new ObjectInputStream for this input stream and pass it to the closure. This method ensures the stream is closed after the closure returns.
inputStream
- an input streamclosure
- a closureCreate a new ObjectInputStream for this input stream and pass it to the closure. This method ensures the stream is closed after the closure returns.
inputStream
- an input streamclassLoader
- the class loader to use when loading the classclosure
- a closureCreate a new ObjectOutputStream for this output stream and then pass it to the closure. This method ensures the stream is closed after the closure returns.
outputStream
- am output streamclosure
- a closureCreate a new PrintWriter for this Writer. The writer is passed to the closure, and will be closed before this method returns.
writer
- a writerclosure
- the closure to invoke with the PrintWriterCreate a new PrintWriter for this OutputStream. The writer is passed to the closure, and will be closed before this method returns.
stream
- an OutputStreamclosure
- the closure to invoke with the PrintWriterAllows this reader to be used within the closure, ensuring that it is closed before this method returns.
reader
- the reader which is used and then closedclosure
- the closure that the writer is passed intoHelper method to create a new Reader for a stream and then passes it into the closure. The reader (and this stream) is closed after the closure returns.
in
- a streamclosure
- the closure to invoke with the InputStreamHelper method to create a new Reader for a stream and then passes it into the closure. The reader (and this stream) is closed after the closure returns.
in
- a streamcharset
- the charset used to decode the streamclosure
- the closure to invoke with the readerAllows this input stream to be used within the closure, ensuring that it is flushed and closed before this method returns.
stream
- the stream which is used and then closedclosure
- the closure that the stream is passed intoPasses this OutputStream to the closure, ensuring that the stream is closed after the closure returns, regardless of errors.
os
- the stream which is used and then closedclosure
- the closure that the stream is passed intoAllows this writer to be used within the closure, ensuring that it is flushed and closed before this method returns.
writer
- the writer which is used and then closedclosure
- the closure that the writer is passed intoCreates a writer from this stream, passing it to the given closure. This method ensures the stream is closed after the closure returns.
stream
- the stream which is used and then closedclosure
- the closure that the writer is passed intoCreates a writer from this stream, passing it to the given closure. This method ensures the stream is closed after the closure returns.
stream
- the stream which is used and then closedcharset
- the charset usedclosure
- the closure that the writer is passed intoA 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.
self
- a Writerwritable
- an object implementing the Writable interfaceWrite the text and append a newline (using the platform's line-ending).
writer
- a BufferedWriterline
- the line to write