2022-06-21 19:30:07 +02:00
|
|
|
using System;
|
2016-09-26 10:55:52 +01:00
|
|
|
using System.IO;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
namespace SharpCompress.IO;
|
|
|
|
|
|
|
|
|
|
internal class ReadOnlySubStream : NonDisposingStream
|
2015-12-30 11:19:42 +00:00
|
|
|
{
|
2022-12-20 15:06:44 +00:00
|
|
|
public ReadOnlySubStream(Stream stream, long bytesToRead) : this(stream, null, bytesToRead)
|
|
|
|
|
{ }
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public ReadOnlySubStream(Stream stream, long? origin, long bytesToRead)
|
|
|
|
|
: base(stream, throwOnDispose: false)
|
|
|
|
|
{
|
|
|
|
|
if (origin != null)
|
2015-12-30 11:19:42 +00:00
|
|
|
{
|
2022-12-20 15:06:44 +00:00
|
|
|
stream.Position = origin.Value;
|
2015-12-30 11:19:42 +00:00
|
|
|
}
|
2022-12-20 15:06:44 +00:00
|
|
|
BytesLeftToRead = bytesToRead;
|
|
|
|
|
}
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
private long BytesLeftToRead { get; set; }
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override bool CanRead => true;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override bool CanSeek => false;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override bool CanWrite => false;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override void Flush()
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override long Length => throw new NotSupportedException();
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override long Position
|
|
|
|
|
{
|
|
|
|
|
get => throw new NotSupportedException();
|
|
|
|
|
set => throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
if (BytesLeftToRead < count)
|
2022-12-20 13:45:47 +00:00
|
|
|
{
|
2022-12-20 15:06:44 +00:00
|
|
|
count = (int)BytesLeftToRead;
|
2022-12-20 13:45:47 +00:00
|
|
|
}
|
2022-12-20 15:06:44 +00:00
|
|
|
var read = Stream.Read(buffer, offset, count);
|
|
|
|
|
if (read > 0)
|
2015-12-30 11:19:42 +00:00
|
|
|
{
|
2022-12-20 15:06:44 +00:00
|
|
|
BytesLeftToRead -= read;
|
2015-12-30 11:19:42 +00:00
|
|
|
}
|
2022-12-20 15:06:44 +00:00
|
|
|
return read;
|
|
|
|
|
}
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override int ReadByte()
|
|
|
|
|
{
|
|
|
|
|
if (BytesLeftToRead <= 0)
|
2018-05-27 00:50:16 +02:00
|
|
|
{
|
2022-12-20 15:06:44 +00:00
|
|
|
return -1;
|
2018-05-27 00:50:16 +02:00
|
|
|
}
|
2022-12-20 15:06:44 +00:00
|
|
|
var value = Stream.ReadByte();
|
|
|
|
|
if (value != -1)
|
|
|
|
|
{
|
|
|
|
|
--BytesLeftToRead;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2018-05-27 00:50:16 +02:00
|
|
|
|
2022-06-21 19:30:07 +02:00
|
|
|
#if !NETFRAMEWORK && !NETSTANDARD2_0
|
2022-12-20 15:06:44 +00:00
|
|
|
public override int Read(Span<byte> buffer)
|
|
|
|
|
{
|
|
|
|
|
var slice_len = BytesLeftToRead < buffer.Length ? BytesLeftToRead : buffer.Length;
|
|
|
|
|
var read = Stream.Read(buffer.Slice(0, (int)slice_len));
|
|
|
|
|
if (read > 0)
|
2022-06-21 19:30:07 +02:00
|
|
|
{
|
2022-12-20 15:06:44 +00:00
|
|
|
BytesLeftToRead -= read;
|
2022-06-21 19:30:07 +02:00
|
|
|
}
|
2022-12-20 15:06:44 +00:00
|
|
|
return read;
|
|
|
|
|
}
|
2022-06-21 19:30:07 +02:00
|
|
|
#endif
|
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override void SetLength(long value)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
2015-12-30 11:19:42 +00:00
|
|
|
}
|
2022-06-21 19:30:07 +02:00
|
|
|
}
|