From 13ab89379c75a574579073fc398e87ee515d484f Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Tue, 8 Oct 2024 22:20:43 +1300 Subject: [PATCH 1/2] Fix StreamView Position property Setting the position past the length is expected to increase the length of the stream. --- sources/OpenMcdf/StreamView.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/sources/OpenMcdf/StreamView.cs b/sources/OpenMcdf/StreamView.cs index f7f5f54..ca31b91 100644 --- a/sources/OpenMcdf/StreamView.cs +++ b/sources/OpenMcdf/StreamView.cs @@ -63,18 +63,8 @@ public override void Flush() public override long Position { - get - { - return position; - } - - set - { - if (position > length - 1) - throw new ArgumentOutOfRangeException(nameof(value)); - - position = value; - } + get => position; + set => Seek(value, SeekOrigin.Begin); } public override int Read(byte[] buffer, int offset, int count) @@ -185,9 +175,12 @@ public override long Seek(long offset, SeekOrigin origin) throw new IOException("Seek before origin"); position = Length - offset; break; + + default: + throw new ArgumentException(nameof(origin), "Invalid seek origin"); } - if (length <= position) // Don't adjust the length when position is inside the bounds of 0 and the current length. + if (position > length) AdjustLength(position); return position; From 73125173cdaa45c1bba2152d7b0672bd68320861 Mon Sep 17 00:00:00 2001 From: Jeremy Powell Date: Wed, 9 Oct 2024 11:17:56 +1300 Subject: [PATCH 2/2] Fix StreamDecorator Seek behavior --- .../OpenMcdf.Extensions/StreamDecorator.cs | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/sources/OpenMcdf.Extensions/StreamDecorator.cs b/sources/OpenMcdf.Extensions/StreamDecorator.cs index ee3b04a..4335d7f 100644 --- a/sources/OpenMcdf.Extensions/StreamDecorator.cs +++ b/sources/OpenMcdf.Extensions/StreamDecorator.cs @@ -41,14 +41,8 @@ public override void Flush() /// public override long Position { - get - { - return position; - } - set - { - position = value; - } + get => position; + set => Seek(value, SeekOrigin.Begin); } /// @@ -77,18 +71,30 @@ public override long Seek(long offset, SeekOrigin origin) switch (origin) { case SeekOrigin.Begin: + if (offset < 0) + throw new IOException("Seek before origin"); position = offset; break; + case SeekOrigin.Current: + if (position + offset < 0) + throw new IOException("Seek before origin"); position += offset; break; + case SeekOrigin.End: - position -= offset; + if (Length - offset < 0) + throw new IOException("Seek before origin"); + position = Length - offset; break; + default: - throw new Exception("Invalid origin selected"); + throw new ArgumentException(nameof(origin), "Invalid seek origin"); } + if (position > Length) + SetLength(position); + return position; }