Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix StreamView Position property #187

Merged
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
26 changes: 16 additions & 10 deletions sources/OpenMcdf.Extensions/StreamDecorator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ public override void Flush()
/// <inheritdoc/>
public override long Position
{
get
{
return position;
}
set
{
position = value;
}
get => position;
set => Seek(value, SeekOrigin.Begin);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -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;
}

Expand Down
19 changes: 6 additions & 13 deletions sources/OpenMcdf/StreamView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down