From 7d70bc6dc0eb87675736832705d29303bb7cc184 Mon Sep 17 00:00:00 2001 From: mr-sven Date: Mon, 14 Feb 2022 10:20:18 +0100 Subject: [PATCH 1/2] Add can seek for filestream read --- LiteDB/Client/Storage/LiteFileStream.Read.cs | 16 ++++++++++ LiteDB/Client/Storage/LiteFileStream.cs | 31 +++++++++++++++----- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/LiteDB/Client/Storage/LiteFileStream.Read.cs b/LiteDB/Client/Storage/LiteFileStream.Read.cs index e60aeb0f9..ed4d4e158 100644 --- a/LiteDB/Client/Storage/LiteFileStream.Read.cs +++ b/LiteDB/Client/Storage/LiteFileStream.Read.cs @@ -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); + } } } \ No newline at end of file diff --git a/LiteDB/Client/Storage/LiteFileStream.cs b/LiteDB/Client/Storage/LiteFileStream.cs index dea5edd91..abde415fa 100644 --- a/LiteDB/Client/Storage/LiteFileStream.cs +++ b/LiteDB/Client/Storage/LiteFileStream.cs @@ -66,12 +66,34 @@ internal LiteFileStream(ILiteCollection> 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 @@ -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(); From a04a931f6a6def44f704c8806d417322756449e6 Mon Sep 17 00:00:00 2001 From: mr-sven Date: Mon, 14 Feb 2022 13:18:35 +0100 Subject: [PATCH 2/2] Seek from end gets negative value --- LiteDB/Client/Storage/LiteFileStream.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LiteDB/Client/Storage/LiteFileStream.cs b/LiteDB/Client/Storage/LiteFileStream.cs index abde415fa..1b476f645 100644 --- a/LiteDB/Client/Storage/LiteFileStream.cs +++ b/LiteDB/Client/Storage/LiteFileStream.cs @@ -90,7 +90,7 @@ public override long Seek(long offset, SeekOrigin origin) this.SetReadStreamPosition(_streamPosition + offset); break; case SeekOrigin.End: - this.SetReadStreamPosition(Length - offset); + this.SetReadStreamPosition(Length + offset); break; } return _streamPosition;