Class CountingStream
A decorator stream. It wraps another stream, and performs bookkeeping to keep track of the stream Position.
public class CountingStream : Stream, IAsyncDisposable, IDisposable
- Inheritance
-
CountingStream
- Implements
- Inherited Members
- Extension Methods
Remarks
In some cases, it is not possible to get the Position of a stream, let's
say, on a write-only output stream like ASP.NET's
Response.OutputStream, or on a different write-only stream
provided as the destination for the zip by the application. In this
case, programmers can use this counting stream to count the bytes read
or written.
Consider the scenario of an application that saves a self-extracting archive (SFX), that uses a custom SFX stub.
Saving to a filesystem file, the application would open the
filesystem file (getting a FileStream), save the custom sfx stub
into it, and then call ZipFile.Save(), specifying the same
FileStream. ZipFile.Save() does the right thing for the zipentry
offsets, by inquiring the Position of the FileStream before writing
any data, and then adding that initial offset into any ZipEntry
offsets in the zip directory. Everything works fine.
Now suppose the application is an ASPNET application and it saves
directly to Response.OutputStream. It's not possible for DotNetZip to
inquire the Position, so the offsets for the SFX will be wrong.
The workaround is for the application to use this class to wrap
HttpResponse.OutputStream, then write the SFX stub and the ZipFile
into that wrapper stream. Because ZipFile.Save() can inquire the
Position, it will then do the right thing with the offsets.
Constructors
CountingStream(Stream)
The constructor.
public CountingStream(Stream stream)
Parameters
streamStreamThe underlying stream
Properties
BytesRead
the count of bytes that have been read from the stream.
public long BytesRead { get; }
Property Value
BytesWritten
The count of bytes written out to the stream.
public long BytesWritten { get; }
Property Value
CanRead
Whether the stream can be read.
public override bool CanRead { get; }
Property Value
CanSeek
Whether it is possible to call Seek() on the stream.
public override bool CanSeek { get; }
Property Value
CanWrite
Whether it is possible to call Write() on the stream.
public override bool CanWrite { get; }
Property Value
ComputedPosition
Returns the sum of number of bytes written, plus the initial offset before writing.
public long ComputedPosition { get; }
Property Value
Length
The length of the underlying stream.
public override long Length { get; }
Property Value
Position
The Position of the stream.
public override long Position { get; set; }
Property Value
WrappedStream
Gets the wrapped stream.
public Stream WrappedStream { get; }
Property Value
Methods
Adjust(long)
Adjust the byte count on the stream.
public void Adjust(long delta)
Parameters
deltalongthe number of bytes to subtract from the count.
Remarks
Subtract delta from the count of bytes written to the stream. This is necessary when seeking back, and writing additional data, as happens in some cases when saving Zip files.
Flush()
Flushes the underlying stream.
public override void Flush()
Read(byte[], int, int)
The read method.
public override int Read(byte[] buffer, int offset, int count)
Parameters
bufferbyte[]The buffer to hold the data read from the stream.
offsetintthe offset within the buffer to copy the first byte read.
countintthe number of bytes to read.
Returns
- int
the number of bytes read, after decryption and decompression.
Seek(long, SeekOrigin)
Seek in the stream.
public override long Seek(long offset, SeekOrigin origin)
Parameters
offsetlongthe offset point to seek to
originSeekOriginthe reference point from which to seek
Returns
- long
The new position
SetLength(long)
Set the length of the underlying stream. Be careful with this!
public override void SetLength(long value)
Parameters
valuelongthe length to set on the underlying stream.
Write(byte[], int, int)
Write data into the stream.
public override void Write(byte[] buffer, int offset, int count)