Class MarkableSequenceInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.postgresql.pljava.internal.MarkableSequenceInputStream
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class MarkableSequenceInputStream extends InputStream
Version ofSequenceInputStream
that supportsmark
andreset
, to the extent its constituent input streams do.This class implements
mark
andreset
by calling the corresponding methods on the underlying streams; it does not add buffering or have any means of providingmark
andreset
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 amark
has been set, so thatreset
will be possible. When a mark is no longer needed, it can be canceled (by callingmark
with areadlimit
of 0) to again allow the underlying streams to be reclaimed as soon as possible.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MarkableSequenceInputStream.FetchingListIterator<E>
AListIterator
that will fetch an element from aBlockingQueue
wheneverhasNext
would (otherwise) returnfalse
, adding it to the end of the list where the nextnext()
will retrieve it.
-
Field Summary
Fields Modifier and Type Field Description static InputStream
NO_MORE
A sentinel value, needed because aBlockingQueue
does not allow a null value to be enqueued.
-
Constructor Summary
Constructors Constructor Description MarkableSequenceInputStream(InputStream... streams)
Create aMarkableSequenceInputStream
from one or more existing input streams.MarkableSequenceInputStream(BlockingQueue<InputStream> queue)
AMarkableSequenceInputStream
that will receive streams to read, in order, over aBlockingQueue
.
-
Method Summary
Modifier and Type Method Description int
available()
void
close()
void
mark(int readlimit)
Marks the current position in this input stream.boolean
markSupported()
Tests if this input stream supports the mark and reset methods.int
read()
int
read(byte[] b, int off, int len)
void
reset()
long
skip(long n)
-
Methods inherited from class java.io.InputStream
nullInputStream, read, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
-
-
-
Field Detail
-
NO_MORE
public static final InputStream NO_MORE
A sentinel value, needed because aBlockingQueue
does not allow a null value to be enqueued.
-
-
Constructor Detail
-
MarkableSequenceInputStream
public MarkableSequenceInputStream(InputStream... streams)
Create aMarkableSequenceInputStream
from one or more existing input streams.- Parameters:
streams
- Sequence ofInputStream
s that will be read from in order.- Throws:
NullPointerException
- ifstreams
isnull
, or containsnull
for any stream.
-
MarkableSequenceInputStream
public MarkableSequenceInputStream(BlockingQueue<InputStream> queue)
AMarkableSequenceInputStream
that will receive streams to read, in order, over aBlockingQueue
.The thread supplying the queue should enqueue the value
NO_MORE
following the last actualInputStream
to read. (The sentinel is needed because aBlockingQueue
does not allow null values.)- Parameters:
queue
- Source of input streams to read.- Throws:
NullPointerException
- ifqueue
isnull
.
-
-
Method Detail
-
read
public int read() throws IOException
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
public int read(byte[] b, int off, int len) throws IOException
- Overrides:
read
in classInputStream
- Throws:
IOException
-
skip
public long skip(long n) throws IOException
- Overrides:
skip
in classInputStream
- Throws:
IOException
-
available
public int available() throws IOException
- Overrides:
available
in classInputStream
- Throws:
IOException
-
close
public void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
-
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 areadlimit
of zero, returning the stream immediately to the state of having no mark.- Overrides:
mark
in classInputStream
-
reset
public void reset() throws IOException
- Overrides:
reset
in classInputStream
- Throws:
IOException
-
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 asfalse
.- Overrides:
markSupported
in classInputStream
-
-