mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-09 21:24:08 +00:00
Especially for streams, it is more appropriate to throw NotSupportedException instead of NotImplementedException. Usually consumers of streams expect NotSupportedException to handle errors. There are other places that also use NotImplementedException but I didn't examine them for now. I only modified stream classes in SharpCompress.IO. For reference about this best practise, please see these articles: http://blogs.msdn.com/b/brada/archive/2004/07/29/201354.aspx http://blogs.msdn.com/b/jaredpar/archive/2008/12/12/notimplementedexception-vs-notsupportedexception.aspx
94 lines
2.3 KiB
C#
94 lines
2.3 KiB
C#
using System.IO;
|
|
|
|
namespace SharpCompress.IO
|
|
{
|
|
internal class ReadOnlySubStream : Stream
|
|
{
|
|
public ReadOnlySubStream(Stream stream, long bytesToRead)
|
|
: this(stream, null, bytesToRead)
|
|
{
|
|
}
|
|
|
|
public ReadOnlySubStream(Stream stream, long? origin, long bytesToRead)
|
|
{
|
|
Stream = stream;
|
|
if (origin != null)
|
|
{
|
|
stream.Position = origin.Value;
|
|
}
|
|
BytesLeftToRead = bytesToRead;
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing)
|
|
{
|
|
//Stream.Dispose();
|
|
}
|
|
}
|
|
|
|
private long BytesLeftToRead { get; set; }
|
|
|
|
public Stream Stream { get; private set; }
|
|
|
|
public override bool CanRead
|
|
{
|
|
get { return true; }
|
|
}
|
|
|
|
public override bool CanSeek
|
|
{
|
|
get { return false; }
|
|
}
|
|
|
|
public override bool CanWrite
|
|
{
|
|
get { return false; }
|
|
}
|
|
|
|
public override void Flush()
|
|
{
|
|
throw new System.NotSupportedException();
|
|
}
|
|
|
|
public override long Length
|
|
{
|
|
get { throw new System.NotSupportedException(); }
|
|
}
|
|
|
|
public override long Position
|
|
{
|
|
get { throw new System.NotSupportedException(); }
|
|
set { throw new System.NotSupportedException(); }
|
|
}
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
{
|
|
if (BytesLeftToRead < count)
|
|
{
|
|
count = (int)BytesLeftToRead;
|
|
}
|
|
int read = Stream.Read(buffer, offset, count);
|
|
if (read > 0)
|
|
{
|
|
BytesLeftToRead -= read;
|
|
}
|
|
return read;
|
|
}
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
{
|
|
throw new System.NotSupportedException();
|
|
}
|
|
|
|
public override void SetLength(long value)
|
|
{
|
|
throw new System.NotSupportedException();
|
|
}
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
{
|
|
throw new System.NotSupportedException();
|
|
}
|
|
}
|
|
} |