/// When overridden in a derived class, gets the length in bytes of the stream.
/// </summary>
public override long Length
{
get { return Math.Max(0, Math.Min(_limitPosition, _rawStream.Length) - _startPosition); }
}
/// <summary>
/// When overridden in a derived class, gets or sets the position within the current stream.
/// </summary>
public override long Position
{
get { IsNotDisposed(); return _current - _startPosition; }
set { IsNotDisposed(); Seek(value, SeekOrigin.Begin); }
}
/// <summary>
/// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
/// </summary>
public override int Read(byte[] buffer, int offset, int count)
{
IsNotDisposed();
Check.Assert<NotSupportedException>(CanRead);
long remaining = _limitPosition - _current;
if (remaining < count)
count = (int)remaining;
if (count <= 0)
return 0;
int amt = _rawStream.Read(buffer, offset, count);
_current += amt;
return amt;
}
/// <summary>
/// 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.
/// </summary>
public override int ReadByte()
{
IsNotDisposed();
Check.Assert<NotSupportedException>(CanRead);
long remaining = _limitPosition - _current;
if (remaining < 1)
return -1;
int result = _rawStream.ReadByte();
_current++;
return result;
}
/// <summary>
/// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
/// </summary>
public override void Write(byte[] buffer, int offset, int count)
{
IsNotDisposed();
Check.Assert<NotSupportedException>(CanWrite);
long remaining = _limitPosition - _current;
if (remaining < count)
throw new InvalidOperationException("Attempt to write past end of stream.");
_rawStream.Write(buffer, offset, count);
_current += count;
}
/// <summary>
/// Writes a byte to the current position in the stream and advances the position within the stream by one byte.
/// </summary>
public override void WriteByte(byte value)
{
IsNotDisposed();
Check.Assert<NotSupportedException>(CanWrite);
long remaining = _limitPosition - _current;
if (remaining < 1)
throw new InvalidOperationException("Attempt to write past end of stream.");