MemoryStream 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
백업 저장소가 메모리인 스트림을 만듭니다.
public ref class MemoryStream : System::IO::Stream
public class MemoryStream : System.IO.Stream
[System.Serializable]
public class MemoryStream : System.IO.Stream
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class MemoryStream : System.IO.Stream
type MemoryStream = class
inherit Stream
[<System.Serializable>]
type MemoryStream = class
inherit Stream
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MemoryStream = class
inherit Stream
Public Class MemoryStream
Inherits Stream
- 상속
- 상속
- 특성
예제
다음 코드 예제에서는 백업 저장소로 메모리를 사용 하 여 데이터를 읽고 쓰는 방법을 보여 있습니다.
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 = {0}, Length = {1}, "
"Position = {2}\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();
}
}
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.GetInvalidPathChars());
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 = {0}, Length = {1}, Position = {2}\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++] = (byte)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);
}
}
}
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()
' 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.GetInvalidPathChars())
Dim memStream As 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))
count += 1
End While
' Write the stream properties to the console.
Console.WriteLine( _
"Capacity = {0}, Length = {1}, Position = {2}", _
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 = _
New Byte(CType(memStream.Length, Integer)){}
count = memStream.Read(byteArray, 0, 20)
' Read the remaining Bytes, Byte by Byte.
While(count < memStream.Length)
byteArray(count) = _
Convert.ToByte(memStream.ReadByte())
count += 1
End While
' 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.Close()
End Try
End Sub
End Module
설명
스트림의 현재 위치는 다음 읽기 또는 쓰기 작업이 발생할 수 있는 위치입니다. 메서드를 통해 Seek 현재 위치를 검색하거나 설정할 수 있습니다. 새 인스턴스 MemoryStream 가 만들어지면 현재 위치가 0으로 설정됩니다.
참고
이 형식은 IDisposable 인터페이스를 구현하지만 실제로 삭제하는 리소스는 없습니다. 즉 Dispose()를 직접 호출하거나 using
(C#) 또는 Using
(Visual Basic) 같은 언어 구문을 사용하여 삭제할 필요가 없습니다.
부호 없는 바이트 배열로 만든 메모리 스트림은 데이터의 크기를 조정할 수 없는 스트림을 제공합니다. 바이트 배열을 사용하는 경우 생성자에 전달된 매개 변수에 따라 기존 콘텐츠를 수정할 수 있지만 스트림을 추가하거나 축소할 수 없습니다. 빈 메모리 스트림은 크기가 조정 가능하며, 쓸 수 있고 읽을 수 있습니다.
개체가 MemoryStream ResX 파일 또는 .resources 파일에 추가되면 런타임에 GetStream 메서드를 호출하여 검색합니다.
개체가 MemoryStream 리소스 파일로 직렬화되면 실제로 .로 UnmanagedMemoryStream직렬화됩니다. 이 동작은 메서드를 거치 Stream 지 않고도 데이터에 대한 포인터를 직접 가져오는 기능뿐만 아니라 더 나은 성능을 제공합니다.
생성자
MemoryStream() |
0으로 초기화된 확장 가능한 용량을 사용하여 MemoryStream 클래스의 새 인스턴스를 초기화합니다. |
MemoryStream(Byte[]) |
지정된 바이트 배열을 기반으로 하는 MemoryStream 클래스의 크기 조정이 불가능한 새 인스턴스를 초기화합니다. |
MemoryStream(Byte[], Boolean) |
지정된 대로 설정된 MemoryStream 속성을 사용하여 지정된 바이트 배열을 기반으로 하는 CanWrite 클래스의 크기 조정이 불가능한 새 인스턴스를 초기화합니다. |
MemoryStream(Byte[], Int32, Int32) |
바이트 배열의 지정된 영역(인덱스)을 기반으로 하는 MemoryStream 클래스의 크기 조정이 불가능한 새 인스턴스를 초기화합니다. |
MemoryStream(Byte[], Int32, Int32, Boolean) |
지정된 대로 설정된 MemoryStream 속성을 사용하여 지정된 바이트 배열의 영역을 기반으로 하는 CanWrite 클래스의 크기 조정이 불가능한 새 인스턴스를 초기화합니다. |
MemoryStream(Byte[], Int32, Int32, Boolean, Boolean) |
지정된 대로 설정된 MemoryStream 속성과 지정된 대로 설정된 CanWrite 호출 기능을 사용하여 지정된 바이트 배열의 영역을 기반으로 하는 GetBuffer() 클래스의 새 인스턴스를 초기화합니다. |
MemoryStream(Int32) |
지정된 대로 초기화된 확장 가능한 용량을 사용하여 MemoryStream 클래스의 새 인스턴스를 초기화합니다. |
속성
CanRead |
현재 스트림이 읽기를 지원하는지를 나타내는 값을 가져옵니다. |
CanSeek |
현재 스트림이 검색을 지원하는지를 나타내는 값을 가져옵니다. |
CanTimeout |
현재 스트림이 시간 초과될 수 있는지를 결정하는 값을 가져옵니다. (다음에서 상속됨 Stream) |
CanWrite |
현재 스트림이 쓰기를 지원하는지를 나타내는 값을 가져옵니다. |
Capacity |
이 스트림에 할당된 바이트 수를 가져오거나 설정합니다. |
Length |
스트림의 길이(바이트)를 가져옵니다. |
Position |
스트림 내의 현재 위치를 가져오거나 설정합니다. |
ReadTimeout |
스트림 읽기 시도가 만료되기 전까지 기다릴 시간을 결정하는 값(밀리초)을 가져오거나 설정합니다. (다음에서 상속됨 Stream) |
WriteTimeout |
스트림 쓰기 시도가 만료되기 전까지 기다릴 시간을 결정하는 값(밀리초)을 가져오거나 설정합니다. (다음에서 상속됨 Stream) |
메서드
BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) |
비동기 읽기 작업을 시작합니다. 대신 ReadAsync(Byte[], Int32, Int32, CancellationToken)를 사용하세요. |
BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) |
비동기 읽기 작업을 시작합니다. 대신 ReadAsync(Byte[], Int32, Int32)를 사용하세요. (다음에서 상속됨 Stream) |
BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) |
비동기 쓰기 작업을 시작합니다. 대신 WriteAsync(Byte[], Int32, Int32, CancellationToken)를 사용하세요. |
BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) |
비동기 쓰기 작업을 시작합니다. 대신 WriteAsync(Byte[], Int32, Int32)를 사용하세요. (다음에서 상속됨 Stream) |
Close() |
읽기 및 쓰기용 스트림을 닫습니다. |
Close() |
현재 스트림을 닫고 현재 스트림과 관련된 소켓과 파일 핸들 등의 리소스를 모두 해제합니다. 이 메서드를 호출하는 대신 스트림이 올바르게 삭제되었는지 확인합니다. (다음에서 상속됨 Stream) |
CopyTo(Stream) |
현재 스트림에서 바이트를 읽어서 다른 스트림에 해당 바이트를 씁니다. 두 스트림 위치는 복사된 바이트 수만큼 고급입니다. (다음에서 상속됨 Stream) |
CopyTo(Stream, Int32) |
현재의 메모리 스트림에서 바이트를 읽고, 지정된 버퍼 크기를 활용하여 다른 스트림에 씁니다. |
CopyTo(Stream, Int32) |
현재 스트림에서 바이트를 읽어서 지정된 버퍼 크기로 다른 스트림에 씁니다. 두 스트림 위치는 복사된 바이트 수만큼 고급입니다. (다음에서 상속됨 Stream) |
CopyToAsync(Stream) |
현재 스트림에서 모든 바이트를 비동기적으로 읽어 다른 스트림에 씁니다. 두 스트림 위치는 복사된 바이트 수만큼 고급입니다. (다음에서 상속됨 Stream) |
CopyToAsync(Stream, CancellationToken) |
현재 스트림에서 바이트를 비동기적으로 읽어 지정된 취소 토큰을 사용하여 다른 스트림에 씁니다. 두 스트림 위치는 복사된 바이트 수만큼 고급입니다. (다음에서 상속됨 Stream) |
CopyToAsync(Stream, Int32) |
현재 스트림에서 바이트를 비동기적으로 읽어 지정된 버퍼 크기로 다른 스트림에 씁니다. 두 스트림 위치는 복사된 바이트 수만큼 고급입니다. (다음에서 상속됨 Stream) |
CopyToAsync(Stream, Int32, CancellationToken) |
현재 스트림에서 모든 바이트를 비동기적으로 읽어 지정된 버퍼 크기 및 취소 토큰을 사용하여 다른 스트림에 씁니다. |
CopyToAsync(Stream, Int32, CancellationToken) |
현재 스트림에서 바이트를 비동기적으로 읽어 지정된 버퍼 크기 및 취소 토큰을 사용하여 다른 스트림에 씁니다. 두 스트림 위치는 복사된 바이트 수만큼 고급입니다. (다음에서 상속됨 Stream) |
CreateObjRef(Type) |
원격 개체와 통신하는 데 사용되는 프록시 생성에 필요한 모든 관련 정보가 들어 있는 개체를 만듭니다. (다음에서 상속됨 MarshalByRefObject) |
CreateWaitHandle() |
사용되지 않습니다.
사용되지 않습니다.
사용되지 않습니다.
WaitHandle 개체를 할당합니다. (다음에서 상속됨 Stream) |
Dispose() |
Stream에서 사용하는 모든 리소스를 해제합니다. (다음에서 상속됨 Stream) |
Dispose(Boolean) |
MemoryStream 클래스에 사용되는 관리되지 않는 리소스를 해제하고, 필요에 따라 관리되는 리소스를 해제합니다. |
Dispose(Boolean) |
Stream에서 사용하는 관리되지 않는 리소스를 해제하고, 관리되는 리소스를 선택적으로 해제할 수 있습니다. (다음에서 상속됨 Stream) |
DisposeAsync() |
Stream에서 사용하는 관리되지 않는 리소스를 비동기적으로 해제합니다. (다음에서 상속됨 Stream) |
EndRead(IAsyncResult) |
보류 중인 비동기 읽기가 완료되기를 기다립니다. 대신 ReadAsync(Byte[], Int32, Int32, CancellationToken)를 사용하세요. |
EndRead(IAsyncResult) |
보류 중인 비동기 읽기가 완료되기를 기다립니다. 대신 ReadAsync(Byte[], Int32, Int32)를 사용하세요. (다음에서 상속됨 Stream) |
EndWrite(IAsyncResult) |
비동기 쓰기 작업을 끝냅니다. 대신 WriteAsync(Byte[], Int32, Int32, CancellationToken)를 사용하세요. |
EndWrite(IAsyncResult) |
비동기 쓰기 작업을 끝냅니다. 대신 WriteAsync(Byte[], Int32, Int32)를 사용하세요. (다음에서 상속됨 Stream) |
Equals(Object) |
지정된 개체가 현재 개체와 같은지 확인합니다. (다음에서 상속됨 Object) |
Flush() |
Flush() 메서드를 재정의하여 아무런 작업도 수행되지 않도록 합니다. |
FlushAsync() |
이 스트림에 대한 모든 버퍼를 비동기적으로 지우고 버퍼링된 모든 데이터가 내부 디바이스에 비동기적으로 쓰여지도록 합니다. (다음에서 상속됨 Stream) |
FlushAsync(CancellationToken) |
이 스트림에 대한 모든 버퍼를 비동기적으로 지우고 취소 요청을 모니터링합니다. |
FlushAsync(CancellationToken) |
이 스트림에 대해 모든 버퍼를 비동기적으로 지우고 버퍼링된 데이터가 내부 디바이스에 쓰여지도록 하고 취소 요청을 모니터링합니다. (다음에서 상속됨 Stream) |
GetBuffer() |
이 스트림을 만드는 데 사용된 부호 없는 바이트의 배열을 반환합니다. |
GetHashCode() |
기본 해시 함수로 작동합니다. (다음에서 상속됨 Object) |
GetLifetimeService() |
사용되지 않습니다.
이 인스턴스의 수명 정책을 제어하는 현재의 수명 서비스 개체를 검색합니다. (다음에서 상속됨 MarshalByRefObject) |
GetType() |
현재 인스턴스의 Type을 가져옵니다. (다음에서 상속됨 Object) |
InitializeLifetimeService() |
사용되지 않습니다.
이 인스턴스의 수명 정책을 제어하는 수명 서비스 개체를 가져옵니다. (다음에서 상속됨 MarshalByRefObject) |
MemberwiseClone() |
현재 Object의 단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
MemberwiseClone(Boolean) |
현재 MarshalByRefObject 개체의 단순 복사본을 만듭니다. (다음에서 상속됨 MarshalByRefObject) |
ObjectInvariant() |
이 API는 제품 인프라를 지원하며 코드에서 직접 사용되지 않습니다. Contract에 대한 지원을 제공합니다. |
ObjectInvariant() |
사용되지 않습니다.
Contract에 대한 지원을 제공합니다. (다음에서 상속됨 Stream) |
Read(Byte[], Int32, Int32) |
현재 스트림에서 바이트 블록을 읽어서 버퍼에 씁니다. |
Read(Span<Byte>) |
현재의 메모리 스트림에서 바이트 시퀀스를 읽고, 읽은 바이트 수만큼 메모리 스트림 내의 위치를 앞으로 이동합니다. |
Read(Span<Byte>) |
파생 클래스에서 재정의되면 현재 스트림에서 바이트의 시퀀스를 읽고, 읽은 바이트 수만큼 스트림 내에서 앞으로 이동합니다. (다음에서 상속됨 Stream) |
ReadAsync(Byte[], Int32, Int32) |
현재 스트림에서 바이트 시퀀스를 읽고 읽은 바이트 수만큼 스트림에서 위치를 비동기적으로 앞으로 이동합니다. (다음에서 상속됨 Stream) |
ReadAsync(Byte[], Int32, Int32, CancellationToken) |
현재 스트림에서 바이트의 시퀀스를 비동기적으로 읽고 읽은 바이트 수만큼 스트림 내에서 앞으로 이동하며 취소 요청을 모니터링합니다. |
ReadAsync(Byte[], Int32, Int32, CancellationToken) |
현재 스트림에서 바이트의 시퀀스를 비동기적으로 읽고 읽은 바이트 수만큼 스트림 내에서 앞으로 이동하며 취소 요청을 모니터링합니다. (다음에서 상속됨 Stream) |
ReadAsync(Memory<Byte>, CancellationToken) |
현재의 메모리 스트림에서 바이트 시퀀스를 비동기적으로 읽어 |
ReadAsync(Memory<Byte>, CancellationToken) |
현재 스트림에서 바이트의 시퀀스를 비동기적으로 읽고 읽은 바이트 수만큼 스트림 내에서 앞으로 이동하며 취소 요청을 모니터링합니다. (다음에서 상속됨 Stream) |
ReadAtLeast(Span<Byte>, Int32, Boolean) |
현재 스트림에서 최소 바이트 수를 읽고 읽은 바이트 수만큼 스트림 내의 위치를 이동합니다. (다음에서 상속됨 Stream) |
ReadAtLeastAsync(Memory<Byte>, Int32, Boolean, CancellationToken) |
현재 스트림에서 최소 바이트 수를 비동기적으로 읽고, 읽은 바이트 수만큼 스트림 내의 위치를 이동하고, 취소 요청을 모니터링합니다. (다음에서 상속됨 Stream) |
ReadByte() |
현재 스트림에서 바이트를 읽습니다. |
ReadExactly(Byte[], Int32, Int32) |
|
ReadExactly(Span<Byte>) |
현재 스트림에서 바이트를 읽고 스트림이 채워질 때까지 스트림 내의 |
ReadExactlyAsync(Byte[], Int32, Int32, CancellationToken) |
현재 스트림에서 바이트 수를 비동기적으로 읽고 |
ReadExactlyAsync(Memory<Byte>, CancellationToken) |
현재 스트림에서 바이트를 비동기적으로 읽고, 스트림이 채워질 때까지 |
Seek(Int64, SeekOrigin) |
현재 스트림 내의 위치를 지정된 값으로 설정합니다. |
SetLength(Int64) |
현재 스트림의 길이를 지정된 값으로 설정합니다. |
ToArray() |
Position 속성에 관계없이 바이트 배열에 스트림 내용을 씁니다. |
ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
TryGetBuffer(ArraySegment<Byte>) |
이 스트림을 만드는 데 사용된 부호 없는 바이트의 배열을 반환합니다. 반환 값은 변환의 성공 여부를 나타냅니다. |
Write(Byte[], Int32, Int32) |
버퍼에서 읽은 데이터를 사용하여 현재 스트림에 바이트 블록을 씁니다. |
Write(ReadOnlySpan<Byte>) |
|
Write(ReadOnlySpan<Byte>) |
파생 클래스를 재정의될 때 현재 스트림에 바이트의 시퀀스를 쓰고 쓰여진 바이트 수만큼 이 스트림 내에서 앞으로 이동합니다. (다음에서 상속됨 Stream) |
WriteAsync(Byte[], Int32, Int32) |
현재 스트림에 바이트 시퀀스를 비동기적으로 쓰고 쓴 바이트 수만큼 이 스트림에서 현재 위치를 앞으로 이동합니다. (다음에서 상속됨 Stream) |
WriteAsync(Byte[], Int32, Int32, CancellationToken) |
바이트의 시퀀스를 현재 스트림에 비동기적으로 쓰고 쓰여진 바이트 수만큼 이 스트림 내의 현재 위치를 앞으로 이동한 후 취소 요청을 모니터링합니다. |
WriteAsync(Byte[], Int32, Int32, CancellationToken) |
바이트의 시퀀스를 현재 스트림에 비동기적으로 쓰고 쓰여진 바이트 수만큼 이 스트림 내의 현재 위치를 앞으로 이동한 후 취소 요청을 모니터링합니다. (다음에서 상속됨 Stream) |
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) |
|
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) |
바이트의 시퀀스를 현재 스트림에 비동기적으로 쓰고 쓰여진 바이트 수만큼 이 스트림 내의 현재 위치를 앞으로 이동한 후 취소 요청을 모니터링합니다. (다음에서 상속됨 Stream) |
WriteByte(Byte) |
현재 위치에서 현재 스트림에 바이트를 씁니다. |
WriteTo(Stream) |
다른 스트림에 이 메모리 스트림의 전체 내용을 씁니다. |
명시적 인터페이스 구현
IDisposable.Dispose() |
Stream에서 사용하는 모든 리소스를 해제합니다. (다음에서 상속됨 Stream) |
확장 메서드
AsInputStream(Stream) |
Windows 스토어 앱용 .NET의 관리형 스트림을 Windows 런타임의 입력 스트림으로 변환합니다. |
AsOutputStream(Stream) |
Windows 스토어 앱용 .NET의 관리형 스트림을 Windows 런타임의 출력 스트림으로 변환합니다. |
AsRandomAccessStream(Stream) |
지정된 스트림을 임의 액세스 스트림으로 변환합니다. |
GetWindowsRuntimeBuffer(MemoryStream) |
지정된 메모리 스트림과 동일한 메모리를 나타내는 Windows.Storage.Streams.IBuffer 인터페이스를 반환합니다. |
GetWindowsRuntimeBuffer(MemoryStream, Int32, Int32) |
지정된 메모리 스트림을 나타내는 메모리 내 영역을 나타내는 Windows.Storage.Streams.IBuffer 인터페이스를 반환합니다. |
ConfigureAwait(IAsyncDisposable, Boolean) |
비동기 일회용에서 반환되는 작업을 대기하는 방법을 구성합니다. |