Class CrcCalculatorStream
A Stream that calculates a CRC32 (a checksum) on all bytes read, or on all bytes written.
public class CrcCalculatorStream : Stream, IAsyncDisposable, IDisposable
- Inheritance
-
CrcCalculatorStream
- Implements
- Inherited Members
- Extension Methods
Remarks
This class can be used to verify the CRC of a ZipEntry when reading from a stream, or to calculate a CRC when writing to a stream. The stream should be used to either read, or write, but not both. If you intermix reads and writes, the results are not defined.
This class is intended primarily for use internally by the DotNetZip library.
Constructors
CrcCalculatorStream(Stream)
The default constructor.
public CrcCalculatorStream(Stream stream)
Parameters
streamStreamThe underlying stream
Remarks
Instances returned from this constructor will leave the underlying stream open upon Close(). The stream uses the default CRC32 algorithm, which implies a polynomial of 0xEDB88320.
CrcCalculatorStream(Stream, bool)
The constructor allows the caller to specify how to handle the underlying stream at close.
public CrcCalculatorStream(Stream stream, bool leaveOpen)
Parameters
streamStreamThe underlying stream
leaveOpenbooltrue to leave the underlying stream open upon close of the
CrcCalculatorStream; false otherwise.
Remarks
The stream uses the default CRC32 algorithm, which implies a polynomial of 0xEDB88320.
CrcCalculatorStream(Stream, long)
A constructor allowing the specification of the length of the stream to read.
public CrcCalculatorStream(Stream stream, long length)
Parameters
Remarks
The stream uses the default CRC32 algorithm, which implies a polynomial of 0xEDB88320.
Instances returned from this constructor will leave the underlying stream open upon Close().
CrcCalculatorStream(Stream, long, bool)
A constructor allowing the specification of the length of the stream to read, as well as whether to keep the underlying stream open upon Close().
public CrcCalculatorStream(Stream stream, long length, bool leaveOpen)
Parameters
streamStreamThe underlying stream
lengthlongThe length of the stream to slurp
leaveOpenbooltrue to leave the underlying stream open upon close of the
CrcCalculatorStream; false otherwise.
Remarks
The stream uses the default CRC32 algorithm, which implies a polynomial of 0xEDB88320.
CrcCalculatorStream(Stream, long, bool, CRC32)
A constructor allowing the specification of the length of the stream to read, as well as whether to keep the underlying stream open upon Close(), and the CRC32 instance to use.
public CrcCalculatorStream(Stream stream, long length, bool leaveOpen, CRC32 crc32)
Parameters
streamStreamThe underlying stream
lengthlongThe length of the stream to slurp
leaveOpenbooltrue to leave the underlying stream open upon close of the
CrcCalculatorStream; false otherwise.crc32CRC32the CRC32 instance to use to calculate the CRC32
Remarks
The stream uses the specified CRC32 instance, which allows the application to specify how the CRC gets calculated.
Properties
CanRead
Indicates whether the stream supports reading.
public override bool CanRead { get; }
Property Value
CanSeek
Indicates whether the stream supports seeking.
public override bool CanSeek { get; }
Property Value
Remarks
Always returns false.
CanWrite
Indicates whether the stream supports writing.
public override bool CanWrite { get; }
Property Value
Crc
Provides the current CRC for all blocks slurped in.
public int Crc { get; }
Property Value
Remarks
The running total of the CRC is kept as data is written or read through the stream. read this property after all reads or writes to get an accurate CRC for the entire stream.
LeaveOpen
Indicates whether the underlying stream will be left open when the
CrcCalculatorStream is Closed.
public bool LeaveOpen { get; set; }
Property Value
Remarks
Set this at any point before calling Close().
Length
Returns the length of the underlying stream.
public override long Length { get; }
Property Value
Position
The getter for this property returns the total bytes read. If you use the setter, it will throw NotSupportedException.
public override long Position { get; set; }
Property Value
TotalBytesSlurped
Gets the total number of bytes run through the CRC32 calculator.
public long TotalBytesSlurped { get; }
Property Value
Remarks
This is either the total number of bytes read, or the total number of bytes written, depending on the direction of this stream.
Methods
Close()
Closes the stream.
public override void Close()
Flush()
Flush the stream.
public override void Flush()
Read(byte[], int, int)
Read from the stream
public override int Read(byte[] buffer, int offset, int count)
Parameters
bufferbyte[]the buffer to read
offsetintthe offset at which to start
countintthe number of bytes to read
Returns
- int
the number of bytes actually read
Seek(long, SeekOrigin)
Seeking is not supported on this stream. This method always throws NotSupportedException
public override long Seek(long offset, SeekOrigin origin)
Parameters
offsetlongN/A
originSeekOriginN/A
Returns
- long
N/A
SetLength(long)
This method always throws NotSupportedException
public override void SetLength(long value)
Parameters
valuelongN/A
Write(byte[], int, int)
Write to the stream.
public override void Write(byte[] buffer, int offset, int count)