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..1b476f645 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();