Package: java.io

## [Java] Class InputStream

- java.io.InputStream

-     public class InputStream
      extends Object

  GDK enhancements for InputStream.

  displayName:
    InputStream
  receiverFqcn:
    java.io.InputStream

- - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `eachByte(Closure closure)` Traverse through each byte of the specified stream.
    `` | `public void` | `eachByte(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 T` | `eachLine(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 T` | `eachLine(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 T` | `eachLine(Closure<T> closure)` Iterates through this stream, passing each line to the given 1 or 2 arg closure.
    `<T>` | `public T` | `eachLine(int firstLine, Closure<T> closure)` Iterates through this stream, passing each line to the given 1 or 2 arg closure.
    `` | `public Writable` | `filterLine(Closure predicate)` Filter lines from an input stream using a closure predicate.
    `` | `public Writable` | `filterLine(String charset, Closure predicate)` Filter lines from an input stream using a closure predicate.
    `` | `public void` | `filterLine(Writer writer, Closure predicate)` Uses a closure to filter lines from this InputStream and pass them to the given writer.
    `` | `public void` | `filterLine(Writer writer, String charset, Closure predicate)` Uses a closure to filter lines from this InputStream and pass them to the given writer.
    `` | `public byte[]` | `getBytes()` Read the content of this InputStream and return it as a byte[].
    `` | `public String` | `getText()` Read the content of this InputStream and return it as a String.
    `` | `public String` | `getText(String charset)` Read the content of this InputStream using specified charset and return it as a String.
    `` | `public Iterator<Byte>` | `iterator()` Standard iterator for an input stream which iterates through the stream content in a byte-based fashion.
    `` | `public ObjectInputStream` | `newObjectInputStream()` Create an object input stream for this input stream.
    `` | `public ObjectInputStream` | `newObjectInputStream(ClassLoader classLoader)` Create an object input stream for this input stream using the given class loader.
    `` | `public BufferedReader` | `newReader()` Creates a reader for this input stream.
    `` | `public BufferedReader` | `newReader(String charset)` Creates a reader for this input stream, using the specified charset as the encoding.
    `` | `public List<String>` | `readLines()` Reads the stream into a list, with one element for each line.
    `` | `public List<String>` | `readLines(String charset)` Reads the stream into a list, with one element for each line.
    `<T>` | `public T` | `splitEachLine(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 T` | `splitEachLine(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 T` | `splitEachLine(String regex, Closure<T> closure)` Iterates through the given InputStream line by line, splitting each line using the given separator.
    `<T>` | `public T` | `splitEachLine(Pattern pattern, Closure<T> closure)` Iterates through the given InputStream line by line, splitting each line using the given separator Pattern.
    `<T>` | `public T` | `withObjectInputStream(Closure<T> closure)` Create a new ObjectInputStream for this input stream and pass it to the closure.
    `<T>` | `public T` | `withObjectInputStream(ClassLoader classLoader, Closure<T> closure)` Create a new ObjectInputStream for this input stream and pass it to the closure.
    `<T>` | `public T` | `withReader(Closure<T> closure)` Helper method to create a new Reader for a stream and then passes it into the closure.
    `<T>` | `public T` | `withReader(String charset, Closure<T> closure)` Helper method to create a new Reader for a stream and then passes it into the closure.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `addShutdownHook, any, any, asBoolean, asType, clamp, clamp, collect, collect, collect, dump, each, eachMatch, eachMatch, eachWithIndex, every, every, find, find, findAll, findAll, findIndexOf, findIndexOf, findIndexValues, findIndexValues, findLastIndexOf, findLastIndexOf, findResult, findResult, findResult, findResult, getAt, getMetaClass, getMetaPropertyValues, getProperties, grep, grep, hasProperty, identity, inject, inject, inject, inject, inspect, invokeMethod, is, isCase, isNotCase, iterator, metaClass, print, print, printf, printf, println, println, println, putAt, respondsTo, respondsTo, setMetaClass, sleep, sleep, split, sprintf, sprintf, stream, takeIf, tap, toString, use, use, use, with, with, withCloseable, withCloseable, withMethodClosure, withStream, withStream, withTraits`

