How to utilize BufferedStream and MemoryStream in C#



< img src =",70"alt="" > A stream is an abstraction over a series of bytes. You can think about it as a continuous buffer that can be read or written to. Streams are typically utilized in combination with buffers to help load information into memory more efficiently. The System.IO namespace in.NET has lots of classes that deal with streams, such as FileStream, MemoryStream, FileInfo, and StreamReader/Writer classes.Basically, streams

are categorized as either byte streams or character streams, where byte streams handle data represented as bytes and character streams deal with characters. In.NET, the byte streams include the Stream, FileStream, MemoryStream, and BufferedStream classes. The.NET character streams consist of TextReader, TextWriter, StreamReader, and StreamWriter.This article illustrates using the BufferedStream and MemoryStream classes in C#, with pertinent code examples anywhere applicable. To work with the code examples offered in this article, you must have Visual Studio 2022 set up in your system. If you do not currently have a copy, you can download Visual Studio 2022 here. Develop a console application project in Visual Studio First of all, let’s create a.NET Core console application task in

Visual Studio. Assuming Visual Studio 2022 is set up in your

system, follow the steps outlined below to produce a new.NET Core console application task in Visual Studio. Introduce the Visual Studio IDE. Click on”Produce new project.”In the “Develop new job”window, select”Console App(. Internet Core)”from the list

  1. of templates displayed.
  2. Click Next. In the”
  3. Configure your brand-new task” window revealed next, define the name and area for the brand-new task. Click Next.
  4. In the “Extra
  5. info”window shown next, select”. NET 7.0 (Standard Term Support )”as the Structure variation you want to use.
  6. Click Produce. We’ll use this.NET 7 console application project to work with the BufferedStream and MemoryStream classes in the subsequent sections of this article.What is a buffer?A buffer represents a block of bytes in

    memory where you can momentarily keep short-term information. A buffer helps in minimizing the variety of calls your application makes to check out and write data from and to the file system. Buffers work when you need

    to keep information that is being moved from one computer system to another or from one program component to another. Buffers are used in combination with streams to make it easier for programs to read and compose information efficiently. Buffers keep information briefly so that your application need not keep re-reading the data from disk each time it is requested.Use the BufferedStream class in C# The BufferedStream class represents a type of stream that can buffer data before composing it to the stream. Simply put, a buffered stream can read or write information into a buffer, permitting you to check out or compose larger chunks of information simultaneously to enhance performance.

    You can produce buffered streams from

    memory streams and file streams. When you produce a circumstances of the BufferedStream class, you can define the buffer size too. The default buffer size is 4096 bytes. Checking out information from a buffered stream includes reading from the buffer when you call the Read technique. Even if you call Read multiple times, the data will be fetched from the stream only once.When you write to a buffered

    stream, the data is written into a buffer and after that flushed to the stream when you call the Flush method. This enhances efficiency by preventing accessing the stream for every Write call. When we use a buffer, we do not execute writes or checks out up until a particular variety of operations have been requested.By saving some quantity of data in its internal

    buffer, BufferedStream can process multiple operations on the exact same portion of memory without needing to assign memory again and once again. This conserves both time and memory consumption when producing brand-new objects repeatedly.Note that you can utilize a BufferedStream instance for either reading information or composing data, but you can not use the very same instance for both operations

    . BufferedStream is designed to avoid input and output from slowing down when there is no need for a buffer. A buffered stream might not even assign an internal buffer if the read and compose size is always greater than the internal buffer size. The following code bit demonstrates how you

    can write information to a file utilizing BufferedStream. using (FileStream fileStream =brand-new FileStream (“D: MyTextFile.txt “, FileMode.Create, FileAccess.ReadWrite ))BufferedStream bufferedStream =brand-new BufferedStream (fileStream, 1024 ); byte [] bytes=Encoding.ASCII.GetBytes (“This is a sample text.” ); bufferedStream.Write (bytes ); bufferedStream.Flush(); bufferedStream.Close(); When should you use BufferedStream? Use BufferedStream when you wish to add support for buffering to an existing stream. Thus if the original stream were a network stream, the data sent out to it would be cached in a little buffer before being written to or recovered from the network stream.Using the MemoryStream class in C# The MemoryStream class represents a lightweight stream that enables you to write to or check out from a memory buffer. The MemoryStream class

    supports the exact same techniques and properties as those of the Stream class. MemoryStream offers a basic method to check out or compose data straight from memory, without needing to designate and deallocate memory every time you wish to read or write something. This makes it quicker than using other techniques that need you to reallocate memory on each use.A memory stream is a stream

    that is really fast and effective considering that the information resides in the memory. Nevertheless, this also means that it can be easily lost if the program crashes or the computer shuts down quickly. The MemoryStream class belongs to the System.IO namespace. It can be used to read from and compose to files, network connections, and other gadgets that support reading and composing information. The MemoryStream class can likewise be used for serializing an object into a stream of bytes for storage or transmission over a network connection.The following code snippet shows how you can write data to a memory stream in C#. byte [] bytes=System.Text.Encoding.ASCII.GetBytes (“This is a sample text. “); utilizing(MemoryStream memoryStream= new MemoryStream (50)) p>

    memoryStream.Write(bytes, 0, bytes.Length); When should you utilize MemoryStream? As its name suggests, MemoryStream is a memory-only stream. As such, it must be utilized just when the amount of data that needs to be cached is little enough to conveniently suit memory.While BufferedStream is much faster and more efficient, MemoryStream is appropriate for circumstances where your application needs quicker access to information.

    You can use the async variations of the Read and Compose techniques of BufferedStream and MemoryStream classes for even much better performance and scalability. Copyright © 2022 IDG Communications, Inc. Source

Leave a Reply

Your email address will not be published. Required fields are marked *