Class ConcatReader

java.lang.Object
java.io.Reader
com.Ostermiller.util.ConcatReader
All Implemented Interfaces:
Closeable, AutoCloseable, Readable

public class ConcatReader extends Reader
A reader which reads sequentially from multiple sources. More information about this class is available from ostermiller.org.
Since:
ostermillerutils 1.04.00
Author:
Stephen Ostermiller https://ostermiller.org/contact.pl?regarding=Java+Utilities
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new reader that can dynamically accept new sources.
    Create a new reader with one source.
    Create a new reader with an arbitrary number of sources.
    Create a new reader with two sources.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Add the given reader to the queue of readers from which to concatenate data.
    void
    Add the given reader to the queue of readers from which to concatenate data.
    void
    Close the stream and any underlying streams.
    void
    Causes the addReader method to throw IllegalStateException and read() methods to return -1 (end of stream) when there is no more available data.
    void
    mark(int readlimit)
    Mark not supported.
    boolean
    Mark not supported.
    int
    Read a single character.
    int
    read(char[] cbuf)
    Read characters into an array.
    int
    read(char[] cbuf, int off, int len)
    Read characters into a portion of an array.
    boolean
    Tell whether this stream is ready to be read.
    void
    Reset not supported.
    long
    skip(long n)
    Skip characters.

    Methods inherited from class java.io.Reader

    nullReader, read, transferTo

    Methods inherited from class java.lang.Object

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

    • ConcatReader

      public ConcatReader()
      Create a new reader that can dynamically accept new sources.

      New sources should be added using the addReader() method. When all sources have been added the lastReaderAdded() should be called so that read methods can return -1 (end of stream).

      Adding new sources may by interleaved with read calls.

      Since:
      ostermillerutils 1.04.01
    • ConcatReader

      public ConcatReader(Reader in)
      Create a new reader with one source.

      When using this constructor, more readers cannot be added later, and calling addReader() will throw an illegal state Exception.

      Parameters:
      in - reader to use as a source.
      Throws:
      NullPointerException - if in is null
      Since:
      ostermillerutils 1.04.00
    • ConcatReader

      public ConcatReader(Reader in1, Reader in2)
      Create a new reader with two sources.

      When using this constructor, more readers cannot be added later, and calling addReader() will throw an illegal state Exception.

      Parameters:
      in1 - first reader to use as a source.
      in2 - second reader to use as a source.
      Throws:
      NullPointerException - if either source is null.
      Since:
      ostermillerutils 1.04.00
    • ConcatReader

      public ConcatReader(Reader[] in)
      Create a new reader with an arbitrary number of sources.

      When using this constructor, more readers cannot be added later, and calling addReader() will throw an illegal state Exception.

      Parameters:
      in - readers to use as a sources.
      Throws:
      NullPointerException - if the input array on any element is null.
      Since:
      ostermillerutils 1.04.00
  • Method Details

    • lastReaderAdded

      public void lastReaderAdded()
      Causes the addReader method to throw IllegalStateException and read() methods to return -1 (end of stream) when there is no more available data.

      Calling this method when this class is no longer accepting more readers has no effect.

      Since:
      ostermillerutils 1.04.01
    • addReader

      public void addReader(Reader in)
      Add the given reader to the queue of readers from which to concatenate data.
      Parameters:
      in - Reader to add to the concatenation.
      Throws:
      IllegalStateException - if more readers can't be added because lastReaderAdded() has been called, close() has been called, or a constructor with reader parameters was used.
      Since:
      ostermillerutils 1.04.01
    • addReaders

      public void addReaders(Reader[] in)
      Add the given reader to the queue of readers from which to concatenate data.
      Parameters:
      in - Reader to add to the concatenation.
      Throws:
      IllegalStateException - if more readers can't be added because lastReaderAdded() has been called, close() has been called, or a constructor with reader parameters was used.
      NullPointerException - the array of readers, or any of the contents is null.
      Since:
      ostermillerutils 1.04.01
    • read

      public int read() throws IOException
      Read a single character. This method will block until a character is available, an I/O error occurs, or the end of all underlying streams are reached.

      If this class in not done accepting readers and the end of the last known stream is reached, this method will block forever unless another thread adds a reader or interrupts.

      Overrides:
      read in class Reader
      Returns:
      The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached
      Throws:
      IOException - - If an I/O error occurs
      Since:
      ostermillerutils 1.04.00
    • read

      public int read(char[] cbuf) throws IOException
      Read characters into an array. This method will block until some input is available, an I/O error occurs, or the end of all underlying streams are reached.

      If this class in not done accepting readers and the end of the last known stream is reached, this method will block forever unless another thread adds a reader or interrupts.

      Overrides:
      read in class Reader
      Parameters:
      cbuf - - Destination buffer
      Returns:
      The number of characters read, or -1 if the end of the stream has been reached
      Throws:
      IOException - - If an I/O error occurs
      NullPointerException - - If the buffer is null.
      Since:
      ostermillerutils 1.04.00
    • read

      public int read(char[] cbuf, int off, int len) throws IOException
      Read characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of all underlying streams are reached.

      If this class in not done accepting readers and the end of the last known stream is reached, this method will block forever unless another thread adds a reader or interrupts.

      Specified by:
      read in class Reader
      Parameters:
      cbuf - Destination buffer
      off - Offset at which to start storing characters
      len - Maximum number of characters to read
      Returns:
      The number of characters read, or -1 if the end of the stream has been reached
      Throws:
      IOException - - If an I/O error occurs
      NullPointerException - - If the buffer is null.
      IndexOutOfBoundsException - - if length or offset are not possible.
      Since:
      ostermillerutils 1.04.00
    • skip

      public long skip(long n) throws IOException
      Skip characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached.

      If this class in not done accepting readers and the end of the last known stream is reached, this method will block forever unless another thread adds a reader or interrupts.

      Overrides:
      skip in class Reader
      Parameters:
      n - the number of characters to skip
      Returns:
      The number of characters actually skipped
      Throws:
      IllegalArgumentException - If n is negative.
      IOException - If an I/O error occurs
      Since:
      ostermillerutils 1.04.00
    • ready

      public boolean ready() throws IOException
      Tell whether this stream is ready to be read.
      Overrides:
      ready in class Reader
      Returns:
      True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
      Throws:
      IOException - If an I/O error occurs
      Since:
      ostermillerutils 1.04.00
    • close

      public void close() throws IOException
      Close the stream and any underlying streams. Once a stream has been closed, further read(), ready(), mark(), or reset() invocations will throw an IOException. Closing a previously-closed stream, however, has no effect.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Reader
      Throws:
      IOException - If an I/O error occurs
      Since:
      ostermillerutils 1.04.00
    • mark

      public void mark(int readlimit) throws IOException
      Mark not supported.
      Overrides:
      mark in class Reader
      Throws:
      IOException - because mark is not supported.
      Since:
      ostermillerutils 1.04.00
    • reset

      public void reset() throws IOException
      Reset not supported.
      Overrides:
      reset in class Reader
      Throws:
      IOException - because reset is not supported.
      Since:
      ostermillerutils 1.04.00
    • markSupported

      public boolean markSupported()
      Mark not supported.
      Overrides:
      markSupported in class Reader
      Returns:
      false
      Since:
      ostermillerutils 1.04.00