- - ### Method Detail

    - #### public void eachByte(Closure closure)

      Traverse through each byte of the specified stream. The stream is closed after the closure returns.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `closure` - closure to apply to each byte
      Since:
        1.0

    - #### public void eachByte(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.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `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.
      Since:
        1.8

    - #### <T> public T eachLine(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. The stream is closed before this method returns.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `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
      See Also:
        eachLine(java.io.InputStream, java.lang.String, int, groovy.lang.Closure)
      Since:
        1.5.5

    - #### <T> public T eachLine(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. The stream is closed after this method returns.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `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
      See Also:
        eachLine(java.io.Reader, int, groovy.lang.Closure)
      Since:
        1.5.7

    - #### <T> public T eachLine(Closure<T> closure)

      Iterates through this stream, passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `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
      See Also:
        eachLine(java.io.InputStream, int, groovy.lang.Closure)
      Since:
        1.5.6

    - #### <T> public T eachLine(int firstLine, Closure<T> closure)

      Iterates through this stream, passing each line to the given 1 or 2 arg closure. The stream is closed before this method returns.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `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
      See Also:
        eachLine(java.io.Reader, int, groovy.lang.Closure)
      Since:
        1.5.7

    - #### public Writable filterLine(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:
        `predicate` - a closure which returns boolean and takes a line
      Returns:
        a writable which writes out the filtered lines
      See Also:
        filterLine(java.io.Reader, groovy.lang.Closure)
      Since:
        1.0

    - #### public Writable filterLine(String charset, 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.

      throws:
        UnsupportedEncodingException if the encoding specified is not supported
      Parameters:
        `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
      See Also:
        filterLine(java.io.Reader, groovy.lang.Closure)
      Since:
        1.6.8

    - #### public void filterLine(Writer writer, Closure predicate)

      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.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `writer` - a writer to write output to
        `predicate` - a closure which returns true if a line should be accepted
      See Also:
        filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
      Since:
        1.0

    - #### public void filterLine(Writer writer, String charset, Closure predicate)

      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.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `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
      See Also:
        filterLine(java.io.Reader, java.io.Writer, groovy.lang.Closure)
      Since:
        1.6.8

    - #### public byte[] getBytes()

      Read the content of this InputStream and return it as a byte[]. The stream is closed before this method returns.

      throws:
        IOException if an IOException occurs.
      Returns:
        the byte[] from that InputStream
      Since:
        1.7.1

    - #### public String getText()

      Read the content of this InputStream and return it as a String. The stream is closed before this method returns.

      throws:
        IOException if an IOException occurs.
      Returns:
        the text from that URL
      Since:
        1.0

    - #### public String getText(String charset)

      Read the content of this InputStream using specified charset and return it as a String. The stream is closed before this method returns.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `charset` - opens the stream with a specified charset
      Returns:
        the text from that URL
      Since:
        1.0

    - #### public Iterator<Byte> iterator()

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

      Returns:
        an Iterator for the InputStream
      Since:
        1.5.0

    - #### public ObjectInputStream newObjectInputStream()

      Create an object input stream for this input stream.

      throws:
        IOException if an IOException occurs.
      Returns:
        an object input stream
      Since:
        1.5.0

    - #### public ObjectInputStream newObjectInputStream(ClassLoader classLoader)

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

      throws:
        IOException if an IOException occurs.
      Parameters:
        `classLoader` - the class loader to use when loading the class
      Returns:
        an object input stream
      Since:
        1.5.0

    - #### public BufferedReader newReader()

      Creates a reader for this input stream.

      Returns:
        a reader
      Since:
        1.0

    - #### public BufferedReader newReader(String charset)

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

      throws:
        UnsupportedEncodingException if the encoding specified is not supported
      Parameters:
        `charset` - the charset for this input stream
      Returns:
        a reader
      Since:
        1.6.0

    - #### public List<String> readLines()

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

      throws:
        IOException if an IOException occurs.
      Returns:
        a List of lines
      See Also:
        readLines(java.io.Reader)
      Since:
        1.0

    - #### public List<String> readLines(String charset)

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

      throws:
        IOException if an IOException occurs.
      Parameters:
        `charset` - opens the stream with a specified charset
      Returns:
        a List of lines
      See Also:
        readLines(java.io.Reader)
      Since:
        1.6.8

    - #### <T> public T splitEachLine(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. The list of tokens for each line is then passed to the given closure. Finally, the stream is closed.

      throws:
        IOException if an IOException occurs.
      throws:
        java.util.regex.PatternSyntaxException if the regular expression's syntax is invalid
      Parameters:
        `regex` - the delimiting regular expression
        `charset` - opens the stream with a specified charset
        `closure` - a closure
      Returns:
        the last value returned by the closure
      See Also:
        splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)
      Since:
        1.5.5

    - #### <T> public T splitEachLine(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. The list of tokens for each line is then passed to the given closure. Finally, the stream is closed.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `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
      See Also:
        splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
      Since:
        1.6.8

    - #### <T> public T splitEachLine(String regex, Closure<T> closure)

      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.

      throws:
        IOException if an IOException occurs.
      throws:
        java.util.regex.PatternSyntaxException if the regular expression's syntax is invalid
      Parameters:
        `regex` - the delimiting regular expression
        `closure` - a closure
      Returns:
        the last value returned by the closure
      See Also:
        splitEachLine(java.io.Reader, java.lang.String, groovy.lang.Closure)
      Since:
        1.5.6

    - #### <T> public T splitEachLine(Pattern pattern, Closure<T> closure)

      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.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `pattern` - the regular expression Pattern for the delimiter
        `closure` - a closure
      Returns:
        the last value returned by the closure
      See Also:
        splitEachLine(java.io.Reader, java.util.regex.Pattern, groovy.lang.Closure)
      Since:
        1.6.8

    - #### <T> public T withObjectInputStream(Closure<T> closure)

      Create a new ObjectInputStream for this input stream and pass it to the closure. This method ensures the stream is closed after the closure returns.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `closure` - a closure
      Returns:
        the value returned by the closure
      See Also:
        withStream(java.io.InputStream, groovy.lang.Closure)
      Since:
        1.5.0

    - #### <T> public T withObjectInputStream(ClassLoader classLoader, Closure<T> closure)

      Create a new ObjectInputStream for this input stream and pass it to the closure. This method ensures the stream is closed after the closure returns.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `classLoader` - the class loader to use when loading the class
        `closure` - a closure
      Returns:
        the value returned by the closure
      See Also:
        withStream(java.io.InputStream, groovy.lang.Closure)
      Since:
        1.5.0

    - #### <T> public T withReader(Closure<T> closure)

      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.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `closure` - the closure to invoke with the InputStream
      Returns:
        the value returned by the closure
      See Also:
        InputStreamReader
      Since:
        1.5.2

    - #### <T> public T withReader(String charset, Closure<T> closure)

      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.

      throws:
        IOException if an IOException occurs.
      Parameters:
        `charset` - the charset used to decode the stream
        `closure` - the closure to invoke with the reader
      Returns:
        the value returned by the closure
      See Also:
        InputStreamReader
      Since:
        1.5.6