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
extend the test and fix it in the BufferedStream as well
  • Loading branch information
adamsitnik authored and github-actions committed Oct 15, 2021
commit 57c73a18dd736f97055059eec6ef1476c8c78d2a
11 changes: 10 additions & 1 deletion src/libraries/System.IO.FileSystem/tests/FileStream/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ public void NoInt32OverflowInTheBufferingLogic()
Assert.Equal(buffer.Length, stream.Read(buffer));
Assert.Equal(data1, buffer);

Array.Clear(buffer);
stream.Seek(positon2, SeekOrigin.Begin);
Assert.Equal(buffer.Length, stream.Read(buffer));
Assert.Equal(data2, buffer);
}

using (var stream = new BufferedStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 0)))
{
stream.Seek(positon1, SeekOrigin.Begin);
Assert.Equal(buffer.Length, stream.Read(buffer));
Assert.Equal(data1, buffer);

stream.Seek(positon2, SeekOrigin.Begin);
Assert.Equal(buffer.Length, stream.Read(buffer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,12 @@ public override long Seek(long offset, SeekOrigin origin)
// Otherwise we will throw away the buffer. This can only happen on read, as we flushed write data above.

// The offset of the new/updated seek pointer within _buffer:
_readPos = (int)(newPos - (oldPos - _readPos));
long readPos = (newPos - (oldPos - _readPos));

// If the offset of the updated seek pointer in the buffer is still legal, then we can keep using the buffer:
if (0 <= _readPos && _readPos < _readLen)
if (0 <= readPos && readPos < _readLen)
{
_readPos = (int)readPos;
// Adjust the seek pointer of the underlying stream to reflect the amount of useful bytes in the read buffer:
_stream.Seek(_readLen - _readPos, SeekOrigin.Current);
}
Expand Down