Table of Contents

Class DataStream

Namespace
Yarhl.IO
Assembly
Yarhl.dll

Virtual Stream with substream capabilities and read/write abstraction layer.

public class DataStream : Stream, IAsyncDisposable, IDisposable
Inheritance
DataStream
Implements
Inherited Members

Remarks

The type is thread-safe at the level of the substream. For instance, it is safe to use several DataStream over the same base stream in parallel. The type is not thread-safe for its method. For instance, it is NOT safe to use the same DataStream in different threads at the same time.

Constructors

DataStream()

Initializes a new instance of the DataStream class. A new stream is created in memory.

public DataStream()

DataStream(Stream)

Initializes a new instance of the DataStream class.

public DataStream(Stream stream)

Parameters

stream Stream

Base stream.

Remarks

The dispose ownership is transferred to this stream.

DataStream(Stream, long, long)

Initializes a new instance of the DataStream class from a substream transferring the ownership of the life-cycle. In the case the stream is another DataStream the ownership is inherited.

public DataStream(Stream stream, long offset, long length)

Parameters

stream Stream

Base stream.

offset long

Offset from the base stream.

length long

Length of this substream.

DataStream(Stream, long, long, bool)

Initializes a new instance of the DataStream class.

public DataStream(Stream stream, long offset, long length, bool transferOwnership)

Parameters

stream Stream

Base stream.

offset long

Offset from the base stream.

length long

Length of this substream.

transferOwnership bool

Transfer the ownsership of the stream argument to this class so it can dispose it.

Properties

AbsolutePosition

Gets the position from the base stream.

public long AbsolutePosition { get; }

Property Value

long

ActiveStreams

Gets the number of streams in use.

public static int ActiveStreams { get; }

Property Value

int

BaseStream

Gets the base stream.

public Stream BaseStream { get; }

Property Value

Stream

CanRead

Gets a value indicating whether the current stream supports reading.

public override bool CanRead { get; }

Property Value

bool

CanSeek

Gets a value indicating whether the current stream supports seeking.

public override bool CanSeek { get; }

Property Value

bool

CanTimeout

Gets a value indicating whether the current stream support timeouts.

public override bool CanTimeout { get; }

Property Value

bool

CanWrite

Gets a value indicating whether the current stream supports writing.

public override bool CanWrite { get; }

Property Value

bool

Disposed

Gets a value indicating whether this DataStream is disposed.

public bool Disposed { get; }

Property Value

bool

EndOfStream

Gets a value indicating whether the position is at end of the stream.

public bool EndOfStream { get; }

Property Value

bool

Length

Gets the length of this stream.

public override long Length { get; }

Property Value

long

Offset

Gets the offset from the BaseStream.

public long Offset { get; }

Property Value

long

ParentDataStream

Gets the parent DataStream only if this stream was initialized from a DataStream.

public DataStream? ParentDataStream { get; }

Property Value

DataStream

Position

Gets or sets the position from the start of this stream.

public override long Position { get; set; }

Property Value

long

ReadTimeout

Gets or sets an invalid value as read time is not supported.

public override int ReadTimeout { get; set; }

Property Value

int

WriteTimeout

Gets or sets an invalid value as write time is not supported.

public override int WriteTimeout { get; set; }

Property Value

int

Methods

Compare(Stream)

Compare the content of the stream with another one.

public bool Compare(Stream otherStream)

Parameters

otherStream Stream

Stream to compare with.

Returns

bool

The result of the comparison.

Dispose(bool)

Releases all resource used by the DataStream object.

protected override void Dispose(bool disposing)

Parameters

disposing bool

If set to true free managed resources also.

Flush()

Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

public override void Flush()

PopPosition()

Pop the last position from the stack and move to it.

public void PopPosition()

PushCurrentPosition()

Push the current position into a stack.

public void PushCurrentPosition()

PushToPosition(long, SeekOrigin)

Push the current position into a stack and move to a new one.

public void PushToPosition(long shift, SeekOrigin mode = SeekOrigin.Begin)

Parameters

shift long

Distance to move position.

mode SeekOrigin

Mode to move position.

PushToPosition(long, SeekMode)

Push the current position into a stack and move to a new one.

