Class StreamContainer
Represents stream container which contains the stream and provides stream processing routines.
public class StreamContainer : DisposableObject, IDisposable
- Inheritance
-
StreamContainer
- Implements
- Derived
- Inherited Members
Constructors
StreamContainer(Stream)
Initializes a new instance of the StreamContainer class.
public StreamContainer(Stream stream)
Parameters
stream
StreamThe stream.
StreamContainer(Stream, bool)
Initializes a new instance of the StreamContainer class.
public StreamContainer(Stream stream, bool disposeStream)
Parameters
stream
StreamThe data stream.
disposeStream
boolif set to
true
the stream will be disposed when container is disposed.
Fields
ReadWriteBytesCount
Specifies read and write bytes count when reading sequentially.
public const int ReadWriteBytesCount = 4096
Field Value
startPosition
The starting position inside the stream.
protected long startPosition
Field Value
Properties
CanRead
Gets a value indicating whether stream supports reading.
public virtual bool CanRead { get; }
Property Value
- bool
true
if stream supports reading; otherwise,false
.
CanSeek
Gets a value indicating whether stream supports seeking.
public virtual bool CanSeek { get; }
Property Value
- bool
true
if stream supports seeking; otherwise,false
.
CanWrite
Gets a value indicating whether stream supports writing.
public virtual bool CanWrite { get; }
Property Value
- bool
true
if stream supports writing; otherwise,false
.
IsStreamDisposedOnClose
Gets a value indicating whether this stream is disposed on close.
public virtual bool IsStreamDisposedOnClose { get; }
Property Value
- bool
true
if stream is disposed on close; otherwise,false
.
Length
Gets or sets the stream length in bytes. This value is less than the Length by the starting stream position passed in the StreamContainer constructor.
public virtual long Length { get; set; }
Property Value
- long
The stream length.
Position
Gets or sets the current position within the stream. This value represents offset from the starting stream position passed in the StreamContainer constructor.
public virtual long Position { get; set; }
Property Value
- long
The current stream position.
Stream
Gets the data stream.
public virtual Stream Stream { get; }
Property Value
- Stream
The data stream.
SyncRoot
Gets an object that can be used to synchronize access to the synchronized resource.
public object SyncRoot { get; }
Property Value
- object
The object that can be used to synchronize access to the synchronized resource.
Methods
AfterStreamDisposed()
Called after the stream is disposed.
protected virtual void AfterStreamDisposed()
BeforeSave(Stream)
Called before save occurs on the destination stream.
protected virtual void BeforeSave(Stream destinationStream)
Parameters
destinationStream
StreamThe destination stream.
BeforeStreamDisposed()
Called before the stream is disposed.
protected virtual void BeforeStreamDisposed()
DisposeStream()
Disposes the stream.
protected virtual void DisposeStream()
Flush()
Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
public virtual void Flush()
Read(byte[])
Reads bytes to fill the specified bytes buffer.
public virtual int Read(byte[] bytes)
Parameters
bytes
byte[]The bytes to fill.
Returns
- int
The number of bytes read. This value can be less than the number of bytes in the buffer if there is not enough bytes in the stream.
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 virtual int Read(byte[] buffer, int offset, int count)
Parameters
buffer
byte[]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.offset
intThe zero-based byte offset in
buffer
at which to begin storing the data read from the current stream.count
intThe maximum number of bytes to be read from the current stream.
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 virtual int ReadByte()
Returns
- int
The unsigned byte cast to an Int32, or -1 if at the end of the stream.
ReleaseManagedResources()
Releases the managed resources. Make sure no unmanaged resources are released here, since they may have been already released.
protected override void ReleaseManagedResources()
Save(Stream)
Saves (copies) the stream's data to the specified stream. Uses default buffer size ReadWriteBytesCount and stream Length value.
public virtual void Save(Stream destinationStream)
Parameters
destinationStream
StreamThe stream to save the data to.
Save(Stream, int)
Saves (copies) all the stream's data to the specified stream. Uses stream Length value.
public virtual void Save(Stream destinationStream, int bufferSize)
Parameters
Save(Stream, int, long)
Saves (copies) the stream's data to the specified stream.
public virtual void Save(Stream destinationStream, int bufferSize, long length)
Parameters
destinationStream
StreamThe stream to save the data to.
bufferSize
intThe buffer size. By default ReadWriteBytesCount value is used.
length
longThe stream data length to copy. By default the length is set to Length value.
Save(string)
Saves (copies) the stream's data to the specified stream. Uses default buffer size ReadWriteBytesCount and stream Length value.
public virtual void Save(string filePath)
Parameters
filePath
stringThe file path to save the stream data to.
Save(string, int)
Saves (copies) the stream's data to the specified stream. Uses stream Length value.
public virtual void Save(string filePath, int bufferSize)
Parameters
filePath
stringThe file path to save the stream data to.
bufferSize
intThe buffer size. By default ReadWriteBytesCount value is used.
Save(string, int, long)
Saves (copies) the stream's data to the specified stream.
public virtual void Save(string filePath, int bufferSize, long length)
Parameters
filePath
stringThe file path to save the stream data to.
bufferSize
intThe buffer size. By default ReadWriteBytesCount value is used.
length
longThe stream data length to copy. By default the length is set to Length value.
Seek(long, SeekOrigin)
Sets the position within the current stream.
public virtual long Seek(long offset, SeekOrigin origin)
Parameters
offset
longA byte offset relative to the
origin
parameter. This value represents offset from the starting stream position passed in the StreamContainer constructor.origin
SeekOriginA value of type SeekOrigin indicating the reference point used to obtain the new position.
Returns
- long
The new position within the current stream.
SeekBegin()
Sets the stream position to the beginning of the stream. This value represents offset from the starting stream position passed in the StreamContainer constructor.
public virtual void SeekBegin()
ToBytes()
Converts the stream data to the byte array.
public virtual byte[] ToBytes()
Returns
ToBytes(long, long)
Converts the stream data to the byte array.
public virtual byte[] ToBytes(long position, long bytesCount)
Parameters
Returns
Write(byte[])
Writes all of the specified bytes to the stream.
public virtual void Write(byte[] bytes)
Parameters
bytes
byte[]The bytes to write.
Write(byte[], int, int)
Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
public virtual void Write(byte[] buffer, int offset, int count)
Parameters
buffer
byte[]An array of bytes. This method copies
count
bytes frombuffer
to the current stream.offset
intThe zero-based byte offset in
buffer
at which to begin copying bytes to the current stream.count
intThe number of bytes to be written to the current stream.
WriteByte(byte)
Writes a byte to the current position in the stream and advances the position within the stream by one byte.
public virtual void WriteByte(byte value)
Parameters
value
byteThe byte to write to the stream.
WriteTo(StreamContainer)
Copies the contained data to another StreamContainer.
public virtual void WriteTo(StreamContainer streamContainer)
Parameters
streamContainer
StreamContainerThe stream container to copy to.
WriteTo(StreamContainer, long)
Copies the contained data to another StreamContainer.
public virtual void WriteTo(StreamContainer streamContainer, long length)
Parameters
streamContainer
StreamContainerThe stream container to copy to.
length
longThe bytes count to write.
Exceptions
- FrameworkException
Copy operation cannot complete. Cannot read + count + bytes.
Operators
explicit operator Stream(StreamContainer)
Performs an explicit conversion from StreamContainer to Stream.
public static explicit operator Stream(StreamContainer streamContainer)
Parameters
streamContainer
StreamContainerThe stream container.
Returns
- Stream
The result of the conversion.