mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-14 02:14:15 +01:00
custom file stream implementation
This commit is contained in:
parent
a3b33c0e6b
commit
56b27e5ee8
5 changed files with 153 additions and 17 deletions
|
|
@ -0,0 +1,70 @@
|
|||
namespace MareSynchronosStaticFilesServer.Utils;
|
||||
|
||||
public sealed class BlockFileDataStream : Stream
|
||||
{
|
||||
private readonly BlockFileDataSubstream[] _substreams;
|
||||
private int _currentStreamIndex = 0;
|
||||
|
||||
public BlockFileDataStream(BlockFileDataSubstream[] substreams)
|
||||
{
|
||||
_substreams = substreams;
|
||||
}
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => false;
|
||||
public override long Length => throw new NotSupportedException();
|
||||
public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
int totalRead = 0;
|
||||
int currentOffset = 0;
|
||||
int remainingCount = count;
|
||||
while (totalRead < count && _currentStreamIndex < _substreams.Length)
|
||||
{
|
||||
var lastReadBytes = _substreams[_currentStreamIndex].Read(buffer, currentOffset, remainingCount);
|
||||
if (lastReadBytes < remainingCount)
|
||||
{
|
||||
_substreams[_currentStreamIndex].Dispose();
|
||||
_currentStreamIndex++;
|
||||
}
|
||||
|
||||
totalRead += lastReadBytes;
|
||||
currentOffset += lastReadBytes;
|
||||
remainingCount -= lastReadBytes;
|
||||
}
|
||||
|
||||
return totalRead;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
base.Dispose(disposing);
|
||||
foreach (var substream in _substreams)
|
||||
{
|
||||
// probably unnecessary but better safe than sorry
|
||||
substream.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
public override void Flush()
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override long Seek(long offset, SeekOrigin origin)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void SetLength(long value)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public override void Write(byte[] buffer, int offset, int count)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue