Class IOGroovyMethods

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.

  • Constructor Summary

    Constructors
    Constructor Description
    IOGroovyMethods()  
  • Method Summary

    Modifier and Type Method Description
    static void eachByte​(java.io.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.
    static void eachByte​(java.io.InputStream is, Closure closure)
    Traverse through each byte of the specified stream.
    static <T> T eachLine​(java.io.InputStream stream, int firstLine, Closure<T> closure)
    Iterates through this stream, passing each line to the given 1 or 2 arg closure.
    static <T> T eachLine​(java.io.InputStream stream, Closure<T> closure)
    Iterates through this stream, passing each line to the given 1 or 2 arg closure.
    static <T> T eachLine​(java.io.InputStream stream, java.lang.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.
    static <T> T eachLine​(java.io.InputStream stream, java.lang.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.
    static <T> T eachLine​(java.io.Reader self, int firstLine, Closure<T> closure)
    Iterates through the given reader line by line.
    static <T> T eachLine​(java.io.Reader self, Closure<T> 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 Writable filterLine​(java.io.InputStream self, Closure predicate)
    Filter lines from an input stream using a closure predicate.
    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)
    Uses a closure to filter lines from this InputStream and pass them to the given writer.
    static Writable filterLine​(java.io.InputStream self, java.lang.String charset, Closure predicate)
    Filter lines from an input stream using a closure predicate.
    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 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 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.BufferedReader reader)
    Read the content of the BufferedReader and return it as a String.
    static java.lang.String getText​(java.io.InputStream is)
    Read the content of this InputStream and return it as a String.
    static java.lang.String getText​(java.io.InputStream is, java.lang.String charset)
    Read the content of this InputStream using specified charset and return it as a String.
    static java.lang.String getText​(java.io.Reader reader)
    Read the content of the Reader and return it as a String.
    static java.util.Iterator<java.lang.Byte> iterator​(java.io.DataInputStream self)
    Standard iterator for a data input stream which iterates through the stream content a Byte at a time.
    static java.util.Iterator<java.lang.Byte> 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<java.lang.String> iterator​(java.io.Reader self)
    Creates an iterator which will traverse through the reader a line at a time.
    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, byte[] value)
    Overloads the leftShift operator to provide an append mechanism to add bytes to a stream.
    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.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 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.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.OutputStream stream)
    Create a new PrintWriter for this OutputStream.
    static java.io.PrintWriter newPrintWriter​(java.io.Writer writer)
    Create a new PrintWriter for this 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)
    Creates a reader for this input stream, using the specified charset as the encoding.
    static java.io.Writer newWriter​(java.io.OutputStream stream)
    Creates a writer for this stream.
    static java.io.Writer newWriter​(java.io.OutputStream stream, java.lang.String charset)
    Creates a writer for this stream using the given charset.
    static java.lang.String readLine​(java.io.Reader self)
    Read a single, whole line from the given Reader.
    static java.util.List<java.lang.String> readLines​(java.io.InputStream stream)
    Reads the stream into a list, with one element for each line.
    static java.util.List<java.lang.String> readLines​(java.io.InputStream stream, java.lang.String charset)
    Reads the stream into a list, with one element for each line.
    static java.util.List<java.lang.String> readLines​(java.io.Reader reader)
    Reads the reader into a list of Strings, with one entry for each line.
    static void setBytes​(java.io.OutputStream os, byte[] bytes)
    Write the byte[] to the output stream.
    static <T> T splitEachLine​(java.io.InputStream stream, java.lang.String regex, Closure<T> closure)
    Iterates through the given InputStream line by line, splitting each line using the given separator.
    static <T> T splitEachLine​(java.io.InputStream stream, java.lang.String regex, java.lang.String charset, Closure<T> closure)
    Iterates through the given InputStream line by line using the specified encoding, splitting each line using the given separator.
    static <T> T splitEachLine​(java.io.InputStream stream, java.util.regex.Pattern pattern, Closure<T> closure)
    Iterates through the given InputStream line by line, splitting each line using the given separator Pattern.
    static <T> T splitEachLine​(java.io.InputStream stream, java.util.regex.Pattern pattern, java.lang.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.
    static <T> T splitEachLine​(java.io.Reader self, java.lang.String regex, Closure<T> closure)
    Iterates through the given reader line by line, splitting each line using the given regex separator.
    static <T> T splitEachLine​(java.io.Reader self, java.util.regex.Pattern pattern, Closure<T> closure)
    Iterates through the given reader line by line, splitting each line using the given regex separator Pattern.
    static void transformChar​(java.io.Reader self, java.io.Writer writer, Closure closure)
    Transforms each character from this reader by passing it to the given 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 <T,​ U extends java.io.Closeable>
    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.
    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 <T> T withObjectInputStream​(java.io.InputStream inputStream, Closure<T> closure)
    Create a new ObjectInputStream for this file and pass it to the closure.
    static <T> T withObjectInputStream​(java.io.InputStream inputStream, java.lang.ClassLoader classLoader, Closure<T> closure)
    Create a new ObjectInputStream for this file and pass it to the closure.
    static <T> T withObjectOutputStream​(java.io.OutputStream outputStream, Closure<T> closure)
    Create a new ObjectOutputStream for this output stream and then pass it to the closure.
    static <T> T withPrintWriter​(java.io.OutputStream stream, Closure<T> closure)
    Create a new PrintWriter for this OutputStream.
    static <T> T withPrintWriter​(java.io.Writer writer, Closure<T> closure)
    Create a new PrintWriter for this Writer.
    static <T> T withReader​(java.io.InputStream in, Closure<T> closure)
    Helper method to create a new Reader for a stream and then passes it into the closure.
    static <T> T withReader​(java.io.InputStream in, java.lang.String charset, Closure<T> closure)
    Helper method to create a new Reader for a stream and then passes it into the closure.
    static <T> T withReader​(java.io.Reader reader, Closure<T> closure)
    Allows this reader to be used within the closure, ensuring that it is closed before this method returns.
    static <T,​ U extends java.io.InputStream>
    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.
    static <T> T withWriter​(java.io.OutputStream stream, Closure<T> closure)
    Creates a writer from this stream, passing it to the given closure.
    static <T> T withWriter​(java.io.OutputStream stream, java.lang.String charset, Closure<T> closure)
    Creates a writer from this stream, passing it to the given closure.
    static <T> T withWriter​(java.io.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.
    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)
    Write the text and append a newline (using the platform's line-ending).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • IOGroovyMethods

      public IOGroovyMethods()
  • Method Details

    • leftShift

      public static java.io.Writer leftShift​(java.io.Writer self, java.lang.Object value) throws java.io.IOException
      Overloads the leftShift operator for Writer to allow an object to be written using Groovy's default representation for the object.
      Parameters:
      self - a Writer
      value - an Object whose default representation will be written to the Writer
      Returns:
      the writer on which this operation was invoked
      Throws:
      java.io.IOException - if an I/O error occurs.
      Since:
      1.0
    • leftShift

      public static java.lang.Appendable leftShift​(java.lang.Appendable self, java.lang.Object value) throws java.io.IOException
      Overloads the leftShift operator for Appendable to allow an object to be appended using Groovy's default representation for the object.
      Parameters:
      self - an Appendable
      value - an Object whose default representation will be appended to the Appendable
      Returns:
      the Appendable on which this operation was invoked
      Throws:
      java.io.IOException - if an I/O error occurs.
      Since:
      2.1.0
    • withFormatter

      public static java.lang.Appendable withFormatter​(java.lang.Appendable self, Closure closure)
      Invokes 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.
      Parameters:
      self - an Appendable
      closure - a 1-arg Closure which will be called with a Formatter as its argument
      Returns:
      the Appendable on which this operation was invoked
      Since:
      2.1.0
    • withFormatter

      public 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. 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.
      Parameters:
      self - an Appendable
      locale - a Locale used when creating the Formatter
      closure - a 1-arg Closure which will be called with a Formatter as its argument
      Returns:
      the Appendable on which this operation was invoked
      Since:
      2.1.0
    • write

      public static void write​(java.io.Writer self, Writable writable) throws java.io.IOException
      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.
      Parameters:
      self - a Writer
      writable - an object implementing the Writable interface
      Throws:
      java.io.IOException - if an I/O error occurs.
      Since:
      1.0
    • leftShift

      public static java.io.Writer leftShift​(java.io.OutputStream self, java.lang.Object value) throws java.io.IOException
      Overloads the leftShift operator to provide an append mechanism to add values to a stream.
      Parameters:
      self - an OutputStream
      value - a value to append
      Returns:
      a Writer
      Throws:
      java.io.IOException - if an I/O error occurs.
      Since:
      1.0
    • leftShift

      public static void leftShift​(java.io.ObjectOutputStream self, java.lang.Object value) throws java.io.IOException
      Overloads the leftShift operator to add objects to an ObjectOutputStream.
      Parameters:
      self - an ObjectOutputStream
      value - an object to write to the stream
      Throws:
      java.io.IOException - if an I/O error occurs.
      Since:
      1.5.0
    • leftShift

      public static java.io.OutputStream leftShift​(java.io.OutputStream self, java.io.InputStream in) throws java.io.IOException
      Pipe an InputStream into an OutputStream for efficient stream copying.
      Parameters:
      self - stream on which to write
      in - stream to read from
      Returns:
      the outputstream itself
      Throws:
      java.io.IOException - if an I/O error occurs.
      Since:
      1.0
    • leftShift

      public static java.io.OutputStream leftShift​(java.io.OutputStream self, byte[] value) throws java.io.IOException
      Overloads the leftShift operator to provide an append mechanism to add bytes to a stream.
      Parameters:
      self - an OutputStream
      value - a value to append
      Returns:
      an OutputStream
      Throws:
      java.io.IOException - if an I/O error occurs.
      Since:
      1.0
    • newObjectOutputStream

      public static java.io.ObjectOutputStream newObjectOutputStream​(java.io.OutputStream outputStream) throws java.io.IOException
      Create an object output stream for this output stream.
      Parameters:
      outputStream - an output stream
      Returns:
      an object output stream
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.0
    • withObjectOutputStream

      public static <T> T withObjectOutputStream​(java.io.OutputStream outputStream, Closure<T> closure) throws java.io.IOException
      Create 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.
      Parameters:
      outputStream - am output stream
      closure - a closure
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.0
      See Also:
      withStream(java.io.OutputStream, groovy.lang.Closure)
    • newObjectInputStream

      public static java.io.ObjectInputStream newObjectInputStream​(java.io.InputStream inputStream) throws java.io.IOException
      Create an object input stream for this input stream.
      Parameters:
      inputStream - an input stream
      Returns:
      an object input stream
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.0
    • newObjectInputStream

      public static java.io.ObjectInputStream newObjectInputStream​(java.io.InputStream inputStream, java.lang.ClassLoader classLoader) throws java.io.IOException
      Create an object input stream for this input stream using the given class loader.
      Parameters:
      inputStream - an input stream
      classLoader - the class loader to use when loading the class
      Returns:
      an object input stream
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.0
    • eachObject

      public static void eachObject​(java.io.ObjectInputStream ois, Closure closure) throws java.io.IOException, java.lang.ClassNotFoundException
      Iterates through the given object stream object by object. The ObjectInputStream is closed afterwards.
      Parameters:
      ois - an ObjectInputStream, closed after the operation
      closure - a closure
      Throws:
      java.io.IOException - if an IOException occurs.
      java.lang.ClassNotFoundException - if the class is not found.
      Since:
      1.0
    • withObjectInputStream

      public static <T> T withObjectInputStream​(java.io.InputStream inputStream, Closure<T> closure) throws java.io.IOException
      Create a new ObjectInputStream for this file and pass it to the closure. This method ensures the stream is closed after the closure returns.
      Parameters:
      inputStream - an input stream
      closure - a closure
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.0
      See Also:
      withStream(java.io.InputStream, groovy.lang.Closure)
    • withObjectInputStream

      public static <T> T withObjectInputStream​(java.io.InputStream inputStream, java.lang.ClassLoader classLoader, Closure<T> closure) throws java.io.IOException
      Create a new ObjectInputStream for this file and pass it to the closure. This method ensures the stream is closed after the closure returns.
      Parameters:
      inputStream - an input stream
      classLoader - the class loader to use when loading the class
      closure - a closure
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.0
      See Also:
      withStream(java.io.InputStream, groovy.lang.Closure)
    • eachLine

      public static <T> T eachLine​(java.io.InputStream stream, java.lang.String charset, Closure<T> closure) throws java.io.IOException
      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.
      Parameters:
      stream - a stream
      charset - opens the stream with a specified charset
      closure - a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.5
      See Also:
      eachLine(java.io.InputStream, java.lang.String, int, groovy.lang.Closure)
    • eachLine

      public static <T> T eachLine​(java.io.InputStream stream, java.lang.String charset, int firstLine, Closure<T> closure) throws java.io.IOException
      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.
      Parameters:
      stream - a stream
      charset - opens the stream with a specified charset
      firstLine - 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)
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.7
      See Also:
      eachLine(java.io.Reader, int, groovy.lang.Closure)
    • eachLine

      public static <T> T eachLine​(java.io.InputStream stream, Closure<T> closure) throws java.io.IOException
      Iterates through this stream, passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.
      Parameters:
      stream - a stream
      closure - a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.6
      See Also:
      eachLine(java.io.InputStream, int, groovy.lang.Closure)
    • eachLine

      public static <T> T eachLine​(java.io.InputStream stream, int firstLine, Closure<T> closure) throws java.io.IOException
      Iterates through this stream, passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.
      Parameters:
      stream - a stream
      firstLine - 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)
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.7
      See Also:
      eachLine(java.io.Reader, int, groovy.lang.Closure)
    • eachLine

      public static <T> T eachLine​(java.io.Reader self, Closure<T> closure) throws java.io.IOException
      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.
      Parameters:
      self - a Reader, closed after the method returns
      closure - a closure (arg 1 is line, optional arg 2 is line number starting at line 1)
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.6
      See Also:
      eachLine(java.io.Reader, int, groovy.lang.Closure)
    • eachLine

      public static <T> T eachLine​(java.io.Reader self, int firstLine, Closure<T> closure) throws java.io.IOException
      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.
      Parameters:
      self - a Reader, closed after the method returns
      firstLine - 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)
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.7
    • splitEachLine

      public static <T> T splitEachLine​(java.io.Reader self, java.lang.String regex, Closure<T> closure) throws java.io.IOException
      Iterates 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|'
       
      Parameters:
      self - a Reader, closed after the method returns
      regex - the delimiting regular expression
      closure - a closure
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      java.util.regex.PatternSyntaxException - if the regular expression's syntax is invalid
      Since:
      1.5.5
      See Also:
      String.split(java.lang.String)
    • splitEachLine

      public static <T> T splitEachLine​(java.io.Reader self, java.util.regex.Pattern pattern, Closure<T> closure) throws java.io.IOException
      Iterates 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|'
       
      Parameters:
      self - a Reader, closed after the method returns
      pattern - the regular expression Pattern for the delimiter
      closure - a closure
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      java.util.regex.PatternSyntaxException - if the regular expression's syntax is invalid
      Since:
      1.6.8
      See Also:
      String.split(java.lang.String)
    • splitEachLine

      public static <T> T splitEachLine​(java.io.InputStream stream, java.lang.String regex, java.lang.String charset, Closure<T> closure) throws java.io.IOException
      Iterates 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.
      Parameters:
      stream - an InputStream
      regex - the delimiting regular expression
      charset - opens the stream with a specified charset
      closure - a closure
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      java.util.regex.PatternSyntaxException - if the regular expression's syntax is invalid
      Since:
      1.5.5
      See Also:
      splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)
    • splitEachLine

      public static <T> T splitEachLine​(java.io.InputStream stream, java.util.regex.Pattern pattern, java.lang.String charset, Closure<T> closure) throws java.io.IOException
      Iterates 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.
      Parameters:
      stream - an InputStream
      pattern - the regular expression Pattern for the delimiter
      charset - opens the stream with a specified charset
      closure - a closure
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.6.8
      See Also:
      splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
    • splitEachLine

      public static <T> T splitEachLine​(java.io.InputStream stream, java.lang.String regex, Closure<T> closure) throws java.io.IOException
      Iterates 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.
      Parameters:
      stream - an InputStream
      regex - the delimiting regular expression
      closure - a closure
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      java.util.regex.PatternSyntaxException - if the regular expression's syntax is invalid
      Since:
      1.5.6
      See Also:
      splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)
    • splitEachLine

      public static <T> T splitEachLine​(java.io.InputStream stream, java.util.regex.Pattern pattern, Closure<T> closure) throws java.io.IOException
      Iterates 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.
      Parameters:
      stream - an InputStream
      pattern - the regular expression Pattern for the delimiter
      closure - a closure
      Returns:
      the last value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.6.8
      See Also:
      splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
    • readLine

      public static java.lang.String readLine​(java.io.Reader self) throws java.io.IOException
      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.
      Parameters:
      self - a Reader
      Returns:
      a line
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
      See Also:
      readLines(java.io.Reader), iterator(java.io.Reader), eachLine(java.io.Reader, groovy.lang.Closure)
    • readLines

      public static java.util.List<java.lang.String> readLines​(java.io.InputStream stream) throws java.io.IOException
      Reads the stream into a list, with one element for each line.
      Parameters:
      stream - a stream
      Returns:
      a List of lines
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
      See Also:
      readLines(java.io.Reader)
    • readLines

      public static java.util.List<java.lang.String> readLines​(java.io.InputStream stream, java.lang.String charset) throws java.io.IOException
      Reads the stream into a list, with one element for each line.
      Parameters:
      stream - a stream
      charset - opens the stream with a specified charset
      Returns:
      a List of lines
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.6.8
      See Also:
      readLines(java.io.Reader)
    • readLines

      public static java.util.List<java.lang.String> readLines​(java.io.Reader reader) throws java.io.IOException
      Reads the reader into a list of Strings, with one entry for each line. The reader is closed before this method returns.
      Parameters:
      reader - a Reader
      Returns:
      a List of lines
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
    • getText

      public static java.lang.String getText​(java.io.InputStream is) throws java.io.IOException
      Read the content of this InputStream and return it as a String. The stream is closed before this method returns.
      Parameters:
      is - an input stream
      Returns:
      the text from that URL
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
    • getText

      public static java.lang.String getText​(java.io.InputStream is, java.lang.String charset) throws java.io.IOException
      Read the content of this InputStream using specified charset and return it as a String. The stream is closed before this method returns.
      Parameters:
      is - an input stream
      charset - opens the stream with a specified charset
      Returns:
      the text from that URL
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
    • getText

      public static java.lang.String getText​(java.io.Reader reader) throws java.io.IOException
      Read the content of the Reader and return it as a String. The reader is closed before this method returns.
      Parameters:
      reader - a Reader whose content we want to read
      Returns:
      a String containing the content of the buffered reader
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
      See Also:
      getText(java.io.BufferedReader)
    • getText

      public static java.lang.String getText​(java.io.BufferedReader reader) throws java.io.IOException
      Read the content of the BufferedReader and return it as a String. The BufferedReader is closed afterwards.
      Parameters:
      reader - a BufferedReader whose content we want to read
      Returns:
      a String containing the content of the buffered reader
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
    • getBytes

      public static byte[] getBytes​(java.io.InputStream is) throws java.io.IOException
      Read the content of this InputStream and return it as a byte[]. The stream is closed before this method returns.
      Parameters:
      is - an input stream
      Returns:
      the byte[] from that InputStream
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.7.1
    • setBytes

      public static void setBytes​(java.io.OutputStream os, byte[] bytes) throws java.io.IOException
      Write the byte[] to the output stream. The stream is closed before this method returns.
      Parameters:
      os - an output stream
      bytes - the byte[] to write to the output stream
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.7.1
    • writeLine

      public static void writeLine​(java.io.BufferedWriter writer, java.lang.String line) throws java.io.IOException
      Write the text and append a newline (using the platform's line-ending).
      Parameters:
      writer - a BufferedWriter
      line - the line to write
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
    • iterator

      public static java.util.Iterator<java.lang.String> iterator​(java.io.Reader self)
      Creates an iterator which will traverse through the reader a line at a time.
      Parameters:
      self - a Reader object
      Returns:
      an Iterator for the Reader
      Since:
      1.5.0
      See Also:
      BufferedReader.readLine()
    • iterator

      public static java.util.Iterator<java.lang.Byte> iterator​(java.io.InputStream self)
      Standard iterator for a input stream which iterates through the stream content in a byte-based fashion.
      Parameters:
      self - an InputStream object
      Returns:
      an Iterator for the InputStream
      Since:
      1.5.0
    • iterator

      public static java.util.Iterator<java.lang.Byte> iterator​(java.io.DataInputStream self)
      Standard iterator for a data input stream which iterates through the stream content a Byte at a time.
      Parameters:
      self - a DataInputStream object
      Returns:
      an Iterator for the DataInputStream
      Since:
      1.5.0
    • newReader

      public static java.io.BufferedReader newReader​(java.io.InputStream self)
      Creates a reader for this input stream.
      Parameters:
      self - an input stream
      Returns:
      a reader
      Since:
      1.0
    • newReader

      public static java.io.BufferedReader newReader​(java.io.InputStream self, java.lang.String charset) throws java.io.UnsupportedEncodingException
      Creates a reader for this input stream, using the specified charset as the encoding.
      Parameters:
      self - an input stream
      charset - the charset for this input stream
      Returns:
      a reader
      Throws:
      java.io.UnsupportedEncodingException - if the encoding specified is not supported
      Since:
      1.6.0
    • newPrintWriter

      public static java.io.PrintWriter newPrintWriter​(java.io.Writer writer)
      Create a new PrintWriter for this Writer.
      Parameters:
      writer - a Writer
      Returns:
      a PrintWriter
      Since:
      1.6.0
    • newPrintWriter

      public static java.io.PrintWriter newPrintWriter​(java.io.OutputStream stream)
      Create a new PrintWriter for this OutputStream.
      Parameters:
      stream - an OutputStream
      Returns:
      a PrintWriter
      Since:
      2.2.0
    • withPrintWriter

      public static <T> T withPrintWriter​(java.io.Writer writer, Closure<T> closure) throws java.io.IOException
      Create a new PrintWriter for this Writer. The writer is passed to the closure, and will be closed before this method returns.
      Parameters:
      writer - a writer
      closure - the closure to invoke with the PrintWriter
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.6.0
    • withPrintWriter

      public static <T> T withPrintWriter​(java.io.OutputStream stream, Closure<T> closure) throws java.io.IOException
      Create a new PrintWriter for this OutputStream. The writer is passed to the closure, and will be closed before this method returns.
      Parameters:
      stream - an OutputStream
      closure - the closure to invoke with the PrintWriter
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      2.2.0
    • withWriter

      public static <T> T withWriter​(java.io.Writer writer, Closure<T> closure) throws java.io.IOException
      Allows this writer to be used within the closure, ensuring that it is flushed and closed before this method returns.
      Parameters:
      writer - the writer which is used and then closed
      closure - the closure that the writer is passed into
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.2
    • withReader

      public static <T> T withReader​(java.io.Reader reader, Closure<T> closure) throws java.io.IOException
      Allows this reader to be used within the closure, ensuring that it is closed before this method returns.
      Parameters:
      reader - the reader which is used and then closed
      closure - the closure that the writer is passed into
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.2
    • withStream

      public static <T,​ U extends java.io.InputStream> T withStream​(U stream, Closure<T> closure) throws java.io.IOException
      Allows this input stream to be used within the closure, ensuring that it is flushed and closed before this method returns.
      Parameters:
      stream - the stream which is used and then closed
      closure - the closure that the stream is passed into
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.2
    • withReader

      public static <T> T withReader​(java.io.InputStream in, Closure<T> closure) throws java.io.IOException
      Helper 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.
      Parameters:
      in - a stream
      closure - the closure to invoke with the InputStream
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.2
      See Also:
      InputStreamReader
    • withReader

      public static <T> T withReader​(java.io.InputStream in, java.lang.String charset, Closure<T> closure) throws java.io.IOException
      Helper 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.
      Parameters:
      in - a stream
      charset - the charset used to decode the stream
      closure - the closure to invoke with the reader
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.6
      See Also:
      InputStreamReader
    • withWriter

      public static <T> T withWriter​(java.io.OutputStream stream, Closure<T> closure) throws java.io.IOException
      Creates a writer from this stream, passing it to the given closure. This method ensures the stream is closed after the closure returns.
      Parameters:
      stream - the stream which is used and then closed
      closure - the closure that the writer is passed into
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.2
      See Also:
      withWriter(java.io.Writer, groovy.lang.Closure)
    • newWriter

      public static java.io.Writer newWriter​(java.io.OutputStream stream)
      Creates a writer for this stream.
      Parameters:
      stream - the stream which is used and then closed
      Returns:
      the newly created Writer
      Since:
      2.2.0
    • withWriter

      public static <T> T withWriter​(java.io.OutputStream stream, java.lang.String charset, Closure<T> closure) throws java.io.IOException
      Creates a writer from this stream, passing it to the given closure. This method ensures the stream is closed after the closure returns.
      Parameters:
      stream - the stream which is used and then closed
      charset - the charset used
      closure - the closure that the writer is passed into
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.2
      See Also:
      withWriter(java.io.Writer, groovy.lang.Closure)
    • newWriter

      public static java.io.Writer newWriter​(java.io.OutputStream stream, java.lang.String charset) throws java.io.UnsupportedEncodingException
      Creates a writer for this stream using the given charset.
      Parameters:
      stream - the stream which is used and then closed
      charset - the charset used
      Returns:
      the newly created Writer
      Throws:
      java.io.UnsupportedEncodingException - if an encoding exception occurs.
      Since:
      2.2.0
    • withStream

      public static <T,​ U extends java.io.OutputStream> T withStream​(U os, Closure<T> closure) throws java.io.IOException
      Passes this OutputStream to the closure, ensuring that the stream is closed after the closure returns, regardless of errors.
      Parameters:
      os - the stream which is used and then closed
      closure - the closure that the stream is passed into
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.2
    • eachByte

      public static void eachByte​(java.io.InputStream is, Closure closure) throws java.io.IOException
      Traverse through each byte of the specified stream. The stream is closed after the closure returns.
      Parameters:
      is - stream to iterate over, closed after the method call
      closure - closure to apply to each byte
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
    • eachByte

      public static void eachByte​(java.io.InputStream is, int bufferLen, Closure closure) throws java.io.IOException
      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.
      Parameters:
      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.
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.8
    • transformChar

      public static void transformChar​(java.io.Reader self, java.io.Writer writer, Closure closure) throws java.io.IOException
      Transforms 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.
      Parameters:
      self - a Reader object
      writer - a Writer to receive the transformed characters
      closure - a closure that performs the required transformation
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.5.0
    • transformLine

      public static void transformLine​(java.io.Reader reader, java.io.Writer writer, Closure closure) throws java.io.IOException
      Transforms the lines from a reader with a Closure and write them to a writer. Both Reader and Writer are closed after the operation.
      Parameters:
      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.
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
    • filterLine

      public static void filterLine​(java.io.Reader reader, java.io.Writer writer, Closure closure) throws java.io.IOException
      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. Both Reader and Writer are closed after the operation.
      Parameters:
      reader - a reader, closed after the call
      writer - a writer, closed after the call
      closure - the closure which returns booleans
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
    • filterLine

      public 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. The closure should return true if the line should be passed to the writer.
      Parameters:
      reader - this reader
      closure - a closure used for filtering
      Returns:
      a Writable which will use the closure to filter each line from the reader when the Writable#writeTo(Writer) is called.
      Since:
      1.0
    • filterLine

      public static Writable filterLine​(java.io.InputStream self, Closure predicate)
      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.
      Parameters:
      self - an input stream
      predicate - a closure which returns boolean and takes a line
      Returns:
      a writable which writes out the filtered lines
      Since:
      1.0
      See Also:
      filterLine(java.io.Reader, groovy.lang.Closure)
    • filterLine

      public static Writable filterLine​(java.io.InputStream self, java.lang.String charset, Closure predicate) throws java.io.UnsupportedEncodingException
      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.
      Parameters:
      self - an input stream
      charset - opens the stream with a specified charset
      predicate - a closure which returns boolean and takes a line
      Returns:
      a writable which writes out the filtered lines
      Throws:
      java.io.UnsupportedEncodingException - if the encoding specified is not supported
      Since:
      1.6.8
      See Also:
      filterLine(java.io.Reader, groovy.lang.Closure)
    • filterLine

      public static void filterLine​(java.io.InputStream self, java.io.Writer writer, Closure predicate) throws java.io.IOException
      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.
      Parameters:
      self - the InputStream
      writer - a writer to write output to
      predicate - a closure which returns true if a line should be accepted
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.0
      See Also:
      filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
    • filterLine

      public static void filterLine​(java.io.InputStream self, java.io.Writer writer, java.lang.String charset, Closure predicate) throws java.io.IOException
      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.
      Parameters:
      self - the InputStream
      writer - a writer to write output to
      charset - opens the stream with a specified charset
      predicate - a closure which returns true if a line should be accepted
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      1.6.8
      See Also:
      filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
    • withCloseable

      public static <T,​ U extends java.io.Closeable> T withCloseable​(U self, Closure<T> action) throws java.io.IOException
      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.

      Parameters:
      self - the Closeable
      action - the closure taking the Closeable as parameter
      Returns:
      the value returned by the closure
      Throws:
      java.io.IOException - if an IOException occurs.
      Since:
      2.4.0
    • withCloseable

      public static <T,​ U extends java.lang.AutoCloseable> T withCloseable​(U self, Closure<T> action) throws java.lang.Exception
      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.

      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.

      Parameters:
      self - the AutoCloseable
      action - the closure taking the AutoCloseable as parameter
      Returns:
      the value returned by the closure
      Throws:
      java.lang.Exception - if an Exception occurs.
      Since:
      2.5.0