[Obsolete("Use the overload with SeekOrigin.")]
public void PushToPosition(long shift, SeekMode mode)

Parameters

shift long

Distance to move position.

mode SeekMode

Mode to move position.

Read(byte[], int, int)

Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

Buffer to copy data.

offset int

Index to start copying in buffer.

count int

Maximum number of bytes to read.

Returns

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.

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.

public override int ReadByte()

Returns

int

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

RunInPosition(Action, long, SeekOrigin)

Run a method in a specific position. This command will move into the position, run the method and return to the current position.

public void RunInPosition(Action action, long position, SeekOrigin mode = SeekOrigin.Begin)

Parameters

action Action

Action to run.

position long

Position to move.

mode SeekOrigin

Mode to move position.

RunInPosition(Action, long, SeekMode)

Run a method in a specific position. This command will move into the position, run the method and return to the current position.

[Obsolete("Use the overload with SeekOrigin.")]
public void RunInPosition(Action action, long position, SeekMode mode)

Parameters

action Action

Action to run.

position long

Position to move.

mode SeekMode

Mode to move position.

Seek(long, SeekOrigin)

Move the position of the stream.

public override long Seek(long offset, SeekOrigin origin = SeekOrigin.Begin)

Parameters

offset long

Distance to move position.

origin SeekOrigin

Mode to move position.

Returns

long

The new position of the stream.

Seek(long, SeekMode)

Move the position of the Stream.

[Obsolete("Use the overload with SeekOrigin.")]
public void Seek(long shift, SeekMode mode)

Parameters

shift long

Distance to move position.

mode SeekMode

Mode to move position.

SetLength(long)

Sets the length of the current stream.

public override void SetLength(long value)

Parameters

value long

The new length value.

Slice(long)

Creates a substream starting in a defined position.

public DataStream Slice(long start)

Parameters

start long

Defined starting position.

Returns

DataStream

The substream defined by offset and length parameters.

Slice(long, long)

Creates a substream starting in a defined position and with a defined length.

public DataStream Slice(long start, long length)

Parameters

start long

Defined starting position.

length long

Defined length to be written.

Returns

DataStream

The substream defined by offset and length parameters.

Write(byte[], int, int)

Writes the a portion of the buffer to the stream.

public override void Write(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

Buffer to write.

offset int

Index in the buffer.

count int

Bytes to write.

WriteByte(byte)

Writes a byte.

public override void WriteByte(byte value)

Parameters

value byte

Byte value.

WriteSegmentTo(long, Stream)

Writes a segment of the stream into another stream from a defined position to the end.

public void WriteSegmentTo(long start, Stream stream)

Parameters

start long

Starting position to read from the current stream.

stream Stream

Output stream.

Remarks

It preserves the current position and writes to the current position of the destination stream.

WriteSegmentTo(long, long, Stream)

Writes a segment of the stream into another stream.

public void WriteSegmentTo(long start, long length, Stream stream)

Parameters

start long

Starting position to read from the current stream.

length long

Length of the segment to read.

stream Stream

Output stream.

Remarks

It preserves the current position and writes to the current position of the destination stream.

WriteSegmentTo(long, long, string)

Writes a segment of the stream into a file.

public void WriteSegmentTo(long start, long length, string fileOut)

Parameters

start long

Starting position to read from the current stream.

length long

Length of the segment to read.

fileOut string

Output file path.

Remarks

It preserves the current position and creates any required directory.

WriteSegmentTo(long, string)

Writes a segment of the stream into a file from a defined position to the end.

public void WriteSegmentTo(long start, string fileOut)

Parameters

start long

Starting position to read from the current stream.

fileOut string

Output file path.

Remarks

It preserves the current position and creates any required directory.

WriteTo(Stream)

Writes the complete stream into another stream preserving the current position.

public void WriteTo(Stream stream)

Parameters

stream Stream

The stream to write.

Remarks

This method is similar to CopyTo(Stream). The difference is that it copies always from the position 0 of the current stream, and it preserves the current position afterwards. It writes into the current position of the destination stream.

WriteTo(string)

Writes the complete stream into a file.

public void WriteTo(string fileOut)

Parameters

fileOut string

Output file path.

Remarks

It preserves the current position and creates any required directory.