C# Class NetworkCommsDotNet.Tools.StreamTools.ThreadSafeStream

A wrapper around a stream to ensure it can be accessed in a thread safe way. The .net implementation of Stream.Synchronized is not suitable on its own.
Inheritance: Stream, IDisposable
Show file Open project: MarcFletcher/NetworkComms.Net Class Usage Examples

Public Methods

Method Description
CopyTo ( Stream destinationStream, long startPosition, long length, int writeBufferSize, double timeoutMSPerKBWrite = 1000, int minTimeoutMS = 500 ) : double

Copies data specified by start and length properties from internal stream to the provided stream.

Dispose ( ) : void

Disposes the internal stream if DiposeInnerStreamOnDispose is true. Use Close() to close the inner stream regardless of DiposeInnerStreamOnDispose.

Dispose ( bool forceDispose ) : void

Disposes the internal stream. If DiposeInnerStreamOnDispose is false, forceDispose must be true to dispose of the internal stream.

Flush ( ) : void
GetBuffer ( ) : byte[]

Attempts to return the buffer associated with the internal stream. In certain circumstances this is more efficient than copying the stream contents into a new buffer using ToArray. If the internal stream is not a memory stream will throw InvalidCastException. If access to the buffer is not allowed will throw an UnauthorizedAccessException.

MD5 ( ) : string

Return the MD5 hash of the current ThreadSafeStream as a string

MD5 ( long start, int length ) : string

Return the MD5 hash of part of the current ThreadSafeStream as a string

Read ( byte buffer, int offset, int count ) : int
Seek ( long offset, SeekOrigin origin ) : long
SetLength ( long value ) : void
ThreadSafeStream ( Stream stream ) : System

Create a thread safe stream. Once any actions are complete the stream must be correctly disposed by the user.

ThreadSafeStream ( Stream stream, bool closeStreamAfterSend ) : System

Create a thread safe stream.

ToArray ( int numberZeroBytesPrefex ) : byte[]

Returns data from entire Stream

ToArray ( long start, long length, int numberZeroBytesPrefix, int numberZeroBytesAppend ) : byte[]

Returns data from the specified portion of Stream

Write ( byte buffer, int offset, int count ) : void
Write ( byte buffer, long startPosition ) : void

Writes the provided buffer to the internal stream starting at the provided position within the internal stream

Method Details

CopyTo() public method

Copies data specified by start and length properties from internal stream to the provided stream.
public CopyTo ( Stream destinationStream, long startPosition, long length, int writeBufferSize, double timeoutMSPerKBWrite = 1000, int minTimeoutMS = 500 ) : double
destinationStream Stream The destination stream to write to
startPosition long
length long
writeBufferSize int The buffer size to use for copying stream contents
timeoutMSPerKBWrite double The timouts in milliseconds per KB to write
minTimeoutMS int The minimum time allowed for any sized copy
return double

Dispose() public method

Disposes the internal stream if DiposeInnerStreamOnDispose is true. Use Close() to close the inner stream regardless of DiposeInnerStreamOnDispose.
public Dispose ( ) : void
return void

Dispose() public method

Disposes the internal stream. If DiposeInnerStreamOnDispose is false, forceDispose must be true to dispose of the internal stream.
public Dispose ( bool forceDispose ) : void
forceDispose bool If true the internal stream will be disposed regardless of value.
return void

Flush() public method

public Flush ( ) : void
return void

GetBuffer() public method

Attempts to return the buffer associated with the internal stream. In certain circumstances this is more efficient than copying the stream contents into a new buffer using ToArray. If the internal stream is not a memory stream will throw InvalidCastException. If access to the buffer is not allowed will throw an UnauthorizedAccessException.
public GetBuffer ( ) : byte[]
return byte[]

MD5() public method

Return the MD5 hash of the current ThreadSafeStream as a string
public MD5 ( ) : string
return string

MD5() public method

Return the MD5 hash of part of the current ThreadSafeStream as a string
public MD5 ( long start, int length ) : string
start long The start position in the stream
length int The length of stream to MD5
return string

Read() public method

public Read ( byte buffer, int offset, int count ) : int
buffer byte
offset int
count int
return int

Seek() public method

public Seek ( long offset, SeekOrigin origin ) : long
offset long
origin SeekOrigin
return long

SetLength() public method

public SetLength ( long value ) : void
value long
return void

ThreadSafeStream() public method

Create a thread safe stream. Once any actions are complete the stream must be correctly disposed by the user.
public ThreadSafeStream ( Stream stream ) : System
stream Stream The stream to make thread safe
return System

ThreadSafeStream() public method

Create a thread safe stream.
public ThreadSafeStream ( Stream stream, bool closeStreamAfterSend ) : System
stream Stream The stream to make thread safe.
closeStreamAfterSend bool If true the provided stream will be disposed once data has been written to the network. If false the stream must be disposed of correctly by the user
return System

ToArray() public method

Returns data from entire Stream
public ToArray ( int numberZeroBytesPrefex ) : byte[]
numberZeroBytesPrefex int If non zero will append N 0 value bytes to the start of the returned array
return byte[]

ToArray() public method

Returns data from the specified portion of Stream
public ToArray ( long start, long length, int numberZeroBytesPrefix, int numberZeroBytesAppend ) : byte[]
start long The start position of the desired bytes
length long The total number of desired bytes, not including the zero byte prefix and append parameters
numberZeroBytesPrefix int If non zero will append N 0 value bytes to the start of the returned array
numberZeroBytesAppend int If non zero will append N 0 value bytes to the end of the returned array
return byte[]

Write() public method

public Write ( byte buffer, int offset, int count ) : void
buffer byte
offset int
count int
return void

Write() public method

Writes the provided buffer to the internal stream starting at the provided position within the internal stream
public Write ( byte buffer, long startPosition ) : void
buffer byte
startPosition long
return void