Class TeeReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Readable

    public class TeeReader
    extends ProxyReader
    Reader proxy that transparently writes a copy of all characters read from the proxied reader to a given Reader. Using ProxyReader.skip(long) or ProxyReader.mark(int)/ProxyReader.reset() on the reader will result on some characters from the reader being skipped or duplicated in the writer.

    The proxied reader is closed when the close() method is called on this proxy. You may configure whether the reader closes the writer.

    Since:
    2.7
    • Constructor Summary

      Constructors 
      Constructor Description
      TeeReader​(java.io.Reader input, java.io.Writer branch)
      Creates a TeeReader that proxies the given Reader and copies all read characters to the given Writer.
      TeeReader​(java.io.Reader input, java.io.Writer branch, boolean closeBranch)
      Creates a TeeReader that proxies the given Reader and copies all read characters to the given Writer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the proxied reader and, if so configured, the associated writer.
      int read()
      Reads a single character from the proxied reader and writes it to the associated writer.
      int read​(char[] chr)
      Reads characters from the proxied reader and writes the read characters to the associated writer.
      int read​(char[] chr, int st, int end)
      Reads characters from the proxied reader and writes the read characters to the associated writer.
      int read​(java.nio.CharBuffer target)
      Reads characters from the proxied reader and writes the read characters to the associated writer.
      • Methods inherited from class java.io.Reader

        nullReader, transferTo
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TeeReader

        public TeeReader​(java.io.Reader input,
                         java.io.Writer branch)
        Creates a TeeReader that proxies the given Reader and copies all read characters to the given Writer. The given writer will not be closed when this reader gets closed.
        Parameters:
        input - reader to be proxied
        branch - writer that will receive a copy of all characters read
      • TeeReader

        public TeeReader​(java.io.Reader input,
                         java.io.Writer branch,
                         boolean closeBranch)
        Creates a TeeReader that proxies the given Reader and copies all read characters to the given Writer. The given writer will be closed when this reader gets closed if the closeBranch parameter is true.
        Parameters:
        input - reader to be proxied
        branch - writer that will receive a copy of all characters read
        closeBranch - flag for closing also the writer when this reader is closed
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Closes the proxied reader and, if so configured, the associated writer. An exception thrown from the reader will not prevent closing of the writer.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class ProxyReader
        Throws:
        java.io.IOException - if either the reader or writer could not be closed
      • read

        public int read()
                 throws java.io.IOException
        Reads a single character from the proxied reader and writes it to the associated writer.
        Overrides:
        read in class ProxyReader
        Returns:
        next character from the reader, or -1 if the reader has ended
        Throws:
        java.io.IOException - if the reader could not be read (or written)
      • read

        public int read​(char[] chr)
                 throws java.io.IOException
        Reads characters from the proxied reader and writes the read characters to the associated writer.
        Overrides:
        read in class ProxyReader
        Parameters:
        chr - character buffer
        Returns:
        number of characters read, or -1 if the reader has ended
        Throws:
        java.io.IOException - if the reader could not be read (or written)
      • read

        public int read​(char[] chr,
                        int st,
                        int end)
                 throws java.io.IOException
        Reads characters from the proxied reader and writes the read characters to the associated writer.
        Overrides:
        read in class ProxyReader
        Parameters:
        chr - character buffer
        st - start offset within the buffer
        end - maximum number of characters to read
        Returns:
        number of characters read, or -1 if the reader has ended
        Throws:
        java.io.IOException - if the reader could not be read (or written)
      • read

        public int read​(java.nio.CharBuffer target)
                 throws java.io.IOException
        Reads characters from the proxied reader and writes the read characters to the associated writer.
        Specified by:
        read in interface java.lang.Readable
        Overrides:
        read in class ProxyReader
        Parameters:
        target - character buffer
        Returns:
        number of characters read, or -1 if the reader has ended
        Throws:
        java.io.IOException - if the reader could not be read (or written)