XCI-Explorer/XTSSharp/SectorStream.cs

116 lines
2.7 KiB
C#
Raw Normal View History

2018-06-17 18:21:52 +00:00
using System;
using System.IO;
namespace XTSSharp;
public class SectorStream : Stream
{
private readonly Stream _baseStream;
private readonly long _offset;
private ulong _currentSector;
public int SectorSize
{
get;
private set;
}
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;
public override long Position
{
get
2019-04-14 17:17:26 +00:00
{
return _baseStream.Position - _offset;
}
set
2019-04-14 17:17:26 +00:00
{
ValidateSizeMultiple(value);
_baseStream.Position = value + _offset;
_currentSector = (ulong)(value / SectorSize);
}
}
protected ulong CurrentSector => _currentSector;
public SectorStream(Stream baseStream, int sectorSize)
: this(baseStream, sectorSize, 0L)
{
}
public SectorStream(Stream baseStream, int sectorSize, long offset)
{
SectorSize = sectorSize;
_baseStream = baseStream;
_offset = offset;
}
private void ValidateSizeMultiple(long value)
{
if (value % SectorSize == 0L)
2019-04-14 17:17:26 +00:00
{
return;
}
throw new ArgumentException($"Value needs to be a multiple of {SectorSize}");
}
protected void ValidateSize(long value)
{
if (value == SectorSize)
2019-04-14 17:17:26 +00:00
{
return;
}
throw new ArgumentException($"Value needs to be {SectorSize}");
}
protected void ValidateSize(int value)
{
if (value == SectorSize)
2019-04-14 17:17:26 +00:00
{
return;
}
throw new ArgumentException($"Value needs to be {SectorSize}");
}
public override void Flush()
{
_baseStream.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
long num = origin switch
2019-04-14 17:17:26 +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-06-17 18:21:52 +00:00
}