2022-06-21 19:30:07 +02:00
|
|
|
using System;
|
2025-07-20 17:35:22 +01:00
|
|
|
using System.Diagnostics;
|
2016-09-26 10:55:52 +01:00
|
|
|
using System.IO;
|
2025-10-28 10:09:46 +00:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
namespace SharpCompress.IO;
|
2023-03-21 13:14:08 +00:00
|
|
|
|
2025-07-20 17:35:22 +01:00
|
|
|
internal class ReadOnlySubStream : SharpCompressStream, IStreamStack
|
2015-12-30 11:19:42 +00:00
|
|
|
{
|
2025-07-20 17:35:22 +01:00
|
|
|
#if DEBUG_STREAMS
|
|
|
|
|
long IStreamStack.InstanceId { get; set; }
|
|
|
|
|
#endif
|
2025-07-20 18:11:37 +01:00
|
|
|
|
2025-07-20 17:35:22 +01:00
|
|
|
Stream IStreamStack.BaseStream() => base.Stream;
|
|
|
|
|
|
2023-02-02 00:18:37 +00:00
|
|
|
private long _position;
|
|
|
|
|
|
2026-01-06 12:42:48 +00:00
|
|
|
public ReadOnlySubStream(Stream stream, long bytesToRead, bool leaveOpen = true)
|
|
|
|
|
: this(stream, null, bytesToRead, leaveOpen) { }
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2026-01-06 12:42:48 +00:00
|
|
|
public ReadOnlySubStream(Stream stream, long? origin, long bytesToRead, bool leaveOpen = true)
|
|
|
|
|
: base(stream, leaveOpen, throwOnDispose: false)
|
2022-12-20 15:06:44 +00:00
|
|
|
{
|
2025-03-11 18:57:52 +01:00
|
|
|
if (origin != null && stream.Position != origin.Value)
|
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;
|
2023-02-02 00:18:37 +00:00
|
|
|
_position = 0;
|
2025-07-20 17:35:22 +01:00
|
|
|
#if DEBUG_STREAMS
|
|
|
|
|
this.DebugConstruct(typeof(ReadOnlySubStream));
|
|
|
|
|
#endif
|
2022-12-20 15:06:44 +00:00
|
|
|
}
|
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
|
|
|
|
2024-01-06 00:14:34 +01:00
|
|
|
public override void Flush() { }
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2025-03-11 18:57:52 +01:00
|
|
|
public override long Length => base.Length;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
public override long Position
|
|
|
|
|
{
|
2023-02-02 00:18:37 +00:00
|
|
|
get => _position; //allow position to be read (XZ uses this to calculate alignment)
|
2022-12-20 15:06:44 +00:00
|
|
|
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;
|
2023-02-02 00:18:37 +00:00
|
|
|
_position += 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;
|
2023-02-02 00:18:37 +00:00
|
|
|
_position++;
|
2022-12-20 15:06:44 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2024-03-14 09:00:44 +00:00
|
|
|
var sliceLen = BytesLeftToRead < buffer.Length ? BytesLeftToRead : buffer.Length;
|
|
|
|
|
var read = Stream.Read(buffer.Slice(0, (int)sliceLen));
|
2022-12-20 15:06:44 +00:00
|
|
|
if (read > 0)
|
2022-06-21 19:30:07 +02:00
|
|
|
{
|
2022-12-20 15:06:44 +00:00
|
|
|
BytesLeftToRead -= read;
|
2023-03-01 20:17:43 +01:00
|
|
|
_position += 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
|
|
|
|
|
|
2025-10-28 10:09:46 +00:00
|
|
|
public override async Task<int> ReadAsync(
|
|
|
|
|
byte[] buffer,
|
|
|
|
|
int offset,
|
|
|
|
|
int count,
|
|
|
|
|
CancellationToken cancellationToken
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
if (BytesLeftToRead < count)
|
|
|
|
|
{
|
|
|
|
|
count = (int)BytesLeftToRead;
|
|
|
|
|
}
|
|
|
|
|
var read = await Stream
|
|
|
|
|
.ReadAsync(buffer, offset, count, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
if (read > 0)
|
|
|
|
|
{
|
|
|
|
|
BytesLeftToRead -= read;
|
|
|
|
|
_position += read;
|
|
|
|
|
}
|
|
|
|
|
return read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if !NETFRAMEWORK && !NETSTANDARD2_0
|
|
|
|
|
public override async ValueTask<int> ReadAsync(
|
|
|
|
|
Memory<byte> buffer,
|
|
|
|
|
CancellationToken cancellationToken = default
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
var sliceLen = BytesLeftToRead < buffer.Length ? BytesLeftToRead : buffer.Length;
|
|
|
|
|
var read = await Stream
|
|
|
|
|
.ReadAsync(buffer.Slice(0, (int)sliceLen), cancellationToken)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
if (read > 0)
|
|
|
|
|
{
|
|
|
|
|
BytesLeftToRead -= read;
|
|
|
|
|
_position += read;
|
|
|
|
|
}
|
|
|
|
|
return read;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-12-20 15:20:49 +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:20:49 +00:00
|
|
|
public override void SetLength(long value) => throw new NotSupportedException();
|
2015-12-30 11:19:42 +00:00
|
|
|
|
2022-12-20 15:20:49 +00:00
|
|
|
public override void Write(byte[] buffer, int offset, int count) =>
|
2022-12-20 15:06:44 +00:00
|
|
|
throw new NotSupportedException();
|
2025-07-20 17:35:22 +01:00
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG_STREAMS
|
|
|
|
|
this.DebugDispose(typeof(ReadOnlySubStream));
|
|
|
|
|
#endif
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
2022-06-21 19:30:07 +02:00
|
|
|
}
|