Byte Stream of C ×

I. principle

MemoryStream class

The MemoryStream class is used to read and write data to memory instead of disk. MemoryStream encapsulates the data stored in the form of an unsigned byte array, which is initialized when the MemoryStream object is created, or the array can be created as an empty array. These encapsulated data can be accessed directly in memory. Memory streams reduce the need for temporary buffers and temporary files in an application.

1. The MemoryStream class encapsulates a byte array. When constructing an instance, you can use a byte array as a parameter, but the length of the array cannot be adjusted. Using the default parameterless constructor to create an instance, you can use the Write method to Write, and the array size will be adjusted automatically as the byte data is written.

2. When reading the data stream in the MemoryStream class, you can use the seek method to locate the current location of the reader. You can read the data of the specified length at one time through an array of the specified length. The ReadByte method reads one byte at a time and returns the byte to an integer value.

3. The related functions of UTF-16 encoding in Unicode are defined in the Unicode encoding class. You can convert a string to bytes or bytes to strings by using one of these methods.
MemoryStream is a special case. There are no unmanaged resources in MemoryStream, so it doesn't matter whether its Dispose is called. Managed resource. Net will be recycled automatically

MemoryStream inherits from the Stream class. The advantage of a memory Stream is that the pointer can swing around, that is, a CanSeek, Position,Seek(). Read any paragraph.

2, Examples

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
    class Program
        static void Main(string[] args)
            int count;
            byte[] byteArray;
            Char[] charArray;
            UnicodeEncoding uniEncoding = new UnicodeEncoding();

            // Create the data to write to the stream.
            byte[] firstString = uniEncoding.GetBytes(
                "one two three four five ");
            byte[] secondString = uniEncoding.GetBytes(

            using (MemoryStream memStream = new MemoryStream(100))
                //Scheme 1, Write the first string to the stream directly
                memStream.Write(firstString, 0, firstString.Length);

                //Scheme 2: Write the second string to the stream, byte by byte
                count = 0;
                while (count < secondString.Length)

                // Write the stream properties to the console.
                    "Capacity = {0}, Length = {1}, Position = {2}\n",

                // Set the position to the beginning of the stream.
                memStream.Seek(0, SeekOrigin.Begin);

                // Read the first 20 bytes from the stream.
                byteArray = new byte[memStream.Length];
                count = memStream.Read(byteArray, 0, 1);

                // Read the remaining bytes, byte by byte.
                while (count < memStream.Length)
                    byteArray[count++] =

                // Decode the byte array into a char array
                // and write it to the console.
                charArray = new char[uniEncoding.GetCharCount(
                    byteArray, 0, count)];
                    byteArray, 0, count, charArray, 0);



reference resources:

Tags: encoding

Posted on Sat, 13 Jun 2020 23:50:29 -0400 by char skd1