2018-06-17 18:21:52 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
namespace XTSSharp;
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
public class SectorStream : Stream
|
|
|
|
{
|
|
|
|
private readonly Stream _baseStream;
|
|
|
|
private readonly long _offset;
|
|
|
|
private ulong _currentSector;
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
public int SectorSize
|
|
|
|
{
|
|
|
|
get;
|
|
|
|
private set;
|
|
|
|
}
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
public override bool CanRead => _baseStream.CanRead;
|
|
|
|
public override bool CanSeek => _baseStream.CanSeek;
|
|
|
|
public override bool CanWrite => _baseStream.CanWrite;
|
|
|
|
public override long Length => _baseStream.Length - _offset;
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
public override long Position
|
|
|
|
{
|
|
|
|
get
|
2019-04-14 17:17:26 +00:00
|
|
|
{
|
2022-11-13 16:50:57 +00:00
|
|
|
return _baseStream.Position - _offset;
|
2018-07-08 05:21:58 +00:00
|
|
|
}
|
2022-11-13 16:50:57 +00:00
|
|
|
set
|
2019-04-14 17:17:26 +00:00
|
|
|
{
|
2022-11-13 16:50:57 +00:00
|
|
|
ValidateSizeMultiple(value);
|
|
|
|
_baseStream.Position = value + _offset;
|
|
|
|
_currentSector = (ulong)(value / SectorSize);
|
2018-07-08 05:21:58 +00:00
|
|
|
}
|
2022-11-13 16:50:57 +00:00
|
|
|
}
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
protected ulong CurrentSector => _currentSector;
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
public SectorStream(Stream baseStream, int sectorSize)
|
|
|
|
: this(baseStream, sectorSize, 0L)
|
|
|
|
{
|
|
|
|
}
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
public SectorStream(Stream baseStream, int sectorSize, long offset)
|
|
|
|
{
|
|
|
|
SectorSize = sectorSize;
|
|
|
|
_baseStream = baseStream;
|
|
|
|
_offset = offset;
|
|
|
|
}
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
private void ValidateSizeMultiple(long value)
|
|
|
|
{
|
|
|
|
if (value % SectorSize == 0L)
|
2019-04-14 17:17:26 +00:00
|
|
|
{
|
2022-11-13 16:50:57 +00:00
|
|
|
return;
|
2018-07-08 05:21:58 +00:00
|
|
|
}
|
2022-11-13 16:50:57 +00:00
|
|
|
throw new ArgumentException($"Value needs to be a multiple of {SectorSize}");
|
|
|
|
}
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
protected void ValidateSize(long value)
|
|
|
|
{
|
|
|
|
if (value == SectorSize)
|
2019-04-14 17:17:26 +00:00
|
|
|
{
|
2022-11-13 16:50:57 +00:00
|
|
|
return;
|
2018-07-08 05:21:58 +00:00
|
|
|
}
|
2022-11-13 16:50:57 +00:00
|
|
|
throw new ArgumentException($"Value needs to be {SectorSize}");
|
|
|
|
}
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
protected void ValidateSize(int value)
|
|
|
|
{
|
|
|
|
if (value == SectorSize)
|
2019-04-14 17:17:26 +00:00
|
|
|
{
|
2022-11-13 16:50:57 +00:00
|
|
|
return;
|
2018-07-08 05:21:58 +00:00
|
|
|
}
|
2022-11-13 16:50:57 +00:00
|
|
|
throw new ArgumentException($"Value needs to be {SectorSize}");
|
|
|
|
}
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
public override void Flush()
|
|
|
|
{
|
|
|
|
_baseStream.Flush();
|
|
|
|
}
|
2018-07-08 05:21:58 +00:00
|
|
|
|
2022-11-13 16:50:57 +00:00
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
{
|
|
|
|
long num = origin switch
|
2019-04-14 17:17:26 +00:00
|
|
|
{
|
2022-11-13 16:50:57 +00:00
|
|
|
SeekOrigin.Begin => offset,
|
|
|
|
SeekOrigin.End => Length - offset,
|
|
|
|
_ => Position + offset,
|
|
|
|
};
|
|
|
|
Position = num;
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void SetLength(long value)
|
|
|
|
{
|
|
|
|
ValidateSizeMultiple(value);
|
|
|
|
_baseStream.SetLength(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
{
|
|
|
|
ValidateSize(count);
|
|
|
|
int result = _baseStream.Read(buffer, offset, count);
|
|
|
|
_currentSector++;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
|
|
{
|
|
|
|
ValidateSize(count);
|
|
|
|
_baseStream.Write(buffer, offset, count);
|
|
|
|
_currentSector++;
|
2018-07-08 05:21:58 +00:00
|
|
|
}
|
2018-06-17 18:21:52 +00:00
|
|
|
}
|