Skip to content

Implement BinaryReader.ReadExactly #106238

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

Merged
merged 5 commits into from
Aug 20, 2024
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
16 changes: 16 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/IO/BinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,22 @@ public virtual byte[] ReadBytes(int count)
return result;
}

/// <summary>
/// Reads bytes from the current stream and advances the position within the stream until the <paramref name="buffer" /> is filled.
/// </summary>
/// <remarks>
/// When <paramref name="buffer"/> is empty, this read operation will be completed without waiting for available data in the stream.
/// </remarks>
/// <param name="buffer">A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current stream.</param>
/// <exception cref="ObjectDisposedException">The stream is closed.</exception>
/// <exception cref="IOException">An I/O error occurred.</exception>
/// <exception cref="EndOfStreamException">The end of the stream is reached before filling the <paramref name="buffer" />.</exception>
public virtual void ReadExactly(Span<byte> buffer)
Copy link
Member

Choose a reason for hiding this comment

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

The BinaryReader source files appear almost not have any tripple slash comments documenting the methods. I would of course be happy to add documentation, but I am unsure if the documentation of these methods is supposed to go somewhere else or if I am supposed to break with the existing "convention" of not documenting these methods.

Please add the tripple slash comments here. We have an internal porting process where we sporadically run a command line app that feeds the new comments to api docs. Sample outcome: dotnet/dotnet-api-docs#10210

You can use the wording from Stream.ReadExactly as an inspiration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the clarification, I have updated the method with a triple slash comment, based on the Stream.ReadExactly method as you suggested.

{
ThrowIfDisposed();
_stream.ReadExactly(buffer);
}

private ReadOnlySpan<byte> InternalRead(Span<byte> buffer)
{
Debug.Assert(buffer.Length != 1, "length of 1 should use ReadByte.");
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9913,6 +9913,7 @@ protected virtual void FillBuffer(int numBytes) { }
public virtual int Read(char[] buffer, int index, int count) { throw null; }
public virtual int Read(System.Span<byte> buffer) { throw null; }
public virtual int Read(System.Span<char> buffer) { throw null; }
public virtual void ReadExactly(System.Span<byte> buffer) { throw null; }
public int Read7BitEncodedInt() { throw null; }
public long Read7BitEncodedInt64() { throw null; }
public virtual bool ReadBoolean() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public void BinaryReader_EofReachedEarlyTests_ThrowsException()
RunTest(writer => writer.Write("hello world"), reader => reader.ReadString());
RunTest(writer => writer.Write(new string('x', 1024 * 1024)), reader => reader.ReadString());

// test reading into a buffer
byte[] byteBuffer = new byte[10];

RunTest(writer => writer.Write(byteBuffer), reader => reader.ReadExactly(byteBuffer));

void RunTest(Action<BinaryWriter> writeAction, Action<BinaryReader> readAction)
{
UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true);
Expand Down Expand Up @@ -195,6 +200,7 @@ private void ValidateDisposedExceptions(BinaryReader binaryReader)
Assert.Throws<ObjectDisposedException>(() => binaryReader.Read());
Assert.Throws<ObjectDisposedException>(() => binaryReader.Read(byteBuffer, 0, 1));
Assert.Throws<ObjectDisposedException>(() => binaryReader.Read(charBuffer, 0, 1));
Assert.Throws<ObjectDisposedException>(() => binaryReader.ReadExactly(byteBuffer));
Assert.Throws<ObjectDisposedException>(() => binaryReader.ReadBoolean());
Assert.Throws<ObjectDisposedException>(() => binaryReader.ReadByte());
Assert.Throws<ObjectDisposedException>(() => binaryReader.ReadBytes(1));
Expand Down Expand Up @@ -438,6 +444,31 @@ public void Read_CharSpan_ThrowIfDisposed()
}
}

[Theory]
[InlineData(100, 10)]
[InlineData(10, 10)]
[InlineData(10, 0)]
[InlineData(0, 0)]
public void ReadExactly_ByteSpan(int sourceSize, int destinationSize)
{
using (var stream = CreateStream())
{
byte[] source = new byte[sourceSize];
new Random(345).NextBytes(source);
stream.Write(source);
stream.Position = 0;

using (var reader = new BinaryReader(stream))
{
byte[] destination = new byte[destinationSize];

reader.ReadExactly(destination);

Assert.Equal(source[..destinationSize], destination);
}
}
}

private class DerivedBinaryReader : BinaryReader
{
public DerivedBinaryReader(Stream input) : base(input) { }
Expand Down
Loading