Repository navigation
Description
Description
Default buffer size value
The StreamWriter constructor overload that takes FileStreamOptions uses a buffer size of DefaultFileStreamBufferSize (4096) for the StreamWriter char buffer, but all other StreamWriter overloads without a caller-specified buffer size use DefaultBufferSize (1024). This is clearly a mistake and that DefaultFileStreamBufferSize was only intended to be used as the default buffer size of the FileStream byte buffer, not the StreamWriter char buffer.
Compare with the StreamReader constructor overload that takes FileStreamOptions, which correctly uses a buffer size of DefaultBufferSize (1024).
It's not clear why the DefaultFileStreamBufferSize constant is even defined in StreamReader or StreamWriter. The default buffer size of FileStream itself is also 4096, so StreamWriter and StreamReader could just use the FileStream constructor overload that doesn't require a bufferSize and get the same result. Neither StreamWriter nor StreamReader reference DefaultFileStreamBufferSize when using FileStreamOptions anyway.
Also, the StreamWriter constructor docs are inaccurate, since the bufferSize description is "The buffer size, in bytes.", but it actually indicates the size of a char[], not a byte[].
The StreamReader constructor docs are better, though inconsistent. One of the StreamReader overloads gives the complete correct description of "The minimum buffer size, in number of 16-bit characters.", but the other overloads just say "The minimum buffer size." without specifying the unit.
ArgumentOutOfRangeException conditions
The StreamWriter and StreamReader constructors that take a Stream allow bufferSize to be explicitly passed as -1 to use the default buffer size, but the constructors that take a file path throw an ArgumentOutOfRangeException if -1 is passed as the buffer size. I can't think of a reason for this inconsistency.
The StreamWriter docs claim every constructor throws ArgumentOutOfRangeException when "bufferSize is negative.", which is incorrect because -1 is accepted for Stream-accepting overloads. Also, it fails to mention that ArgumentOutOfRangeException is also thrown when bufferSize is 0.
The StreamReader docs claim ArgumentOutOfRangeException is thrown when "bufferSize is less than or equal to zero.", which at least mentions that 0 throws, but it still doesn't mention that -1 doesn't throw for the Stream-accepting overloads. Also, the docs for one of the StreamReader constructors doesn't mention ArgumentOutOfRangeException at all.
Reproduction Steps
Default buffer size value
new StreamWriter(@"C:\temp\test.txt", new FileStreamOptions())ArgumentOutOfRangeException conditions
new StreamWriter(@"C:\temp\test.txt", append: false, Encoding.UTF8, -1)Expected behavior
Default buffer size value
Uses buffer size consistent with all other StreamWriter constructors (1024 chars)
ArgumentOutOfRangeException conditions
Uses default buffer size
Actual behavior
Default buffer size value
Uses buffer size inconsistent with all other StreamWriter constructors (4096 chars)
ArgumentOutOfRangeException conditions
Throws ArgumentOutOfRangeException
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
Activity
adamsitnik commentedon Aug 20, 2025
@mriehm Thank you for a very detailed report. I agree that both behaviors should be aligned with the rest. Would you like to send a PR?
CloudeaSoft commentedon Aug 21, 2025
I’d like to work on this issue. Can I be assigned?😉
adamsitnik commentedon Aug 21, 2025
Done! @CloudeaSoft please let me know if you need any help!
CloudeaSoft commentedon Aug 21, 2025
@adamsitnik I've opened a pull request, but there're 3 failed tests with total 5 errors in them. I made a list after passing through them, but i'm not sure if they're caused by my changes.
Unknowns
Build tvos-arm64 Release AllSubsets_NativeAOTBuild browser-wasm windows Release LibraryTestsKnown test errors
Build android-x64 Release AllSubsets_CoreCLRjozkee commentedon Sep 17, 2025
I think documentation is not correct here, it should say "in bytes"; bufferSize is used for the byte buffer and the char buffer has a size of
encoding.GetMaxCharCount(bufferSize)and it can change withDetectEncoding().mriehm commentedon Sep 17, 2025
Ah yes, you are right. So the docs for both are incorrect: StreamWriter docs say that its bufferSize parameter controls the size in bytes of its buffer, but it actually controls the size in chars, and the StreamReader docs say that its bufferSize parameter controls the size in chars of its buffer, but it actually controls the size in bytes.
I see now that the full effect of the bufferSize parameter is actually a bit confusing to describe because StreamWriter and StreamReader each allocate both a byte buffer and a char buffer, and the bufferSize parameter has an effect on the size of both buffers in both classes. However, the size of the "output" buffer (byte buffer for StreamWriter and char buffer for StreamReader) is also affected by the size of the text encoding, so only the size of the "input" buffer (char buffer for StreamWriter and byte buffer for StreamReader) is exclusively controlled by the bufferSize parameter.
In any case, your recommendation to update StreamReader docs to say "in bytes" makes sense to me. The fact that there is also a char buffer indirectly affected by the bufferSize parameter can be left as an unmentioned implementation detail.
In fact, if StreamReader were changed such that the char buffer were lazily allocated, I think the char buffer allocation could be avoided entirely in the case where all Read calls supply a large enough user buffer.