Asynchronously detects text encoding from a stream by checking BOM, binary heuristics, and fallback analysis, resetting stream position after.
public static async Task<Encoding?> DetectEncodingAsync(Stream stream, int bufferSize = 4096, CancellationToken cancellationToken = default)
{
if (!stream.CanRead) throw new ArgumentException("Stream must be readable.", nameof(stream));
if (!stream.CanSeek) throw new ArgumentException("Stream must be seekable.", nameof(stream));
var originalPosition = stream.Position;
stream.Seek(0, SeekOrigin.Begin);
var buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
try
{
// ReadAtLeastAsync ensures we fill the buffer if possible, reducing partial read issues.
var bytesRead = await stream.ReadAtLeastAsync(buffer.AsMemory(0, bufferSize), bufferSize, throwOnEndOfStream: false, cancellationToken: cancellationToken)
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key