Class MarkableSequenceInputStream

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class MarkableSequenceInputStream
    extends InputStream
    Version of SequenceInputStream that supports mark and reset, to the extent its constituent input streams do.

    This class implements mark and reset by calling the corresponding methods on the underlying streams; it does not add buffering or have any means of providing mark and reset support if the underlying streams do not.

    As with SequenceInputStream, each underlying stream, when completely read and no longer needed, is closed to free resources. This instance itself will remain in "open at EOF" condition until explicitly closed, but does not prevent reclamation of the underlying streams.

    Unlike SequenceInputStream, this class can keep underlying streams open, after fully reading them, if a mark has been set, so that reset will be possible. When a mark is no longer needed, it can be canceled (by calling mark with a readlimit of 0) to again allow the underlying streams to be reclaimed as soon as possible.

    • Field Detail

      • NO_MORE

        public static final InputStream NO_MORE
        A sentinel value, needed because a BlockingQueue does not allow a null value to be enqueued.
    • Constructor Detail

      • MarkableSequenceInputStream

        public MarkableSequenceInputStream​(InputStream... streams)
        Create a MarkableSequenceInputStream from one or more existing input streams.
        Parameters:
        streams - Sequence of InputStreams that will be read from in order.
        Throws:
        NullPointerException - if streams is null, or contains null for any stream.
      • MarkableSequenceInputStream

        public MarkableSequenceInputStream​(BlockingQueue<InputStream> queue)
        A MarkableSequenceInputStream that will receive streams to read, in order, over a BlockingQueue.

        The thread supplying the queue should enqueue the value NO_MORE following the last actual InputStream to read. (The sentinel is needed because a BlockingQueue does not allow null values.)

        Parameters:
        queue - Source of input streams to read.
        Throws:
        NullPointerException - if queue is null.
    • Method Detail

      • mark

        public void mark​(int readlimit)
        Marks the current position in this input stream. In this implementation, it is possible to 'cancel' a mark, by passing this method a readlimit of zero, returning the stream immediately to the state of having no mark.
        Overrides:
        mark in class InputStream
      • markSupported

        public boolean markSupported()
        Tests if this input stream supports the mark and reset methods.

        By the API spec, this method's return is "an invariant property of a particular input stream instance." For any instance of this class, the result is determined by the first call to this method, and does not change thereafter. At the first call, the result is determined only by the underlying input streams remaining to be read (or, if a mark has been set, which is possible before checking this method, then by the underlying input streams including and following the one that was current when the mark was set). The result will be true unless any of those underlying streams reports it as false.

        Overrides:
        markSupported in class InputStream