43

For the public StreamWriter(Stream stream) constructor, MSDN says

Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size.

I want to use one of the other constructor overloads but would like to use the default buffer size. What is the default buffer size? MSDN does not say anywhere. Rubens Farias' answer here says it's "4 KiB" (whatever that means...KB I guess?) but there's no link to substantiate that.

4
  • This post suggests that default buffer size is 1024 bytes.
    – dotNET
    Commented Apr 2, 2015 at 12:21
  • Ah when documentation fails, decompile. I always forget that! Thanks :)
    – rory.ap
    Commented Apr 2, 2015 at 12:23
  • 1
    Here it is Commented Apr 2, 2015 at 12:24
  • 1
    @roryap: 1KiB = 1024B (as opposed to 1KB, which could mean either 1000B or 1024B). See stackoverflow.com/a/1200253/18192 .
    – Brian
    Commented Apr 10, 2015 at 17:30

1 Answer 1

66

Ah when documentation fails, decompile. I always forget that!

Well, don't do that. It isn't necessary anymore, you can look at the actual source code that the Microsoft programmers wrote. Always better than decompiled code, it has comments.

Visit the Reference Source website. It was updated about a fat year ago, it has now a very slick browser interface that's actually faster than a decompiler. Just type StreamWriter in the search box. Takes you at most a dozen seconds to discover:

    // For UTF-8, the values of 1K for the default buffer size and 4K for the
    // file stream buffer size are reasonable & give very reasonable
    // performance for in terms of construction time for the StreamWriter and
    // write perf.  Note that for UTF-8, we end up allocating a 4K byte buffer,
    // which means we take advantage of adaptive buffering code.
    // The performance using UnicodeEncoding is acceptable.  
    internal const int DefaultBufferSize = 1024;   // char[]
    private const int DefaultFileStreamBufferSize = 4096;

So the default is 1024 characters for the StreamWriter. And if you write to a file instead of a stream then there's a FileStream with a 4096 byte buffer, can't change that. It does expose a classic problem with comments, they have a knack for not being maintained and mismatch the code. The noodling about "adaptive buffering" isn't actually implemented. A KiB is an animal with 1024 toes, never 1000.

2
  • 1
    Re the 4096 byte file buffer. You can change that if you use the right FileStream constructor. Then, you can pass that FileStream to the StreamWriter constructor. Commented Nov 23, 2018 at 7:41
  • I have not tested whether its necessary to pass the larger buffer size to both constructors (StreamWriter and FileStream) or if its enough to pass it only to the FileStream one. But I can't see any harm in passing the same large size to both. Commented Nov 25, 2018 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.