System.IO.UnmanagedMemoryStream.Read 方法

方法描述

将指定数目的字节读入指定的数组。

语法定义(C# System.IO.UnmanagedMemoryStream.Read 方法 的用法)

public override int Read(
	byte[] buffer,
	int offset,
	int count
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
buffer System-Byte[] 此方法返回时包含指定的字节数组,数组中 offset 和 (offset + count - 1) 之间的值由从当前源中读取的字节替换。该参数未经初始化即被传递。
offset System-Int32 buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中读取的数据。
count System-Int32 要从当前流中读取的最大字节数。
返回值 System.Int32 读入缓冲区中的总字节数。 如果当前可用的字节数没有请求的字节数那么多,则总字节数可能小于请求的字节数;如果已到达流的末尾,则为零 (0)。

提示和注释

offset 参数给出 array 参数中字节的偏移量(缓冲区索引,从此处开始读取),count 参数给出从此流中读取的最大字节数。 返回的值是读取字节的实际数量,或如果到达流的结尾,则该值为零。 如果读操作成功,则流的当前位置前进读取的字节数。 如果发生异常,则流的当前位置不变。

只有在到达流的末尾后,Read 方法才返回零。 否则,Read 在返回前始终至少从流读取一个字节。 如果在调用 Read 之后流中无可用数据,则该方法将一直阻塞,直到至少可返回一个字节的数据。 即使尚未到达流的末尾,实现仍可以随意返回少于所请求的字节。

System.IO.UnmanagedMemoryStream.Read 方法例子

使用 Marshal 类分配和解除分配非托管内存块。

// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe

using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

unsafe class TestWriter
{

    static void Main()
	{
		
            // Create some data to read and write.
            byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some data.");

	    // Allocate a block of unmanaged memory and return an IntPtr object.	
            IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);

            // Get a byte pointer from the IntPtr object.
            byte* memBytePtr = (byte*) memIntPtr.ToPointer();

            // Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);

            // Write the data.
            writeStream.Write(message, 0, message.Length);

            // Close the stream.
            writeStream.Close();

            // Create another UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read);

	    // Create a byte array to hold data from unmanaged memory.
            byte[] outMessage = new byte[message.Length];

            // Read from unmanaged memory to the byte array.
            readStream.Read(outMessage, 0, message.Length);

            // Close the stream.
            readStream.Close();

            // Display the data to the console.
            Console.WriteLine(UnicodeEncoding.Unicode.GetString(outMessage));

            // Free the block of unmanaged memory.
            Marshal.FreeHGlobal(memIntPtr);

            Console.ReadLine();
    }
}

异常

异常 异常描述
ObjectDisposedException 流已关闭。
NotSupportedException
  • 基础内存不支持读取。
  • CanRead 属性设置为 false。
ArgumentNullException buffer 参数设置为 null。
ArgumentOutOfRangeException
  • offset 参数小于零。
  • count 参数小于零。
ArgumentException 缓冲区数组的长度减去 offset 参数小于 count 参数。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。