MemoryStream クラスとは? わかりやすく解説

MemoryStream クラス

バッキング ストアとしてメモリ使用するストリーム作成します

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class MemoryStream
    Inherits Stream
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class MemoryStream : Stream
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class MemoryStream : public
 Stream
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class MemoryStream extends Stream
SerializableAttribute 
ComVisibleAttribute(true) 
public class MemoryStream extends
 Stream
解説解説

ファイル作成およびテキストファイルへの書き込み例については、「方法 : ファイルテキスト書き込む」を参照してくださいファイルからのテキスト読み取り例については、「方法 : ファイルかテキスト読み取る」を参照してくださいバイナリ ファイル読み取りおよび書き込み例については、「方法 : 新しく作成されデータ ファイルに対して読み書きする」を参照してください

MemoryStream クラスは、バッキング ストアとしてディスクネットワーク接続ではなくメモリ使用するストリーム作成しますMemoryStream は、MemoryStream オブジェクトの作成時に初期化される符号なしバイト配列として格納されるデータカプセル化ます。配列は空の配列としても作成できますカプセル化されたデータは、メモリ直接アクセスできますメモリ ストリーム使用すると、アプリケーションでの一時バッファ一時ファイル必要性を減らすことができます

ストリームcurrent position は、次の読み取りまたは書き込み操作実行される位置です。現在の位置は、Seek メソッド取得または設定できますMemoryStream新しインスタンス作成するときに、現在の位置ゼロ設定されます。

符号なしバイト配列作成したメモリ ストリームには、サイズ変更できないデータストリーム ビュー機能用意されています。これは書き込み専用です。バイト配列使用する場合ストリーム追加したり、ストリーム縮小したりできません。ただし、コンストラクタに渡すパラメータによっては、既存内容変更できることがあります。空のメモリ ストリームは、サイズ変更書き込み、および読み取りを行うことができます

MemoryStream オブジェクトを ResX ファイルまたは .resources ファイル追加した場合実行時に GetStream メソッド呼び出してこのオブジェクト取得します

MemoryStream オブジェクトリソース ファイルシリアル化される場合、このオブジェクト実際には UnmanagedMemoryStream としてシリアル化されます。これにより、パフォーマンス向上するだけでなく、Stream メソッド使用せずデータへのポインタ直接取得できます

Windows Mobile for Pocket PCWindows Mobile for SmartphoneWindows CE プラットフォームメモ : Windows CE では、クリップボードから貼り付けられメモリ ストリームは、クリップボードコピーされメモリ ストリームよりもわずかに大きなサイズ保持できます。元のメモリ ストリーム最後に追加バイト付加できるからです。メモリ ストリーム厳密に受け取るには、オブジェクトメモリ ストリーム受け取方法判断するためのサイズプレフィックスとしてオブジェクト追加するか、メモリ ストリームとそのサイズ文字列値を含む DataObject をクリップボードコピーします

使用例使用例

メモリバッキング ストアとして使用してデータ読み書きする方法の例を次に示します

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()

        ' 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
            ' 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
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 = {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++] = 
                    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);
        }
    }
}
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();
   }
}
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 = {0}, Length = {1}, Position = {2}\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
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「MemoryStream クラス」の関連用語

MemoryStream クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



MemoryStream クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS