METHOD

Stream.ReadAtLeast

Attempts to fill at least the requested minimum within the supplied buffer. It may read more than the minimum, up to buffer length. With false, early EOF returns the actual count; with true it throws. The minimum must be between zero and buffer length.

Formal Syntax & Structure

csharp syntaxcsharp
stream.ReadAtLeast(buffer, minimumBytes, throwOnEndOfStream: false)

Working Example

Example Snippetcsharp
using System;using System.IO;using System.Collections.Generic;using System.Text.Json;using var s = new MemoryStream(new byte[]{1,2}); byte[] b = new byte[4]; Console.WriteLine(s.ReadAtLeast(b,3,false)); // 2
C# & .NET Reference