Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Class: RT.KitchenSink.Streams.PeekableStream

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

PeekableStream(Stream underlyingStream)
Constructor.

Instance methods

void
  • Overrides: Stream.Flush()
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)
  • Overrides: Stream.Read(byte[], int, int)
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)
  • Overrides: Stream.Seek(long, SeekOrigin)
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
SetLength(long value)
  • Overrides: Stream.SetLength(long)
Sets the length of the underlying stream, if supported by it. Note that setting the length causes all peek streams to be invalidated.
int
Skip(int count)
  • Virtual
Behaves like Read, except that the bytes are discarded.
void
SkipExactly(int count)
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)
  • Overrides: Stream.Write(byte[], int, int)
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. (see also remarks)

Instance properties

bool
  • Overrides: Stream.CanRead
Indicates whether the underlying stream, and hence this stream, supports reading.
bool
  • Overrides: Stream.CanSeek
Indicates whether the underlying stream, and hence this stream, supports seeking.
bool
  • Overrides: Stream.CanWrite
Indicates whether the underlying stream, and hence this stream, supports writing.
long
  • Overrides: Stream.Length
Gets the length of the underlying stream, if supported by it.
long
  • Overrides: Stream.Position
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.