Summary
Implements a stream that exposes methods to transparently peek at the bytes that would be read by a call to
PeekableStream.Read(byte[], int, int),
without affecting the actual outcome of future calls to standard Stream methods. See Remarks for further info.
Remarks
In order for this stream to operate correctly, no direct reads, writes or seeks must be performed on the
underlying stream. All operations must be executed through this class.
Peeking will cause reads in the underlying stream, but this class ensures that the data is read only once,
in order to be compatible with pure (read-once, non-seekable) streams.
The class maintains that any operations on the streams returned by PeekableStream.GetPeekStream() do not modify
the outcome of any other operations performed on this stream, including the value of PeekableStream.Position, the bytes
read by PeekableStream.Read(byte[], int, int), or the effects of PeekableStream.Write(byte[], int, int). One visible side effect may be the change in chunk
size returned by a single call to PeekableStream.Read(byte[], int, int). This assumes that if the underlying stream is seekable then nothing
else changes the data stored in it.
Neither this class nor the peek streams returned by it are thread-safe. All accesses must occur on a single thread.
Constructors
Instance methods
void | | Flushes the underlying stream. |
PeekableStream.PeekStream | | Creates and returns a new peek stream linked to this stream. Reading from the returned stream allows
peeking at the bytes ahead of the current position in this stream, without changing the outcome of future
calls to any methods on this stream. The returned stream must be disposed of when done, since outstanding undisposed peek streams have a slight
performance impact on most operations on this peekable stream. See Remarks on PeekableStream for more info. |
int | Read(byte[] buffer, int offset, int count) |
Reads data from the stream into the specified buffer. Advances all peek streams that got overtaken by the
new stream position so as to continue reading from //
|
long | Seek(long offset, SeekOrigin origin) |
Seeks to the specified position in the underlying stream, if the underlying stream supports it. Note that
seeking causes all peek streams to be invalidated.
|
void | | Sets the length of the underlying stream, if supported by it. Note that setting the length causes
all peek streams to be invalidated. |
int | | Behaves like Read, except that the bytes are discarded. |
void | | Skips the specified number of bytes in the current stream, and throws EndOfStreamException
if the end of the stream is reached early. |
void | Write(byte[] buffer, int offset, int count) |
Writes data to the underlying stream. See Remarks for notes concerning seekable underlying streams.
If the underlying stream is not seekable, writes are assumed to be separate to reads and thus peeks, so
the peek streams aren't touched.
|
Instance properties
bool | | Indicates whether the underlying stream, and hence this stream, supports reading. |
bool | | Indicates whether the underlying stream, and hence this stream, supports seeking. |
bool | | Indicates whether the underlying stream, and hence this stream, supports writing. |
long | | Gets the length of the underlying stream, if supported by it. |
long | |
Gets or sets the current position in the stream, if supported by the underlying stream. Note that
seeking causes all peek streams to be reset so as to resume peeking from the point seeked to.
Note also that getting the current position requires a small amount of computation and should be used with care in tight loops.
|
Nested types
|
Reads on this stream are implemented as peeking into the parent stream. That is, the position of the parent stream
is unaffected when this stream is used to peek ahead of the current position in the parent stream.
|