MemoryStream

MemoryStream Visual Visual

.NET Framework 類庫
MemoryStream 類
創建其支持存儲區為記憶體的流。
命名空間: System.IO
程式集: mscorlib(在 mscorlib.dll 中)
語法
Visual Basic(聲明)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class MemoryStream _
Inherits Stream
Visual Basic(用法)
Dim instance As MemoryStream
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class MemoryStream : Stream
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class MemoryStream : public Stream
J#
/** @attribute SerializableAttribute */
/** @attribute ComVisibleAttribute(true) */
public class MemoryStream extends Stream
JScript
public class MemoryStream extends Stream
備註
有關創建檔案和向檔案中寫入文本的示例,請參見 如何:向檔案寫入文本。有關從檔案中讀取文本的示例,請參見 如何:從檔案讀取文本。有關讀取和寫入二進制檔案的示例,請參見 如何:對新建的數據檔案進行讀取和寫入。
MemoryStream 類創建這樣的流,該流以記憶體而不是磁碟或網路連線作為支持存儲區。MemoryStream 封裝以無符號位元組數組形式存儲的數據,該數組在創建 MemoryStream 對象時被初始化,或者該數組可創建為空數組。可在記憶體中直接訪問這些封裝的數據。記憶體流可降低應用程式中對臨時緩衝區和臨時檔案的需要。
流的當前位置是下一個讀取或寫入操作可能發生的位置。當前位置可以通過 Seek 方法檢索或設定。在創建 MemoryStream 的新實例時,當前位置設定為零。
用無符號位元組數組創建的記憶體流提供無法調整大小的數據流視圖,而且只能向其寫入。當使用位元組數組時,雖然根據傳遞到構造函式中的參數可能能夠修改現有內容,但既不能追加也不能收縮流。空記憶體流是可調整大小的,而且可以向其寫入和從中讀取。
如果將 MemoryStream 對象添加到 resx 檔案或 .resources 檔案中,則可在運行時調用 GetStream 方法對其進行檢索。
如果將 MemoryStream 對象序列化為資源檔案,它將被實際序列化為 UnmanagedMemoryStream。此行為可以提供更好的性能,並可以提供將指針直接指向數據的功能,而不必使用 Stream 方法。
Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE 平台說明:
在 Windows CE 中,從剪貼簿貼上的記憶體流的大小可以比複製到剪貼簿的記憶體流稍微大一點,因為可以在原始記憶體流的結尾附加額外的位元組。若要準確檢索記憶體流,請將對象的大小作為該對象的前綴來確定其接收方式,或將 DataObject 複製到剪貼簿,其中包含該記憶體流和表示其大小的字元串值。
示例
下面的代碼示例說明如何通過將記憶體用作備份來讀取和寫入數據。
Visual Basic 複製代碼
Imports System
Imports System.IO
Imports System.Text
Module MemStream
Sub Main()
Dim count As Integer
Dim byteArray As Byte()
Dim charArray As Char()
Dim uniEncoding As New UnicodeEncoding()
&#039; Create the data to write to the stream.
Dim firstString As Byte() = _
uniEncoding.GetBytes("Invalid file path characters are: ")
Dim secondString As Byte() = _
uniEncoding.GetBytes(Path.InvalidPathChars)
Dim memStream As New MemoryStream(100)
Try
&#039; Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length)
&#039; Write the second string to the stream, byte by byte.
count = 0
While(count < secondString.Length)
memStream.WriteByte(secondString(count))
count += 1
End While
&#039; Write the stream properties to the console.
Console.WriteLine( _
"Capacity = , Length = , Position = ", _
memStream.Capacity.ToString(), _
memStream.Length.ToString(), _
memStream.Position.ToString())
&#039; Set the stream position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin)
&#039; Read the first 20 bytes from the stream.
byteArray = _
New Byte(CType(memStream.Length, Integer)){}
count = memStream.Read(byteArray, 0, 20)
&#039; Read the remaining Bytes, Byte by Byte.
While(count < memStream.Length)
byteArray(count) = _
Convert.ToByte(memStream.ReadByte())
count += 1
End While
&#039; decode the Byte array into a Char array
&#039; and write it to the console.
charArray = _
New Char(uniEncoding.GetCharCount( _
byteArray, 0, count)){}
uniEncoding.GetDecoder().GetChars( _
byteArray, 0, count, charArray, 0)
Console.WriteLine(charArray)
Finally
memStream.Close()
End Try
End Sub
End Module
C# 複製代碼
using System;
using System.IO;
using System.Text;
class MemStream
{
static void Main()
{
int count;
byte[] byteArray;
char[] charArray;
UnicodeEncoding uniEncoding = new UnicodeEncoding();
// Create the data to write to the stream.
byte[] firstString = uniEncoding.GetBytes(
"Invalid file path characters are: ");
byte[] secondString = uniEncoding.GetBytes(
Path.InvalidPathChars);
using(MemoryStream memStream = new MemoryStream(100))
{
// Write the first string to the stream.
memStream.Write(firstString, 0 , firstString.Length);
// Write the second string to the stream, byte by byte.
count = 0;
while(count < secondString.Length)
{
memStream.WriteByte(secondString[count++]);
}
// Write the stream properties to the console.
Console.WriteLine(
"Capacity = , Length = , Position = \n",
memStream.Capacity.ToString(),
memStream.Length.ToString(),
memStream.Position.ToString());
// 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, 20);
// Read the remaining bytes, byte by byte.
while(count < memStream.Length)
{
byteArray[count++] =
Convert.ToByte(memStream.ReadByte());
}
// Decode the byte array into a char array
// and write it to the console.
charArray = new char[uniEncoding.GetCharCount(
byteArray, 0, count)];
uniEncoding.GetDecoder().GetChars(
byteArray, 0, count, charArray, 0);
Console.WriteLine(charArray);
}
}
}
Visual C++ 複製代碼
using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
int count;
array<Byte>^byteArray;
array<Char>^charArray;
UnicodeEncoding^ uniEncoding = gcnew UnicodeEncoding;
// Create the data to write to the stream.
array<Byte>^firstString = uniEncoding->GetBytes( "Invalid file path characters are: " );
array<Byte>^secondString = uniEncoding->GetBytes( Path::InvalidPathChars );
MemoryStream^ memStream = gcnew MemoryStream( 100 );
try
{
// Write the first string to the stream.
memStream->Write( firstString, 0, firstString->Length );
// Write the second string to the stream, byte by byte.
count = 0;
while ( count < secondString->Length )
{
memStream->WriteByte( secondString[ count++ ] );
}
// Write the stream properties to the console.
Console::WriteLine( "Capacity = , Length = , "
"Position = \n", memStream->Capacity.ToString(), memStream->Length.ToString(), memStream->Position.ToString() );
// Set the stream position to the beginning of the stream.
memStream->Seek( 0, SeekOrigin::Begin );
// Read the first 20 bytes from the stream.
byteArray = gcnew array<Byte>(memStream->Length);
count = memStream->Read( byteArray, 0, 20 );
// Read the remaining bytes, byte by byte.
while ( count < memStream->Length )
{
byteArray[ count++ ] = Convert::ToByte( memStream->ReadByte() );
}
// Decode the Byte array into a Char array
// and write it to the console.
charArray = gcnew array<Char>(uniEncoding->GetCharCount( byteArray, 0, count ));
uniEncoding->GetDecoder()->GetChars( byteArray, 0, count, charArray, 0 );
Console::WriteLine( charArray );
}
finally
{
memStream->Close();
}
}
J# 複製代碼
import System.*;
import System.IO.*;
import System.Text.*;
class MemStream
{
public static void main(String[] args)
{
int count;
ubyte byteArray[];
char charArray[];
UnicodeEncoding uniEncoding = new UnicodeEncoding();
// Create the data to write to the stream.
ubyte firstString[] = uniEncoding.GetBytes(
"Invalid file path characters are: ");
ubyte secondString[] = uniEncoding.GetBytes(Path.InvalidPathChars);
MemoryStream memStream = new MemoryStream(100);
try {
// Write the first string to the stream.
memStream.Write(firstString, 0, firstString.length);
// Write the second string to the stream, byte by byte.
count = 0;
while((count < secondString.length)) {
memStream.WriteByte(secondString[count++]);
}
// Write the stream properties to the console.
Console.WriteLine(
"Capacity = , Length = , Position = \n",
(new Integer( memStream.get_Capacity())).ToString(),
(new Long ( memStream.get_Length())).ToString(),
(new Long ( memStream.get_Position())).ToString());
// Set the position to the beginning of the stream.
memStream.Seek(0, SeekOrigin.Begin);
// Read the first 20 bytes from the stream.
byteArray = new ubyte[(int)memStream.get_Length()];
count = memStream.Read(byteArray, 0, 20);
// Read the remaining bytes, byte by byte.
while ((count < memStream.get_Length())) {
byteArray[count++]= Convert.ToByte(memStream.ReadByte());
}
// Decode the byte array into a char array
// and write it to the console.
charArray = new char[uniEncoding.GetCharCount(byteArray,
0, count)];
uniEncoding.GetDecoder().GetChars(byteArray, 0,
count, charArray, 0);
Console.WriteLine(charArray);
}
finally {
memStream.Dispose();
}
}//main
} //MemStream
繼承層次結構
System..::.Object
System..::.MarshalByRefObject
System.IO..::.Stream
System.IO..::.MemoryStream
執行緒安全
此類型的任何公共 static(在 Visual Basic中為 Shared) 成員都是執行緒安全的。但不保證所有實例成員都是執行緒安全的。
平台
Windows Vista, Windows XP SP2, windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
.NET Framework 和 .NET Compact Framework 並不是對每個平台的所有版本都提供支持。有關支持的版本的列表,請參見.NET Framework 系統要求。
版本信息
.NET Framework
受以下版本支持:3.5、3.0 SP1、3.0、2.0 SP1、2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:3.5、2.0、1.0
XNA Framework
受以下版本支持:1.0

相關詞條

相關搜尋

熱門詞條

聯絡我們