Gorgon
Show / Hide Table of Contents

Class GorgonStreamWrapper

Wraps a parent stream object into a stream object with a smaller boundary.

Inheritance
object
MarshalByRefObject
Stream
GorgonStreamWrapper
Implements
IDisposable
Inherited Members
Stream.Null
Stream.CopyToAsync(Stream)
Stream.CopyToAsync(Stream, int)
Stream.CopyTo(Stream)
Stream.CopyTo(Stream, int)
Stream.Close()
Stream.Dispose()
Stream.Dispose(bool)
Stream.FlushAsync()
Stream.CreateWaitHandle()
Stream.ReadAsync(byte[], int, int)
Stream.WriteAsync(byte[], int, int)
Stream.Synchronized(Stream)
Stream.ObjectInvariant()
Stream.ReadTimeout
Stream.WriteTimeout
MarshalByRefObject.MemberwiseClone(bool)
MarshalByRefObject.GetLifetimeService()
MarshalByRefObject.InitializeLifetimeService()
MarshalByRefObject.CreateObjRef(Type)
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
Namespace: Gorgon.IO
Assembly: Gorgon.Core.dll
Syntax
public class GorgonStreamWrapper : Stream, IDisposable
Remarks

Sometimes it's necessary take a Stream and divide it into sections to ensure there are no overruns. This object will take an existing stream object and allows the user to set a start position within that stream, and give a range of readable bytes to break the parent stream into a smaller stream. This gives the protection that a stream provides against buffer overruns all while working within the confines of a smaller part of the parent stream.

When reading or writing, the parent stream will have its position manipulated by this stream to locate the start of this stream plus the current position of this stream. Once these operations are complete, the parent stream Position will be set back to the original position it was in prior to the read/write operation. This ensures that when this object is finished its work, the original parent position will remain unaffected by this stream.

note

It is not necessary to call the Dispose(bool) method or the Close() method on this object since there is nothing to close and ownership of the parent stream resides with the creator of that stream (i.e. this type does not take ownership of a parent stream). In fact, closing or disposing of this object does nothing.

caution

This object is not thread safe. If multiple wrappers are pointing to the same parent stream, and multiple threads use these wrappers, the read/write cursor will be desynchronized. Because of this limitation, all the asynchronous I/O operations will throw an exception.

Constructors

| Edit this page View Source

GorgonStreamWrapper(Stream)

Initializes a new instance of the GorgonStreamWrapper class.

Declaration
public GorgonStreamWrapper(Stream parentStream)
Parameters
Type Name Description
Stream parentStream

The parent of this stream.

Exceptions
Type Condition
ArgumentNullException

Thrown when the parentStream is null.

ArgumentException

Thrown when the CanSeek property on the parent stream is false.

| Edit this page View Source

GorgonStreamWrapper(Stream, long)

Initializes a new instance of the GorgonStreamWrapper class.

Declaration
public GorgonStreamWrapper(Stream parentStream, long streamStart)
Parameters
Type Name Description
Stream parentStream

The parent of this stream.

long streamStart

The position in the parent stream to start at, in bytes.

Exceptions
Type Condition
ArgumentNullException

Thrown when the parentStream is null.

ArgumentOutOfRangeException

Thrown when the streamStart parameter is less than 0.

-or-

The streamStart is larger than or equal to the size of the parentStream.

ArgumentException

Thrown when the CanSeek property on the parent stream is false.

| Edit this page View Source

GorgonStreamWrapper(Stream, long, long, bool)

Initializes a new instance of the GorgonStreamWrapper class.

Declaration
public GorgonStreamWrapper(Stream parentStream, long streamStart, long streamSize, bool allowWrite = true)
Parameters
Type Name Description
Stream parentStream

The parent of this stream.

long streamStart

The position in the parent stream to start at, in bytes.

long streamSize

The number of bytes to partition from the parent stream.

bool allowWrite

[Optional] true to allow writing to the stream, false to make the stream read-only.

Remarks

If writing to the stream, the streamSize is ignored.

Exceptions
Type Condition
ArgumentNullException

Thrown when the parentStream is null.

ArgumentOutOfRangeException

Thrown when the streamStart or the streamSize parameters are less than 0

ArgumentException

Thrown when the CanSeek property on the parent stream is false.

-or-

Thrown when the parentStream is read only and the allowWrite flag is set to true.

Properties

