KAZIK/build/installer/CasinoSetup/SliceStream.cs

83 lines
1.6 KiB
C#

using System;
using System.IO;
namespace CasinoSetup;
public sealed class SliceStream : Stream
{
private readonly Stream source;
private readonly long start;
private readonly long length;
private long position;
public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => length;
public override long Position
{
get
{
return position;
}
set
{
Seek(value, SeekOrigin.Begin);
}
}
public SliceStream(Stream source, long start, long length)
{
if (start < 0 || length < 0 || start > source.Length - length)
{
throw new InvalidDataException("Повреждены границы пакета.");
}
this.source = source;
this.start = start;
this.length = length;
}
public override int Read(byte[] buffer, int offset, int count)
{
source.Position = start + position;
int num = source.Read(buffer, offset, (int)Math.Min(count, length - position));
position += num;
return num;
}
public override long Seek(long offset, SeekOrigin origin)
{
long num = checked(origin switch
{
SeekOrigin.Current => position,
SeekOrigin.Begin => 0L,
_ => length,
} + offset);
if (num < 0 || num > length)
{
throw new IOException("Выход за границы пакета.");
}
return position = num;
}
public override void Flush()
{
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}