public class MemoryStream : Stream
Object
MarshalByRefObject
Stream
MemoryStreamThis type implements IDisposable.
mscorlib
BCL
Provides support for creating and using a stream whose backing store is memory.
The MemoryStream class creates streams that have memory as a backing store instead of a disk or a network connection. MemoryStream encapsulates data stored as an unsigned byte array. The encapsulated data is directly accessible in memory. Memory streams can reduce the need for temporary buffers and files in an application.The current position of a stream is the position at which the next read or write operation takes place. The current position can be retrieved or set through the System.IO.MemoryStream.Seek(System.Int64,System.IO.SeekOrigin) method. When a new instance of MemoryStream is created, the current position is set to zero.
[Note: Memory streams created with an unsigned byte array provide a non-resizable stream view of the data. When using a byte array, you can neither append to nor shrink the stream, although you might be able to modify the existing contents depending on the parameters passed into the constructor.]
System.IO Namespace
MemoryStream Constructors
MemoryStream() Constructor
MemoryStream(int) Constructor
MemoryStream(byte[]) Constructor
MemoryStream(byte[], bool) Constructor
MemoryStream(byte[], int, int) Constructor
MemoryStream(byte[], int, int, bool) Constructor
MemoryStream(byte[], int, int, bool, bool) Constructor
MemoryStream Methods
MemoryStream.Close Method
MemoryStream.Flush Method
MemoryStream.GetBuffer Method
MemoryStream.Read Method
MemoryStream.ReadByte Method
MemoryStream.Seek Method
MemoryStream.SetLength Method
MemoryStream.ToArray Method
MemoryStream.Write Method
MemoryStream.WriteByte Method
MemoryStream.WriteTo Method
MemoryStream Properties
MemoryStream.CanRead Property
MemoryStream.CanSeek Property
MemoryStream.CanWrite Property
MemoryStream.Capacity Property
MemoryStream.Length Property
MemoryStream.Position Property
public MemoryStream();
Constructs and initializes a new resizable instance of the MemoryStream class.
The System.IO.Stream.CanRead, System.IO.Stream.CanSeek, and System.IO.Stream.CanWrite properties of the new instance of the MemoryStream class are set totrue
.The capacity of the new stream instance can be increased by using the System.IO.MemoryStream.SetLength(System.Int64) method or by setting the System.IO.MemoryStream.Capacity property.
The new stream exposes the underlying byte buffer, which can be accessed through the System.IO.MemoryStream.GetBuffer method.
System.IO.MemoryStream Class, System.IO Namespace
public MemoryStream(int capacity);
Constructs and initializes a new resizable instance of the MemoryStream class.
- capacity
- A Int32 that specifies the initial size of the internal Byte array.
Exception Type Condition ArgumentOutOfRangeException capacity is negative.
The System.IO.Stream.CanRead, System.IO.Stream.CanSeek, and System.IO.Stream.CanWrite properties of the new instance of the MemoryStream class are set totrue
.The System.IO.MemoryStream.Capacity of the new stream instance is set to capacity can be increased by using the System.IO.MemoryStream.SetLength(System.Int64) method or by setting the System.IO.MemoryStream.Capacity property. Write operations at the end of the new instance of the MemoryStream class expand the MemoryStream.
The new stream exposes the underlying byte buffer, which can be accessed through the System.IO.MemoryStream.GetBuffer method.
System.IO.MemoryStream Class, System.IO Namespace
public MemoryStream(byte[] buffer);
Constructs and initializes a new non-resizable instance of the MemoryStream class.
- buffer
- The Byte array from which to create the new stream.
Exception Type Condition ArgumentNullException The buffer parameter is null
.
The System.IO.Stream.CanRead, System.IO.Stream.CanSeek, and System.IO.Stream.CanWrite properties of the new instance of the MemoryStream class are set totrue
. System.IO.MemoryStream.Capacity is set to the length of the specified Byte array.[Note: The new stream instance can be written to, but the System.IO.MemoryStream.Capacity of the underlying Byte array cannot be changed. The length of the stream cannot be set to a value greater than System.IO.MemoryStream.Capacity , but the stream can be truncated (see System.IO.MemoryStream.SetLength(System.Int64)).]
The new stream does not expose the underlying byte buffer, and calls to the System.IO.MemoryStream.GetBuffer method throw UnauthorizedAccessException.
System.IO.MemoryStream Class, System.IO Namespace
public MemoryStream(byte[] buffer, bool writable);
Constructs and initializes a new non-resizable instance of the MemoryStream class.
- buffer
- The Byte array from which to create the new stream.
- writable
- A Boolean that specifies whether the new stream instance supports writing.
Exception Type Condition ArgumentNullException buffer is null
.
The System.IO.Stream.CanRead and System.IO.Stream.CanSeek properties of the new instance of the MemoryStream class are set totrue
. The System.IO.MemoryStream.Capacity property is set to the length of the specified Byte array. The System.IO.Stream.CanWrite property is set to writable .[Note: The new stream instance can be written to, but the System.IO.MemoryStream.Capacity of the underlying Byte array cannot be changed. The length of the stream cannot be set to a value larger than System.IO.MemoryStream.Capacity, but the stream can be truncated (see System.IO.MemoryStream.SetLength(System.Int64)).]
The new stream does not expose the underlying Byte buffer, and calls to the System.IO.MemoryStream.GetBuffer method throw UnauthorizedAccessException.
System.IO.MemoryStream Class, System.IO Namespace
public MemoryStream(byte[] buffer, int index, int count);
Constructs and initializes a new non-resizable instance of the MemoryStream class.
- buffer
- The Byte array from which to create the new stream.
- index
- A Int32 that specifies the index into buffer at which the stream begins.
- count
- A Int32 that specifies the length of the stream in bytes.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException index or count is less than zero. ArgumentException (index + count ) is greater than the length of buffer.
The System.IO.Stream.CanRead, System.IO.Stream.CanSeek, and System.IO.Stream.CanWrite properties of the new MemoryStream instance are set totrue
. The System.IO.MemoryStream.Capacity property is set to count .[Note: The new stream instance can be written to, but the System.IO.MemoryStream.Capacity of the underlying Byte array cannot be changed. The length of the stream cannot be set to a value larger than System.IO.MemoryStream.Capacity, but the stream can be truncated (see System.IO.MemoryStream.SetLength(System.Int64)).]
The new stream does not expose the underlying Byte buffer, and calls to the System.IO.MemoryStream.GetBuffer method throw UnauthorizedAccessException .
System.IO.MemoryStream Class, System.IO Namespace
public MemoryStream(byte[] buffer, int index, int count, bool writable);
Constructs and initializes a new non-resizable instance of the MemoryStream class.
- buffer
- The Byte array from which to create the new stream.
- index
- A Int32 that specifies the index in buffer at which the stream begins.
- count
- A Int32 that specifies the length of the stream in bytes.
- writable
- A Boolean that specifies whether the new stream instance supports writing.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException index or count are negative. ArgumentException (index + count ) is greater than the length of buffer.
The System.IO.MemoryStream.CanRead and System.IO.MemoryStream.CanSeek properties of the new MemoryStream are set totrue
. The System.IO.MemoryStream.Capacity property is set to count. The System.IO.Stream.CanWrite property is set to writable.[Note: The new stream instance can be written to, but the System.IO.MemoryStream.Capacity of the underlying byte array cannot be changed. The length of the stream cannot be set to a value larger than System.IO.MemoryStream.Capacity, but the stream can be truncated (see System.IO.MemoryStream.SetLength(System.Int64)).]
The new stream does not expose the underlying byte buffer, and calls to the System.IO.MemoryStream.GetBuffer method throw UnauthorizedAccessException.
System.IO.MemoryStream Class, System.IO Namespace
public MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible);
Constructs and initializes a new instance of the MemoryStream class.
- buffer
- The Byte array from which to create the new stream.
- index
- A Int32 that specifies the index into buffer at which the stream begins.
- count
- A Int32 that specifies the length of the stream in bytes.
- writable
- A Boolean that specifies whether the new stream instance supports writing.
- publiclyVisible
- A Boolean that specifies whether buffer is exposed via the System.IO.MemoryStream.GetBuffer, which returns the Byte array from which the stream was created. Specify
true
to expose buffer; otherwise, specifyfalse
.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException index or count is negative. ArgumentException (index + count ) is greater than the length of buffer.
The System.IO.MemoryStream.CanRead and System.IO.MemoryStream.CanSeek properties of the new MemoryStream instance are set totrue
. The System.IO.MemoryStream.Capacity property is set to count. The System.IO.Stream.CanWrite property is set to writable.[Note: The new stream instance can be written to, but the System.IO.MemoryStream.Capacity of the underlying Byte array cannot be changed. The length of the stream cannot be set to a value larger than System.IO.MemoryStream.Capacity, but the stream can be truncated (see System.IO.MemoryStream.SetLength(System.Int64)).]
System.IO.MemoryStream Class, System.IO Namespace
public override void Close();
Closes the current MemoryStream instance.
The stream will not support reading or writing after this method is invoked. Following a call to System.IO.MemoryStream.Close , operations on the stream can raise an exception.The buffer of a closed MemoryStream is still available, and the System.IO.MemoryStream.ToArray and System.IO.MemoryStream.GetBuffer methods can be called successfully.
[Note: This method overrides System.IO.Stream.Close .]
System.IO.MemoryStream Class, System.IO Namespace
public override void Flush();
Overrides System.IO.Stream.Flush so that no action is performed.
Since any data written to a MemoryStream is written into RAM, this method is redundant.[Note: This method overrides System.IO.Stream.Flush.]
System.IO.MemoryStream Class, System.IO Namespace
public virtual byte[] GetBuffer();
Returns the array of unsigned bytes from which this stream was created.
The Byte array from which the current stream was created, or the underlying array if a Byte array was not provided to the MemoryStream constructor during construction of the current instance.
Exception Type Condition UnauthorizedAccessException The current instance was not created with a publicly visible buffer.
To create a MemoryStream instance with a publicly visible buffer use the default constructor, or MemoryStream( Byte [], Int32, Int32, Boolean, Boolean) or MemoryStream(Int32 ) constructor.If the current stream is resizable, multiple calls to this method do not return the same array if the underlying Byte array is resized between calls. For additional information, see System.IO.MemoryStream.Capacity .
[Note: This method works when the MemoryStream is closed.]
[Behaviors: As described above.]
The following example demonstrates that two calls to the System.IO.MemoryStream.GetBuffer method on a resizable stream do not return the same array if the underlying byte array is reallocated.
using System; using System.IO; public class MemoryStreamTest { public static void Main() { MemoryStream ms = new MemoryStream(10); byte[] a = ms.GetBuffer(); byte[] b = ms.GetBuffer(); //Force reallocation of the underlying byte array. ms.Capacity = 10240; byte[] c = ms.GetBuffer(); if(Object.ReferenceEquals(a, b)) Console.WriteLine("a and b represent the same instance."); else Console.WriteLine("a and b represent the different instances."); if(Object.ReferenceEquals(a, c)) Console.WriteLine("a and c represent the same instance."); else Console.WriteLine("a and c represent the different instances."); } }The output is
a and b represent the same instance.
a and c represent the different instances.
System.IO.MemoryStream Class, System.IO Namespace
public override int Read(byte[] buffer, int offset, int count);
Reads a block of bytes from the current stream at the current position, and writes the data to the specified byte array.
- buffer
- A Byte array. When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream.
- offset
- A Int32 that specifies the byte offset in buffer at which to begin writing.
- count
- A Int32 that specifies the maximum number of bytes to read.
A Int32 that specifies the total number of bytes read into the buffer, or zero if the end of the stream is reached before any bytes are read.
Exception Type Condition ArgumentNullException buffer is null
.ArgumentOutOfRangeException offset or count is negative. ArgumentException (offset + count ) is larger than the length of buffer. ObjectDisposedException The current stream is closed.
If the read operation is successful, the current position within the stream advances by the number of bytes read. If an exception occurs, the current position within the stream remains unchanged.[Note: If the byte array specified in the buffer parameter is the underlying buffer returned by the System.IO.MemoryStream.GetBuffer method, the array contents are overwritten, and no exception is thrown.
This method overrides System.IO.Stream.Read(System.Byte[],System.Int32,System.Int32).
]
The following example demonstrates the result of reading from a MemoryStream into its underlying byte array.
using System; using System.IO; public class MemoryStreamTest { public static void Main() { byte[] values = new byte [] {0,1,2,3,4,5,6,7,8,9}; foreach (byte b in values) { Console.Write(b); } Console.WriteLine(); MemoryStream ms = new MemoryStream (values); ms.Read(values, 1, 5); foreach (byte b in values) { Console.Write(b); } } }The output is
0123456789
0012346789
System.IO.MemoryStream Class, System.IO Namespace
public override int ReadByte();
Reads a byte from the current stream at the current position.
The byte cast to a Int32, or -1 if the end of the stream has been reached.
Exception Type Condition ObjectDisposedException The current stream is closed.
If the read operation is successful, the current position within the stream is advanced by one byte. If an exception occurs, the current position within the stream is unchanged.[Note: This method overrides System.IO.Stream.ReadByte.]
System.IO.MemoryStream Class, System.IO Namespace
public override long Seek(long offset, SeekOrigin loc);
Sets the current position within the current stream to the specified value.
- offset
- A Int64 that specifies the new position within the stream. This is relative to the loc parameter, and may be positive or negative.
- loc
- A SeekOrigin value that specifies the seek reference point.
A Int64 containing the new position within the stream, calculated by combining the seek reference point and the offset.
Exception Type Condition IOException Seeking is attempted before the beginning or more than one byte beyond the end of the stream. ArgumentOutOfRangeException offset is greater than the maximum length of the MemoryStream. ArgumentException loc is not a valid SeekOrigin value. ObjectDisposedException The current stream is closed.
The position cannot be set to more than one byte beyond the end of the stream.[Note: This method overrides System.IO.Stream.Seek(System.Int64,System.IO.SeekOrigin).]
System.IO.MemoryStream Class, System.IO Namespace
public override void SetLength(long value);
Sets the length of the current stream to the specified value.
- value
- A Int64 that specifies the value at which to set the length.
Exception Type Condition NotSupportedException The current stream is not resizable and value is greater than the current System.IO.MemoryStream.Capacity. -or-
The current stream does not support writing.
ArgumentOutOfRangeException value is negative or is greater than the maximum length of the MemoryStream, where the maximum length is (System.Int32.MaxValue - origin), and origin is the index into the underlying buffer at which the stream starts.
If the specified value is less than the current length of the stream, the stream is truncated. If after the truncation the current position within the stream is past the end of the stream, the System.IO.MemoryStream.ReadByte method returns -1, the System.IO.MemoryStream.Read(System.Byte[],System.Int32,System.Int32) method reads zero bytes into the provided byte array, and System.IO.MemoryStream.Write(System.Byte[],System.Int32,System.Int32) and System.IO.MemoryStream.WriteByte(System.Byte) methods append specified bytes at the end of the stream, increasing its length.If the specified value is larger than the current capacity and the stream is resizable, the capacity is increased, and the current position within the stream is unchanged. If the length is increased, the contents of the stream between the old and the new length are initialized to zeros.
[Note: A MemoryStream instance must support writing for this method to work. Use the System.IO.MemoryStream.CanWrite property to determine whether the current instance supports writing. For additional information, see System.IO.Stream.CanWrite .
This method overrides System.IO.Stream.SetLength(System.Int64).
]
System.IO.MemoryStream Class, System.IO Namespace
public virtual byte[] ToArray();
Writes the entire stream contents to a Byte array, regardless of the current position within the stream.
A new Byte array.
This method returns a copy of the contents of the MemoryStream as a byte array. If the current instance was constructed on a provided byte array, a copy of the section of the array to which the current instance has access is returned. [Note: For additional information, see the MemoryStream (Byte[], Int32, Int32 ) constructor.]
[Note: This method works when the MemoryStream is closed.]
[Behaviors: As described above.]
System.IO.MemoryStream Class, System.IO Namespace
public override void Write(byte[] buffer, int offset, int count);
Writes a block of bytes to the current stream at the current position using data read from buffer.
- buffer
- The Byte array to write data from.
- offset
- A Int32 that specifies the zero based byte offset into buffer at which to begin writing from.
- count
- A Int32 that specifies the maximum number of bytes to write from buffer.
Exception Type Condition ArgumentNullException buffer is null
.NotSupportedException The current stream does not support writing. -or-
The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified.
ArgumentException (offset + count ) is greater than the length of buffer. ArgumentOutOfRangeException offset or count are negative. IOException An I/O error occurred. ObjectDisposedException The current stream is closed.
If the write operation is successful, the current position within the stream is advanced by the number of bytes written. If an exception occurs, the current position within the stream is unchanged.Write operations at the end of a resizable MemoryStream expand the MemoryStream.
[Note: Use the System.IO.MemoryStream.CanWrite method to determine whether the current stream supports writing.]
[Note: This method overrides System.IO.Stream.Write(System.Byte[],System.Int32,System.Int32).]
System.IO.MemoryStream Class, System.IO Namespace
public override void WriteByte(byte value);
Writes a Byte to the current stream at the current position.
- value
- The Byte to write.
Exception Type Condition ObjectDisposedException The current stream is closed. NotSupportedException The current stream does not support writing. -or-
The current position is at the end of the stream, and the stream's capacity cannot be modified.
Write operations at the end of a resizable MemoryStream expand the MemoryStream. If the write operation is successful, the current position within the stream is advanced by one byte. If an exception occurs, the position is unchanged.[Note: Use the System.IO.MemoryStream.CanWrite method to determine whether the current stream supports writing.]
[Note: This method overrides System.IO.Stream.WriteByte(System.Byte).]
System.IO.MemoryStream Class, System.IO Namespace
public virtual void WriteTo(Stream stream);
Writes the entire contents of the current MemoryStream instance to a specified stream.
- stream
- The Stream to write the current memory stream to.
Exception Type Condition ArgumentNullException stream is null
.ObjectDisposedException The current or target stream is closed.
[Note: When the current stream is open, this method is equivalent to calling System.IO.Stream.Write(System.Byte[],System.Int32,System.Int32) on the underlying buffer of this stream.]
[Behaviors: As described above.]
System.IO.MemoryStream Class, System.IO Namespace
public override bool CanRead { get; }
Gets a Boolean value indicating whether the current stream supports reading.
true
if the current stream supports reading; otherwisefalse
.
This property is read-only.
System.IO.MemoryStream Class, System.IO Namespace
public override bool CanSeek { get; }
Gets a Boolean value indicating whether the current stream supports seeking.
true
if the stream supports seeking; otherwisefalse
.
This property is read-only.
System.IO.MemoryStream Class, System.IO Namespace
public override bool CanWrite { get; }
Gets a Boolean value indicating whether the current stream supports writing.
true
if the stream supports writing; otherwise,false
.
This property is read-only.
System.IO.MemoryStream Class, System.IO Namespace
public virtual int Capacity { get; set; }
Gets or sets the number of bytes allocated for the current stream.
A Int32 containing the number of bytes allocated for the current stream.
Exception Type Condition ArgumentOutOfRangeException The value specified for a set operation is negative or less than the current length of the stream. NotSupportedException A set operation was attempted on a stream that is closed or whose capacity cannot be modified.
System.IO.MemoryStream.Capacity is the buffer length for system-provided byte arrays. If the current stream is created with a specified Byte array, System.IO.MemoryStream.Capacity indicates the length of the portion of the provided array to which the current stream has access. [Note: For additional information, see the MemoryStream (Byte[], Int32, Int32 ) constructor. ]
System.IO.MemoryStream.Capacity cannot be set to a value less than the current length of the stream, but can be set to less than the current capacity. If the capacity specified is less than the current capacity, the size of the buffer used to hold the stream may be reduced, but need not be.
[Note: If the value specified for a set operation is less than the default value, for performance reasons the property is set to the default. The default value of the System.IO.MemoryStream.Capacity property is unspecified. ]
[Behaviors: As described above.]
System.IO.MemoryStream Class, System.IO Namespace
public override long Length { get; }
Gets the length of the stream in bytes.
A Int64 containing the length of the stream in bytes.
Exception Type Condition ObjectDisposedException The current stream is closed.
This property is read-only.[Note: This property overrides System.IO.Stream.Length.]
System.IO.MemoryStream Class, System.IO Namespace
public override long Position { get; set; }
Gets or sets the current position within the stream.
A Int64 containing the current position within the stream.
Exception Type Condition ArgumentOutOfRangeException The value specified for a set operation is negative or greater than one byte beyond the length of the current stream. ObjectDisposedException The current stream is closed.
The position cannot be set more than one byte beyond the end of the stream.[Note: This property overrides System.IO.Stream.Position.]
System.IO.MemoryStream Class, System.IO Namespace