Class UnionInputStream

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

    public class UnionInputStream
    extends java.io.InputStream
    An InputStream which reads from one or more InputStreams.

    This stream may enter into an EOF state, returning -1 from any of the read methods, and then later successfully read additional bytes if a new InputStream is added after reaching EOF.

    Currently this stream does not support the mark/reset APIs. If mark and later reset functionality is needed the caller should wrap this stream with a BufferedInputStream.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.io.InputStream EOF  
      private java.util.LinkedList<java.io.InputStream> streams  
    • Constructor Summary

      Constructors 
      Constructor Description
      UnionInputStream()
      Create an empty InputStream that is currently at EOF state.
      UnionInputStream​(java.io.InputStream... inputStreams)
      Create an InputStream that is a union of the individual streams.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(java.io.InputStream in)
      Add the given InputStream onto the end of the stream queue.
      int available()
      void close()
      private java.io.InputStream head()  
      boolean isEmpty()
      Returns true if there are no more InputStreams in the stream queue.
      private void pop()  
      int read()
      int read​(byte[] b, int off, int len)
      long skip​(long count)
      • Methods inherited from class java.io.InputStream

        mark, markSupported, read, reset
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EOF

        private static final java.io.InputStream EOF
      • streams

        private final java.util.LinkedList<java.io.InputStream> streams
    • Constructor Detail

      • UnionInputStream

        public UnionInputStream()
        Create an empty InputStream that is currently at EOF state.
      • UnionInputStream

        public UnionInputStream​(java.io.InputStream... inputStreams)
        Create an InputStream that is a union of the individual streams.

        As each stream reaches EOF, it will be automatically closed before bytes from the next stream are read.

        Parameters:
        inputStreams - streams to be pushed onto this stream.
    • Method Detail

      • head

        private java.io.InputStream head()
      • pop

        private void pop()
                  throws java.io.IOException
        Throws:
        java.io.IOException
      • add

        public void add​(java.io.InputStream in)
        Add the given InputStream onto the end of the stream queue.

        When the stream reaches EOF it will be automatically closed.

        Parameters:
        in - the stream to add; must not be null.
      • isEmpty

        public boolean isEmpty()
        Returns true if there are no more InputStreams in the stream queue.

        If this method returns true then all read methods will signal EOF by returning -1, until another InputStream has been pushed into the queue with add(InputStream).

        Returns:
        true if there are no more streams to read from.
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long count)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException