What is the Bufferedstream of C#?

Asked

Viewed 55 times

2

I need to accumulate bytes in a buffer and then download them into a file. I used a few tricks for it but I was told there’s a little guy who can help me, BufferedStream.

By instantiating it, I set the buffer size. So I thought he would give an "autoflush" every time he reached the specified buffer size. However, this does not occur, and he keeps accumulating until I order him to run the flush.

I tested the case where I put the buffer with capacity of 128 bytes and loaded in it 192 bytes, it did nothing.

I’d like to know how that BufferedStream works. What’s the point of him asking for a bufferSize when he doesn’t use it for anything?

2 answers

4

Following the documentation here: https://docs.microsoft.com/pt-br/dotnet/api/system.io.bufferedstream

Add a buffer storage layer to read and write transactions in another flow
....
A buffer is a block of bytes in memory used to cache data, thus reducing > the number of calls to the operating system. Buffers Improve Performance > Read & Write

In short, a class to buffer a stream and improve read and write operations.

About your doubts:
"I tested the case where I put the buffer with capacity of 128 bytes and loaded in it 192 bytes, it did nothing" and "What’s the point of him asking for a bufferSize when he doesn’t use it for anything?"

In this case you did not understand well the function of the class size parameter BufferedStream. See the documentation, first about the parameter in the constructor: https://docs.microsoft.com/en-us/dotnet/api/system.io.bufferedstream.-ctor

A Shared read/write buffer is allocated the first time a Bufferedstream Object is initialized with this constructor. The Shared buffer is not used if all reads and Writes are Greater than or Equal to bufferSize.

Or, in free translation

A shared read/write buffer is allocated at first since a Bufferedstream object is initialized with this constructor. The shared buffer is not used if all readings and recordings are greater than or equal to bufferSize

That is, this parameter serves to define the size of the internal buffer, called here "shared", used by the class BufferedStream.

Still on the documentation link, after an example code has:

If you always read and write to sizes larger than the size of internal buffer, Bufferedstream may not even allot the buffer intern.

That is, you initialized the class with a 128 buffer size, but used 192, so the internal buffer was not even used, and the parameter made no sense.

Even in the documentation link, you have an example of code and a suggestion to test this parameter to determine the best combination with respect to parformance:

Vary the constants dataArraySize and streamBufferSize to display its effect on performance.

In summary, the parameter of bufferSize serves to define the size of the internal buffer and further optimize operations.

2

This class exists basically to create a technique of double buffer when it is needed in some scenario, or for use in conjunction with some stream that is not bufferized and you need this.

If you use a class of stream bufferized, which I think is the case of all the patterns contained in the . NET BCL (at least in the ones that make sense to have a buffer), do not need to use this class, so I would worry little about her, probably want to use something that is not necessary, or in some case well advanced, that there is no indication in the question.

In the past she was most useful when the bufferizing was not applied in several classes.

If you really need it, it’s not a mistake design, and have any specific problem can ask a question with this problem with all necessary information.

If you are curious you can consult the class source.

I will not put the documentation because this is a case that it does not serve much, after all it is outdated, no one was there change to identify that the class is almost obsolete.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.