MemoryStream 构造函数

定义

初始化 MemoryStream 类的新实例。Initializes a new instance of the MemoryStream class.

重载

MemoryStream()

使用初始化为零的可扩展容量初始化 MemoryStream 类的新实例。Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.

MemoryStream(Byte[])

基于指定的字节数组初始化 MemoryStream 类的无法调整大小的新实例。Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array.

MemoryStream(Int32)

使用按指定要求初始化的可扩展容量初始化 MemoryStream 类的新实例。Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.

MemoryStream(Byte[], Boolean)

MemoryStream 属性按指定设置的状态下,基于指定的字节数组初始化 CanWrite 类的无法调整大小的新实例。Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the CanWrite property set as specified.

MemoryStream(Byte[], Int32, Int32)

基于字节数组的指定区域(索引)初始化 MemoryStream 类的无法调整大小的新实例。Initializes a new non-resizable instance of the MemoryStream class based on the specified region (index) of a byte array.

MemoryStream(Byte[], Int32, Int32, Boolean)

MemoryStream 属性按指定设置的状态下,基于字节数组的指定区域,初始化 CanWrite 类的无法调整大小的新实例。Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified.

MemoryStream(Byte[], Int32, Int32, Boolean, Boolean)

MemoryStream 属性和调用 CanWrite 的能力按指定设置的状态下,基于字节数组的指定区域初始化 GetBuffer() 类的新实例。Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer() set as specified.

MemoryStream()

使用初始化为零的可扩展容量初始化 MemoryStream 类的新实例。Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.

public:
 MemoryStream();
public MemoryStream ();
Public Sub New ()

注解

CanReadCanSeekCanWrite 属性都设置为 "true"。The CanRead, CanSeek, and CanWrite properties are all set to true.

当使用 SetLength 方法将长度设置为大于当前流容量的值时,当前流的容量会自动增加。The capacity of the current stream automatically increases when you use the SetLength method to set the length to a value larger than the capacity of the current stream.

此构造函数公开 GetBuffer 返回的基础流。This constructor exposes the underlying stream, which GetBuffer returns.

另请参阅

MemoryStream(Byte[])

基于指定的字节数组初始化 MemoryStream 类的无法调整大小的新实例。Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array.

public:
 MemoryStream(cli::array <System::Byte> ^ buffer);
public MemoryStream (byte[] buffer);
new System.IO.MemoryStream : byte[] -> System.IO.MemoryStream
Public Sub New (buffer As Byte())

参数

buffer
Byte[]

从中创建当前流的无符号字节数组。The array of unsigned bytes from which to create the current stream.

异常

buffernullbuffer is null.

注解

CanReadCanSeekCanWrite 属性都设置为 "true"。The CanRead, CanSeek, and CanWrite properties are all set to true. Capacity 设置为指定的字节数组的长度。Capacity is set to the length of the specified byte array. 新流可以写入,但不可调整大小。The new stream can be written to, but is not resizable.

流的长度不能设置为大于指定字节数组初始长度的值;但是,可以截断流(请参阅 SetLength)。The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

此构造函数不公开基础流。This constructor does not expose the underlying stream. UnauthorizedAccessExceptionGetBuffer 引发。GetBuffer throws UnauthorizedAccessException.

另请参阅

MemoryStream(Int32)

使用按指定要求初始化的可扩展容量初始化 MemoryStream 类的新实例。Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.

public:
 MemoryStream(int capacity);
public MemoryStream (int capacity);
new System.IO.MemoryStream : int -> System.IO.MemoryStream
Public Sub New (capacity As Integer)

参数

capacity
Int32

内部数组的初始大小(以字节为单位)。The initial size of the internal array in bytes.

异常

capacity 为负数。capacity is negative.

示例

此代码示例是为 MemoryStream 类提供的更大示例的一部分。This code example is part of a larger example provided for the MemoryStream class.

MemoryStream^ memStream = gcnew MemoryStream( 100 );
using(MemoryStream memStream = new MemoryStream(100))
Dim memStream As New MemoryStream(100)

注解

CanReadCanSeekCanWrite 属性都设置为 "true"。The CanRead, CanSeek, and CanWrite properties are all set to true.

当使用 SetLength 方法将长度设置为大于当前流容量的值时,容量会自动增加。The capacity automatically increases when you use the SetLength method to set the length to a value larger than the capacity of the current stream. 除了使用 byte [] 参数构造的 MemoryStream 之外,在 MemoryStream 末尾编写操作的扩展 MemoryStreamExcept for a MemoryStream constructed with a byte[] parameter, write operations at the end of a MemoryStream expand the MemoryStream.

此构造函数公开 GetBuffer 返回的基础流。This constructor exposes the underlying stream that GetBuffer returns.

另请参阅

MemoryStream(Byte[], Boolean)

MemoryStream 属性按指定设置的状态下,基于指定的字节数组初始化 CanWrite 类的无法调整大小的新实例。Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the CanWrite property set as specified.

public:
 MemoryStream(cli::array <System::Byte> ^ buffer, bool writable);
public MemoryStream (byte[] buffer, bool writable);
new System.IO.MemoryStream : byte[] * bool -> System.IO.MemoryStream
Public Sub New (buffer As Byte(), writable As Boolean)

参数

buffer
Byte[]

从中创建此流的无符号字节的数组。The array of unsigned bytes from which to create this stream.

writable
Boolean

CanWrite 属性的设置,确定该流是否支持写入。The setting of the CanWrite property, which determines whether the stream supports writing.

异常

buffernullbuffer is null.

注解

"CanRead" 和 "CanSeek" 属性均设置为 "true"。The CanRead and CanSeek properties are both set to true. Capacity 设置为指定的字节数组的长度。Capacity is set to the length of the specified byte array.

流的长度不能设置为大于指定字节数组初始长度的值;但是,可以截断流(请参阅 SetLength)。The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

此构造函数不公开基础流。This constructor does not expose the underlying stream. UnauthorizedAccessExceptionGetBuffer 引发。GetBuffer throws UnauthorizedAccessException.

另请参阅

MemoryStream(Byte[], Int32, Int32)

基于字节数组的指定区域(索引)初始化 MemoryStream 类的无法调整大小的新实例。Initializes a new non-resizable instance of the MemoryStream class based on the specified region (index) of a byte array.

public:
 MemoryStream(cli::array <System::Byte> ^ buffer, int index, int count);
public MemoryStream (byte[] buffer, int index, int count);
new System.IO.MemoryStream : byte[] * int * int -> System.IO.MemoryStream
Public Sub New (buffer As Byte(), index As Integer, count As Integer)

参数

buffer
Byte[]

从中创建此流的无符号字节的数组。The array of unsigned bytes from which to create this stream.

index
Int32

buffer 内的索引,流从此处开始。The index into buffer at which the stream begins.

count
Int32

流的长度(以字节为单位)。The length of the stream in bytes.

异常

buffernullbuffer is null.

indexcount 小于零。index or count is less than zero.

缓冲区长度减去 index 小于 countThe buffer length minus index is less than count.

注解

CanReadCanSeekCanWrite 属性都设置为 true,但无法更改容量。The CanRead, CanSeek, and CanWrite properties are all set to true, but the capacity cannot be changed. Capacity 设置为 countCapacity is set to count.

流的长度不能设置为大于指定字节数组初始长度的值;但是,可以截断流(请参阅 SetLength)。The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

此构造函数不公开基础流。This constructor does not expose the underlying stream. UnauthorizedAccessExceptionGetBuffer 引发。GetBuffer throws UnauthorizedAccessException. 但是,您可以向流中写入。However, you can write to the stream.

另请参阅

MemoryStream(Byte[], Int32, Int32, Boolean)

MemoryStream 属性按指定设置的状态下,基于字节数组的指定区域,初始化 CanWrite 类的无法调整大小的新实例。Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified.

public:
 MemoryStream(cli::array <System::Byte> ^ buffer, int index, int count, bool writable);
public MemoryStream (byte[] buffer, int index, int count, bool writable);
new System.IO.MemoryStream : byte[] * int * int * bool -> System.IO.MemoryStream
Public Sub New (buffer As Byte(), index As Integer, count As Integer, writable As Boolean)

参数

buffer
Byte[]

从中创建此流的无符号字节的数组。The array of unsigned bytes from which to create this stream.

index
Int32

buffer 内的索引,流从此处开始。The index in buffer at which the stream begins.

count
Int32

流的长度(以字节为单位)。The length of the stream in bytes.

writable
Boolean

CanWrite 属性的设置,确定该流是否支持写入。The setting of the CanWrite property, which determines whether the stream supports writing.

异常

buffernullbuffer is null.

indexcount 为负。index or count are negative.

缓冲区长度减去 index 小于 countThe buffer length minus index is less than count.

注解

"CanRead" 和 "CanSeek" 属性均设置为 "true"。The CanRead and CanSeek properties are both set to true. Capacity 设置为 countCapacity is set to count.

流的长度不能设置为大于指定字节数组初始长度的值;但是,可以截断流(请参阅 SetLength)。The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

此构造函数不公开基础流。This constructor does not expose the underlying stream. UnauthorizedAccessExceptionGetBuffer 引发。GetBuffer throws UnauthorizedAccessException. 但是,如果 true``writable,则可以写入流。However, you can write to the stream if writable is true.

另请参阅

MemoryStream(Byte[], Int32, Int32, Boolean, Boolean)

MemoryStream 属性和调用 CanWrite 的能力按指定设置的状态下,基于字节数组的指定区域初始化 GetBuffer() 类的新实例。Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer() set as specified.

public:
 MemoryStream(cli::array <System::Byte> ^ buffer, int index, int count, bool writable, bool publiclyVisible);
public MemoryStream (byte[] buffer, int index, int count, bool writable, bool publiclyVisible);
new System.IO.MemoryStream : byte[] * int * int * bool * bool -> System.IO.MemoryStream
Public Sub New (buffer As Byte(), index As Integer, count As Integer, writable As Boolean, publiclyVisible As Boolean)

参数

buffer
Byte[]

从中创建此流的无符号字节的数组。The array of unsigned bytes from which to create this stream.

index
Int32

buffer 内的索引,流从此处开始。The index into buffer at which the stream begins.

count
Int32

流的长度(以字节为单位)。The length of the stream in bytes.

writable
Boolean

CanWrite 属性的设置,确定该流是否支持写入。The setting of the CanWrite property, which determines whether the stream supports writing.

publiclyVisible
Boolean

设置为 true 可以启用 GetBuffer(),它返回无符号字节数组,流从该数组创建;否则为 falsetrue to enable GetBuffer(), which returns the unsigned byte array from which the stream was created; otherwise, false.

异常

buffernullbuffer is null.

indexcount 为负数。index or count is negative.

缓冲区长度减去 index 小于 countThe buffer length minus index is less than count.

注解

"CanRead" 和 "CanSeek" 属性均设置为 "true"。The CanRead and CanSeek properties are both set to true. Capacity 设置为 countCapacity is set to count.

可以写入新的流实例,但不能更改基础字节数组的 CapacityThe new stream instance can be written to, but the Capacity of the underlying byte array cannot be changed. 流的长度不能设置为大于指定字节数组初始长度的值;但是,可以截断流(请参阅 SetLength)。The length of the stream cannot be set to a value greater than the initial length of the specified byte array; however, the stream can be truncated (see SetLength).

另请参阅

适用于