dotnet / dotnet/dotnet-api-docs

CryptoStream.Flush contradicts Stream.Flush

Open
#10,131 8 comments 0 reactions 0 assignees View on GitHub
area-System.IO Pri3
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

[`Stream.Flush` remarks](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.flush?view=net-8.0#remarks) state that:

> Override Flush on streams that implement a buffer. Use this method to move any information from an underlying buffer to its destination, clear the buffer, or both.

However, [`CryptoStream.Flush` remarks](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptostream.flush?view=net-8.0#remarks) state that:

> Flushing the stream will not flush its underlying encoder unless you explicitly call [Flush](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptostream.flush?view=net-8.0) or [Close](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.close?view=net-8.0). Setting [AutoFlush](https://learn.microsoft.com/en-us/dotnet/api/system.io.streamwriter.autoflush?view=net-8.0) to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.
>
> Note
> You should call either the [Close](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.close?view=net-8.0) method or the [FlushFinalBlock](https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.cryptostream.flushfinalblock?view=net-8.0) method to complete flushing the buffer.

My understanding is that `Flush` flushes whatever it can and there's no guarantee that all data will be flushed.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.