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

Remove _isBlocked constraint from StreamReader.Peek() #89609

Merged
merged 2 commits into from
Jul 29, 2023
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
40 changes: 40 additions & 0 deletions src/libraries/System.IO/tests/StreamReader/StreamReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,46 @@ public void TestPeek()
}
}

[Fact]
public void TestPeekReadOneByteAtATime()
{
byte[] testData = new byte[] { 72, 69, 76, 76, 79 };
using var ms = new MemoryStream(testData);

// DelegateStream to read one at a time.
using var stream = new DelegateStream(
positionGetFunc: () => ms.Position,
lengthFunc: () => ms.Length,
canReadFunc: () => true,
readFunc: (buffer, offset, count) =>
{
if (count == 0 || ms.Position == ms.Length)
{
return 0;
}

ms.ReadExactly(buffer, offset, 1);
return 1;
});

using var sr = new StreamReader(stream);

for (int i = 0; i < testData.Length; i++)
{
Assert.Equal(i, stream.Position);

int tmp = sr.Peek();
Assert.Equal(testData[i], tmp);

tmp = sr.Read();
Assert.Equal(testData[i], tmp);
}

Assert.Equal(stream.Position, stream.Length);
Assert.Equal(-1, sr.Peek());
Assert.Equal(-1, sr.Read());
}

[Fact]
public void ArgumentNullOnNullArray()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ public override int Peek()

if (_charPos == _charLen)
{
if (_isBlocked || ReadBuffer() == 0)
if (ReadBuffer() == 0)
{
return -1;
}
Expand Down