Class DataStream
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
StreamBase 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
StreamBase stream.
offset
longOffset from the base stream.
length
longLength 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
StreamBase stream.
offset
longOffset from the base stream.
length
longLength of this substream.
transferOwnership
boolTransfer 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
ActiveStreams
Gets the number of streams in use.
public static int ActiveStreams { get; }
Property Value
BaseStream
Gets the base stream.
public Stream BaseStream { get; }
Property Value
CanRead
Gets a value indicating whether the current stream supports reading.
public override bool CanRead { get; }
Property Value
CanSeek
Gets a value indicating whether the current stream supports seeking.
public override bool CanSeek { get; }
Property Value
CanTimeout
Gets a value indicating whether the current stream support timeouts.
public override bool CanTimeout { get; }
Property Value
CanWrite
Gets a value indicating whether the current stream supports writing.
public override bool CanWrite { get; }
Property Value
Disposed
Gets a value indicating whether this DataStream is disposed.
public bool Disposed { get; }
Property Value
EndOfStream
Gets a value indicating whether the position is at end of the stream.
public bool EndOfStream { get; }
Property Value
Length
Gets the length of this stream.
public override long Length { get; }
Property Value
Offset
Gets the offset from the BaseStream.
public long Offset { get; }
Property Value
ParentDataStream
Gets the parent DataStream only if this stream was initialized from a DataStream.
public DataStream? ParentDataStream { get; }
Property Value
Position
Gets or sets the position from the start of this stream.
public override long Position { get; set; }
Property Value
ReadTimeout
Gets or sets an invalid value as read time is not supported.
public override int ReadTimeout { get; set; }
Property Value
WriteTimeout
Gets or sets an invalid value as write time is not supported.
public override int WriteTimeout { get; set; }
Property Value
Methods
Compare(Stream)
Compare the content of the stream with another one.
public bool Compare(Stream otherStream)
Parameters
otherStream
StreamStream 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
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
longDistance to move position.
mode
SeekOriginMode 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
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
intIndex to start copying in buffer.
count
intMaximum 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
ActionAction to run.
position
longPosition to move.
mode
SeekOriginMode 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
Seek(long, SeekOrigin)
Move the position of the stream.
public override long Seek(long offset, SeekOrigin origin = SeekOrigin.Begin)
Parameters
offset
longDistance to move position.
origin
SeekOriginMode 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
SetLength(long)
Sets the length of the current stream.
public override void SetLength(long value)
Parameters
value
longThe new length value.
Slice(long)
Creates a substream starting in a defined position.
public DataStream Slice(long start)
Parameters
start
longDefined 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
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
WriteByte(byte)
Writes a byte.
public override void WriteByte(byte value)
Parameters
value
byteByte 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
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
longStarting position to read from the current stream.
length
longLength of the segment to read.
stream
StreamOutput 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
longStarting position to read from the current stream.
length
longLength of the segment to read.
fileOut
stringOutput 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
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
StreamThe 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
stringOutput file path.
Remarks
It preserves the current position and creates any required directory.