MemoryStream类读写内存

  和FileStream一样,MemoryStream和BufferedStream都派生自基类Stream,因此它们有很多共同的属性和方法,但是每一个类都有自己独特的用法。这两个类都是实现对内存进行数据读写的功能,而不是对持久性存储器进行读写。
  读写内存-MemoryStream类
  MemoryStream类用于向内存而不是磁盘读写数据。MemoryStream封装以无符号字节数组形式存储的数据,该数组在创建MemoryStream对象时被初始化,或者该数组可创建为空数组。可在内存中直接访问这些封装的数据。内存流可降低应用程序中对临时缓冲区和临时文件的需要。下表列出了MemoryStream类的重要方法:
  1、Read(byte[] buffer, int offset, int count)从当前流(MemoryStream对象)中读取字节块并将数据写入缓冲区中

  参数:

    buffer,当此方法返回时,包含指定的字节数组,该数组中从 offset 到 (offset + count -1) 之间的值由从当前流中读取的字符替换。

    offset, buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中的数据;

    count,最多读取的字节数;

  返回值:

  类型:System.Int32

  写入缓冲区中的总字节数。 如果当前可用字节数不到所请求的字节数,则这一总字节数可能小于所请求的字节数,或者如果在读取任何字节前已到达流的末尾,则为零。
  2、ReadByte():从当前流(MemoryStream流)中读取一个字节。

  返回值 :

  类型:System.Int32

  强制转换为 Int32 的字节;或者如果已到达流的末尾,则为 -1。
  3、Write(byte[] buffer, int offset, int count)使用从某个缓冲区读取的数据将字节块写入当前流

  参数:

    buffer:从中写入数据的缓冲区。

    offset:buffer 中的从零开始的字节偏移量,从此处开始将字节复制到当前流。

    count:最多写入的字节数。
  4、WriteByte(byte value)将一个字节写入当前流中的当前位置

  参数:

    value:写入的字节。
  Read方法使用的语法如下:

mmstream.Read(byte[] buffer,offset,count) 

  其中mmstream为MemoryStream类的一个流对象,3个参数中,buffer包含指定的字节数组,该数组中,从offset到(offset +count-1)之间的值由当前流中读取的字符替换。Offset是指Buffer中的字节偏移量,从此处开始读取。Count是指最多读取的字节数。Write()方法和Read()方法具有相同的参数类型。

  MemoryStream类的使用实例:

using System;
using System.IO;
using System.Text;
class program
{
    static void Main()
    {
        int count;
        byte[] byteArray;
        char[] charArray;
        UnicodeEncoding uniEncoding = new UnicodeEncoding();
        byte[] firstString = uniEncoding.GetBytes("努力学习");
        byte[] secondString = uniEncoding.GetBytes("不做C#中的菜鸟");
        using (MemoryStream memStream = new MemoryStream(100))
        {
            memStream.Write(firstString, 0, firstString.Length);
            count = 0;
            while (count < secondString.Length)
            {
                memStream.WriteByte(secondString[count++]);
            }
            Console.WriteLine("Capacity={0},Length={1},Position={2}
", memStream.Capacity.ToString(), memStream.Length.ToString(), memStream.Position.ToString());
            memStream.Seek(0, SeekOrigin.Begin);
            byteArray = new byte[memStream.Length];
            count = memStream.Read(byteArray, 0, 20);
            while (count < memStream.Length)
            {
                byteArray[count++] = Convert.ToByte(memStream.ReadByte());
            }
            charArray = new char[uniEncoding.GetCharCount(byteArray, 0, count)];
            uniEncoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0);
            Console.WriteLine(charArray);
            Console.ReadKey();
        }
    }
}

  MemoryStream.Capacity 属性 取得或设定配置给这个资料流的位元组数目。
  MemoryStream.Position 属性 指定当前流的位置。
  MemoryStream.Length 属性获取用字节表示的流长度。
  SeekOrigin()是一个枚举类,作用设定流的一个参数。
  SeekOrigin.Begin我得理解就是文件的最开始,“0”是偏移,表示跳过0个字节。写2就是跳过2个字节。
  MemoryStream类通过字节读写数据。本例中定义了写入的字节数组,为了更好的说明Write和WriteByte的异同,在代码中声明了两个byte数组,其中一个数组写入时调用Write方法,通过指定该方法的三个参数实现如何写入。
  另一个数组调用了WriteByte方法,每次写入一个字节,所以采用while循环来完成全部字节的写入。写入MemoryStream后,可以检索该流的容量,实际长度,当前流的位置,将这些值输出到控制台。通过观察结果,可以确定写入MemoryStream流是否成功。
  调用Read和ReadByte两种方法读取MemoryStream流中的数据,并将其进行Unicode编码后输出到控制台。   

  来源:http://developer.51cto.com/art/201105/263356.htm

原文地址:https://www.cnblogs.com/AngelLee2009/p/3338132.html