Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
move the validation logic to FileStream so each strategy does not nee…
…d to duplicate it
  • Loading branch information
adamsitnik committed Dec 23, 2022
commit 5f7dc6c76acdbc3f10322a7c23afea5202a3d4a1
28 changes: 23 additions & 5 deletions src/libraries/System.Private.CoreLib/src/System/IO/FileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,13 @@ public override long Position
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
}
else if (_strategy.IsClosed)
{
ThrowHelper.ThrowObjectDisposedException_FileClosed();
}
else if (!CanSeek)
{
if(_strategy.IsClosed)
{
ThrowHelper.ThrowObjectDisposedException_FileClosed();
}

ThrowHelper.ThrowNotSupportedException_UnseekableStream();
}

Expand Down Expand Up @@ -583,7 +584,24 @@ public override void EndWrite(IAsyncResult asyncResult)

public override bool CanSeek => _strategy.CanSeek;

public override long Seek(long offset, SeekOrigin origin) => _strategy.Seek(offset, origin);
public override long Seek(long offset, SeekOrigin origin)
{
if (origin < SeekOrigin.Begin || origin > SeekOrigin.End)
{
throw new ArgumentException(SR.Argument_InvalidSeekOrigin, nameof(origin));
}
else if (!CanSeek)
{
if (_strategy.IsClosed)
{
ThrowHelper.ThrowObjectDisposedException_FileClosed();
}

ThrowHelper.ThrowNotSupportedException_UnseekableStream();
}

return _strategy.Seek(offset, origin);
}

internal Task BaseFlushAsync(CancellationToken cancellationToken)
=> base.FlushAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,6 @@ public override void CopyTo(Stream destination, int bufferSize)

public override long Seek(long offset, SeekOrigin origin)
{
EnsureNotClosed();
EnsureCanSeek();

// If we have bytes in the write buffer, flush them out, seek and be done.
if (_writePos > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ internal sealed override void Flush(bool flushToDisk)

public sealed override long Seek(long offset, SeekOrigin origin)
{
if (origin < SeekOrigin.Begin || origin > SeekOrigin.End)
throw new ArgumentException(SR.Argument_InvalidSeekOrigin, nameof(origin));
if (_fileHandle.IsClosed) ThrowHelper.ThrowObjectDisposedException_FileClosed();
if (!CanSeek) ThrowHelper.ThrowNotSupportedException_UnseekableStream();

long oldPos = _filePosition;
long pos = origin switch
{
Expand Down