| Edit this page View Source

CanRead

When overridden in a derived class, gets a value indicating whether the current stream supports reading.

Declaration
public override bool CanRead { get; }
Property Value
Type Description
bool

true if the stream supports reading; otherwise, false.

Overrides
Stream.CanRead
| Edit this page View Source

CanSeek

When overridden in a derived class, gets a value indicating whether the current stream supports seeking.

Declaration
public override bool CanSeek { get; }
Property Value
Type Description
bool

true if the stream supports seeking; otherwise, false.

Overrides
Stream.CanSeek
| Edit this page View Source

CanTimeout

Gets a value that determines whether the current stream can time out.

Declaration
public override bool CanTimeout { get; }
Property Value
Type Description
bool

A value that determines whether the current stream can time out.

Overrides
Stream.CanTimeout
| Edit this page View Source

CanWrite

When overridden in a derived class, gets a value indicating whether the current stream supports writing.

Declaration
public override bool CanWrite { get; }
Property Value
Type Description
bool

true if the stream supports writing; otherwise, false.

Overrides
Stream.CanWrite
| Edit this page View Source

Length

When overridden in a derived class, gets the length in bytes of the stream.

Declaration
public override long Length { get; }
Property Value
Type Description
long

A long value representing the length of the stream in bytes.

Overrides
Stream.Length
Exceptions
Type Condition
NotSupportedException

A class derived from Stream does not support seeking.

ObjectDisposedException

Methods were called after the stream was closed.

| Edit this page View Source

ParentStream

Property to return the parent of this stream.

Declaration
public Stream ParentStream { get; }
Property Value
Type Description
Stream
| Edit this page View Source

Position

When overridden in a derived class, gets or sets the position within the current stream.

Declaration
public override long Position { get; set; }
Property Value
Type Description
long

The current position within the stream.

Overrides
Stream.Position
Exceptions
Type Condition
IOException

An I/O error occurs.

NotSupportedException

The stream does not support seeking.

ObjectDisposedException

Methods were called after the stream was closed.

Methods

| Edit this page View Source

BeginRead(byte[], int, int, AsyncCallback, object)

Begins an asynchronous read operation. (Consider using ReadAsync(byte[], int, int) instead; see the Remarks section.)

Declaration
[Obsolete("Use the ReadAsync method instead.")]
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
Parameters
Type Name Description
byte[] buffer

The buffer to read the data into.

int offset

The byte offset in buffer at which to begin writing data read from the stream.

int count

The maximum number of bytes to read.

AsyncCallback callback

An optional asynchronous callback, to be called when the read is complete.

object state

A user-provided object that distinguishes this particular asynchronous read request from other requests.

Returns
Type Description
IAsyncResult

An IAsyncResult that represents the asynchronous read, which could still be pending.

Overrides
Stream.BeginRead(byte[], int, int, AsyncCallback, object)
Exceptions
Type Condition
IOException

Attempted an asynchronous read past the end of the stream, or a disk error occurs.

ArgumentException

One or more of the arguments is invalid.

ObjectDisposedException

Methods were called after the stream was closed.

NotSupportedException

The current Stream implementation does not support the read operation.

NotSupportedException

This stream does not support asynchronous I/O with this method.

| Edit this page View Source

BeginWrite(byte[], int, int, AsyncCallback, object)

Begins an asynchronous write operation. (Consider using WriteAsync(byte[], int, int) instead; see the Remarks section.)

Declaration
[Obsolete("Use the WriteAsync method instead.")]
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
Parameters
Type Name Description
byte[] buffer

The buffer to write data from.

int offset

The byte offset in buffer from which to begin writing.

int count

The maximum number of bytes to write.

AsyncCallback callback

An optional asynchronous callback, to be called when the write is complete.

object state

A user-provided object that distinguishes this particular asynchronous write request from other requests.

Returns
Type Description
IAsyncResult

An IAsyncResult that represents the asynchronous write, which could still be pending.

Overrides
Stream.BeginWrite(byte[], int, int, AsyncCallback, object)
Exceptions
Type Condition
IOException

Attempted an asynchronous write past the end of the stream, or a disk error occurs.

ArgumentException

One or more of the arguments is invalid.

ObjectDisposedException

Methods were called after the stream was closed.

NotSupportedException

The current Stream implementation does not support the write operation.

NotSupportedException

This stream does not support asynchronous I/O with this method.

