I read that Buffer is a sequence of bytes. But I also read that Stream is also a sequence of bytes. So what is the difference between Stream & Buffer?
-
1A buffer has a specified, definite length whereas a stream does not.– ThePerplexedOneMay 12, 2017 at 10:34
-
4A stream is a sequence of bytes that is read and/or written to, while a buffer is a sequence of bytes that is stored.– Abion47May 12, 2017 at 10:34
-
1@Abion47 As you said buffer is for purpose to store sequence of bytes,so my question is stream is not store in memory?– Neel MahetaMay 12, 2017 at 10:46
-
1@neelmaheta A stream by itself does not store anything. It merely acts as a conduit to move data from one point to another.– Abion47May 12, 2017 at 10:47
-
thanks I read your example of Network stream.Got Idea what the stream actully is.Thanks once again.– Neel MahetaMay 12, 2017 at 10:54
2 Answers
As I said in my comment, the nutshell difference between a buffer and a stream is that a stream is a sequence that transfers information from or to a specified source, whereas a buffer is a sequence of bytes that is stored in memory. For example:
FileStream stream = new FileStream("filepath.txt", FileMode.OpenOrCreate);
Opens a stream to a file. That stream can be read from, written to, or both. As it doesn't require any additional memory, it's lightweight and fast, but arbitrarily referencing a particular set of data in the source can be cumbersome. Streams also benefit from being a connection rather than a discrete set of data, so you don't need to know the size of the data beforehand.
Conversely:
byte[] fileContents = File.ReadAllBytes("filepath.txt");
Reads all the bytes of a file into memory. This is handy for when you need to manipulate the entire file at once, or keep a "local copy" for your program to hold onto so the file can be free for other uses. Depending on the size of the source and the amount of available memory, though, a buffer containing the entire file might not be an option.
This is just a barebones explanation, though. There are more thorough ones out there, For example, as Marc Gravell puts it:
Many data-structures (lists, collections, etc) act as containers - they hold a set of objects. But not a stream; if a list is a bucket, then a stream is a hose. You can pull data from a stream, or push data into a stream - but normally only once and only in one direction (there are exceptions of course). For example, TCP data over a network is a stream; you can send (or receive) chunks of data, but only in connection with the other computer, and usually only once - you can't rewind the Internet.
Streams can also manipulate data passing through them; compression streams, encryption streams, etc. But again - the underlying metaphor here is a hose of data. A file is also generally accessed (at some level) as a stream; you can access blocks of sequential data. Of course, most file systems also provide random access, so streams do offer things like Seek, Position, Length etc - but not all implementations support such. It has no meaning to seek some streams, or get the length of an open socket.
-
I also read that stream has its internal buffer.so what is the use of Internal buffer & where to use it. May 12, 2017 at 11:04
-
@neelmaheta Streams in general don't have an internal buffer. Specific kinds of streams sometimes do, such as a
BufferedStream
or aMemoryStream
.– Abion47May 12, 2017 at 11:13 -
I see that all stream has its internal buffer of default size.You can change default buffer size by passing its value in constructure.I was see this at the time of debug mode in visual studio express edition 2013 May 15, 2017 at 4:22
-
@NeelMaheta To answer that last comment far too late, streams do usually have an internal buffer to keep the interim data that it pulls from the source before it passes that data along to the destination. This isn't a buffer used for deliberate storage, and you shouldn't treat it as a data source in its own right.– Abion47Aug 2, 2018 at 17:37
A buffer has a specified size/length and is used to store data. The Stream on the other hand is used to read and write information from one place to another. For example FileStream
is used to read and write to and from files.
The stream itself has a buffer which buffer when filled to its max size is flushed and the data in the stream is read or written.