Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added Seek and Position setter for LiteFileStream.Read #2148

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions LiteDB/Client/Storage/LiteFileStream.Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,21 @@ private byte[] GetChunkData(int index)
// if chunk is null there is no more chunks
return chunk?["data"].AsBinary;
}

private void SetReadStreamPosition(long newPosition)
{
if (newPosition < 0 && newPosition > Length)
{
throw new ArgumentOutOfRangeException();
}
_streamPosition = newPosition;

// calculate new chunk position
_currentChunkIndex = (int)_streamPosition / MAX_CHUNK_SIZE;
_positionInChunk = (int)_streamPosition % MAX_CHUNK_SIZE;

// get current chunk
_currentChunkData = this.GetChunkData(_currentChunkIndex);
}
}
}
31 changes: 24 additions & 7 deletions LiteDB/Client/Storage/LiteFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,34 @@ internal LiteFileStream(ILiteCollection<LiteFileInfo<TFileId>> files, ILiteColle

public override bool CanWrite { get { return _mode == FileAccess.Write; } }

public override bool CanSeek { get { return false; } }
public override bool CanSeek { get { return _mode == FileAccess.Read; } }

public override long Position
{
get { return _streamPosition; }
set { throw new NotSupportedException(); }
set { if (_mode == FileAccess.Read) { this.SetReadStreamPosition(value); } else { throw new NotSupportedException(); } }
}

public override long Seek(long offset, SeekOrigin origin)
{
if (_mode == FileAccess.Write)
{
throw new NotSupportedException();
}

switch (origin)
{
case SeekOrigin.Begin:
this.SetReadStreamPosition(offset);
break;
case SeekOrigin.Current:
this.SetReadStreamPosition(_streamPosition + offset);
break;
case SeekOrigin.End:
this.SetReadStreamPosition(Length + offset);
break;
}
return _streamPosition;
}

#region Dispose
Expand All @@ -97,11 +119,6 @@ protected override void Dispose(bool disposing)

#region Not supported operations

public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}

public override void SetLength(long value)
{
throw new NotSupportedException();
Expand Down