| Edit this page View Source

CopyToAsync(Stream, int, CancellationToken)

Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.

Declaration
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
Parameters
Type Name Description
Stream destination

The stream to which the contents of the current stream will be copied.

int bufferSize

The size, in bytes, of the buffer. This value must be greater than zero. The default size is 81920.

CancellationToken cancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns
Type Description
Task

A task that represents the asynchronous copy operation.

Overrides
Stream.CopyToAsync(Stream, int, CancellationToken)
Exceptions
Type Condition
ArgumentNullException

destination is null.

ArgumentOutOfRangeException

bufferSize is negative or zero.

ObjectDisposedException

Either the current stream or the destination stream is disposed.

NotSupportedException

The current stream does not support reading, or the destination stream does not support writing.

| Edit this page View Source

EndRead(IAsyncResult)

Waits for the pending asynchronous read to complete. (Consider using ReadAsync(byte[], int, int) instead; see the Remarks section.)

Declaration
[Obsolete("Use the ReadAsync method instead.")]
public override int EndRead(IAsyncResult asyncResult)
Parameters
Type Name Description
IAsyncResult asyncResult

The reference to the pending asynchronous request to finish.

Returns
Type Description
int

The number of bytes read from the stream, between zero (0) and the number of bytes you requested. Streams return zero (0) only at the end of the stream, otherwise, they should block until at least one byte is available.

Overrides
Stream.EndRead(IAsyncResult)
Exceptions
Type Condition
ArgumentNullException

asyncResult is null.

ArgumentException

A handle to the pending read operation is not available.-or-The pending operation does not support reading.

InvalidOperationException

asyncResult did not originate from a BeginRead(byte[], int, int, AsyncCallback, object) method on the current stream.

IOException

The stream is closed or an internal error has occurred.

NotSupportedException

This stream does not support asynchronous I/O with this method.

| Edit this page View Source

EndWrite(IAsyncResult)

Ends an asynchronous write operation. (Consider using WriteAsync(byte[], int, int) instead; see the Remarks section.)

Declaration
[Obsolete("Use the WriteAsync method instead.")]
public override void EndWrite(IAsyncResult asyncResult)
Parameters
Type Name Description
IAsyncResult asyncResult

A reference to the outstanding asynchronous I/O request.

Overrides
Stream.EndWrite(IAsyncResult)
Exceptions
Type Condition
ArgumentNullException

asyncResult is null.

ArgumentException

A handle to the pending write operation is not available.-or-The pending operation does not support writing.

InvalidOperationException

asyncResult did not originate from a BeginWrite(byte[], int, int, AsyncCallback, object) method on the current stream.

IOException

The stream is closed or an internal error has occurred.

NotSupportedException

This stream does not support asynchronous I/O with this method.

| Edit this page View Source

Flush()

When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

Declaration
public override void Flush()
Overrides
Stream.Flush()
Exceptions
Type Condition
IOException

An I/O error occurs.

| Edit this page View Source

FlushAsync(CancellationToken)

Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.

Declaration
public override Task FlushAsync(CancellationToken cancellationToken)
Parameters
Type Name Description
CancellationToken cancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns
Type Description
Task

A task that represents the asynchronous flush operation.

Overrides
Stream.FlushAsync(CancellationToken)
Exceptions
Type Condition
ObjectDisposedException

The stream has been disposed.

| Edit this page View Source

Read(byte[], int, int)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

Declaration
public override int Read(byte[] buffer, int offset, int count)
Parameters
Type Name Description
byte[] buffer

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

int offset

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

int count

The maximum number of bytes to be read from the current stream.

Returns
Type Description
int

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

Overrides
Stream.Read(byte[], int, int)
Exceptions
Type Condition
ArgumentException

The sum of offset and count is larger than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurs.

NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

| Edit this page View Source

ReadAsync(byte[], int, int, CancellationToken)

Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

Declaration
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Parameters
Type Name Description
byte[] buffer

The buffer to write the data into.

int offset

The byte offset in buffer at which to begin writing data from the stream.

int count

The maximum number of bytes to read.

CancellationToken cancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns
Type Description
Task<int>

A task that represents the asynchronous read operation. The value of the count parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

Overrides
Stream.ReadAsync(byte[], int, int, CancellationToken)
Exceptions
Type Condition
ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

The sum of offset and count is larger than the buffer length.

NotSupportedException

The stream does not support reading.

ObjectDisposedException

The stream has been disposed.

InvalidOperationException

The stream is currently in use by a previous read operation.

| Edit this page View Source

ReadByte()

Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

Declaration
public override int ReadByte()
Returns
Type Description
int

The unsigned byte cast to an Int32, or -1 if at the end of the stream.

Overrides
Stream.ReadByte()
Exceptions
Type Condition
NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

| Edit this page View Source

Seek(long, SeekOrigin)

When overridden in a derived class, sets the position within the current stream.

Declaration
public override long Seek(long offset, SeekOrigin origin)
Parameters
Type Name Description
long offset

A byte offset relative to the origin parameter.

SeekOrigin origin

A value of type SeekOrigin indicating the reference point used to obtain the new position.

Returns
Type Description
long

The new position within the current stream.

Overrides
Stream.Seek(long, SeekOrigin)
| Edit this page View Source

SetLength(long)

When overridden in a derived class, sets the length of the current stream.

Declaration
public override void SetLength(long value)
Parameters
Type Name Description
long value

The desired length of the current stream in bytes.

Overrides
Stream.SetLength(long)
Exceptions
Type Condition
IOException

An I/O error occurs.

NotSupportedException

The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.

ObjectDisposedException

Methods were called after the stream was closed.

| Edit this page View Source

Write(byte[], int, int)

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

Declaration
public override void Write(byte[] buffer, int offset, int count)
Parameters
Type Name Description
byte[] buffer

An array of bytes. This method copies count bytes from buffer to the current stream.

int offset

The zero-based byte offset in buffer at which to begin copying bytes to the current stream.

int count

The number of bytes to be written to the current stream.

Overrides
Stream.Write(byte[], int, int)
Exceptions
Type Condition
ArgumentException

The sum of offset and count is greater than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occured, such as the specified file cannot be found.

NotSupportedException

The stream does not support writing.

ObjectDisposedException

Write(byte[], int, int) was called after the stream was closed.

| Edit this page View Source

WriteAsync(byte[], int, int, CancellationToken)

Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

Declaration
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Parameters
Type Name Description
byte[] buffer

The buffer to write data from.

int offset

The zero-based byte offset in buffer from which to begin copying bytes to the stream.

int count

The maximum number of bytes to write.

CancellationToken cancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns
Type Description
Task

A task that represents the asynchronous write operation.

Overrides
Stream.WriteAsync(byte[], int, int, CancellationToken)
Exceptions
Type Condition
ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

The sum of offset and count is larger than the buffer length.

NotSupportedException

The stream does not support writing.

ObjectDisposedException

The stream has been disposed.

InvalidOperationException

The stream is currently in use by a previous write operation.

| Edit this page View Source

WriteByte(byte)

Writes a byte to the current position in the stream and advances the position within the stream by one byte.

Declaration
public override void WriteByte(byte value)
Parameters
Type Name Description
byte value

The byte to write to the stream.

Overrides
Stream.WriteByte(byte)
Exceptions
Type Condition
IOException

An I/O error occurs.

NotSupportedException

The stream does not support writing, or the stream is already closed.

ObjectDisposedException

Methods were called after the stream was closed.

Implements

IDisposable

Extension Methods

GorgonDebugExtensions.ValidateObject<T>(T, string)
GorgonIOExtensions.CopyToStream(Stream, Stream, int, byte[])
GorgonIOExtensions.CopyToStream(Stream, Stream, int, int)
GorgonIOExtensions.Read(Stream, Span<byte>)
GorgonIOExtensions.ReadString(Stream)
GorgonIOExtensions.ReadString(Stream, Encoding)
GorgonIOExtensions.Write(Stream, ReadOnlySpan<byte>)
GorgonIOExtensions.WriteString(Stream, string)
GorgonIOExtensions.WriteString(Stream, string, Encoding)
GorgonNativeExtensions.CopyTo<T>(Stream, GorgonPtr<T>, int, int?)
GorgonNativeExtensions.ToNativeBuffer<T>(Stream, int?)
GorgonNullExtensions.AsNullable<T>(object)
GorgonNullExtensions.IfNull<T>(object, T)
GorgonNullExtensions.IsNull(object)
  • Edit this page
  • View Source
In this article
Back to top Copyright 2023 - Licensed under the MIT license by Michael Winsor (Tape_Worm).
Send comments on this topic to the author