Skip to content

ZipHelper.ReadBytes throws an unreachable exception #111679

Open
@carlossanlop

Description

@carlossanlop

In this method:

internal static void ReadBytes(Stream stream, byte[] buffer, int bytesToRead)
{
int bytesRead = stream.ReadAtLeast(buffer.AsSpan(0, bytesToRead), bytesToRead, throwOnEndOfStream: false);
if (bytesRead < bytesToRead)
{
throw new IOException(SR.UnexpectedEndOfStream);
}
}

We would never throw the IOException because Stream.ReadAtLeast is expected to throw when the specified minimum number of bytes is not read. So we should refactor/simplify this code.

Important: We should refactor this after we merge #103153 as that PR is making changes around that method.

The docs on top of ReadBytes might also have to be changed, because ReadAtALeast will read an equal or larger number of bytes from the stream, which depends on the size of the buffer too.

We could consider getting rid of ReadBytes altogether and always directly use Stream.ReadAtLeast.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions