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 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
45 changes: 45 additions & 0 deletions src/libraries/Common/tests/System/IO/ReadOneAtATimeStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace System.IO.Tests
{
public class ReadOneAtATimeStream : Stream
{
MemoryStream _memoryStream;

public ReadOneAtATimeStream(byte[] buffer)
{
_memoryStream = new MemoryStream(buffer);
}

public override bool CanRead => true;

public override bool CanSeek => false;

public override bool CanWrite => false;

public override long Length => _memoryStream.Length;

public override long Position
{
get => _memoryStream.Position;
set => throw new NotSupportedException();
}

public override int Read(byte[] buffer, int offset, int count)
{
if (count == 0 || Position == Length)
{
return 0;
}

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

public override void Flush() { }
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you simplify this by just deriving from MemoryStream instead of Stream?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also have a DelegateStream type in tests; it lets you just construct an instance, supplying the ctor with a delegate to use for various operations, so you might not even have to declare your own type. And if you do continue to have your own, assuming you derive from MemoryStream such that it can be really short, you could just make it a private nested class right next to the test that's using it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you simplify this by just deriving from MemoryStream instead of Stream?

I tried this first but realized that by overriding Read to call ReadExactly it would cause an infinite recursion loop.
I could achieve it if I call ReadByte but I thought that may break in the future if MemoryStream's ReadByte implementation changed to depend on Read, which is a pattern many Streams do.

Will change it to use the DelegateStream as suggested.

}
24 changes: 24 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,30 @@ public void TestPeek()
}
}

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

using var stream = new ReadOneAtATimeStream(testData);
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
1 change: 1 addition & 0 deletions src/libraries/System.IO/tests/System.IO.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<Compile Include="$(CommonTestPath)System\IO\CallTrackingStream.cs" Link="Common\System\IO\CallTrackingStream.cs" />
<Compile Include="$(CommonTestPath)System\IO\DelegateStream.cs" Link="Common\System\IO\DelegateStream.cs" />
<Compile Include="$(CommonTestPath)System\IO\WrappedMemoryStream.cs" Link="Common\System\IO\WrappedMemoryStream.cs" />
<Compile Include="$(CommonTestPath)System\IO\ReadOneAtATimeStream.cs" Link="Common\System\IO\ReadOneAtATimeStream.cs" />
<Compile Include="TestDataProvider\TestDataProvider.cs" />
<Compile Include="TextReader\CharArrayTextReader.cs" />
<Compile Include="TextReader\TextReaderTests.cs" />
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