UnmanagedMemoryStream.Read メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > UnmanagedMemoryStream.Read メソッドの意味・解説 

UnmanagedMemoryStream.Read メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定したバイト数を指定した配列読み取ります。

このメソッドは、CLS準拠していません。  

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

Public Overrides Function
 Read ( _
    <InAttribute> <OutAttribute> buffer As Byte(),
 _
    offset As Integer, _
    count As Integer _
) As Integer
public override int Read (
    [InAttribute] [OutAttribute] byte[] buffer,
    int offset,
    int count
)
public:
virtual int Read (
    [InAttribute] [OutAttribute] array<unsigned char>^ buffer,
 
    int offset, 
    int count
) override
public int Read (
    /** @attribute InAttribute() */ /** @attribute OutAttribute() */ byte[] buffer,
 
    int offset, 
    int count
)

パラメータ

buffer

このメソッド返されるときに、指定したバイト配列offset から (offset + count - 1) までの値が、現在のソースから読み取られたバイト置き換えられます。このパラメータ初期化せずに渡されます。

offset

現在のストリームから読み取ったデータ格納開始する位置を示す buffer内のバイト オフセットインデックス番号は 0 から始まります

count

現在のストリームから読み取る最大バイト数。

戻り値
バッファ読み取られ合計バイト数。要求しただけのバイト数を読み取ることができなかった場合、この値は要求したバイト数より小さくなりますストリーム末尾到達した場合は 0 (ゼロ) になることがあります

例外例外
例外種類条件

ObjectDisposedException

ストリーム閉じられました。

NotSupportedException

基になるメモリ読み取りサポートしていません。

または

CanRead プロパティfalse設定されています。

ArgumentNullException

buffer パラメータnull 参照 (Visual Basic では Nothing) が設定されています。

ArgumentOutOfRangeException

offset パラメータが 0 未満です。

または

count パラメータが 0 未満です。

ArgumentException

バッファ配列長さから offset パラメータ引いた値が、count パラメータ未満です。

解説解説
使用例使用例

UnmanagedMemoryStream クラス使用して、アンマネージ メモリから読み取る方法と、アンマネージ メモリ書き込む方法次のコード例示します。アンマネージ メモリブロックは、Marshal クラス使用して割り当ておよび割り当て解除されます。

// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe

using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

unsafe class TestWriter
{
    
    static void Main()
    {
        
            // Create som data to read and write.
            byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some
 data.");

        // Allocate a block of unmanaged memory and return an IntPtr
 object.    
            IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);

            // Get a byte pointer from the IntPtr object.
            byte* memBytePtr = (byte*) memIntPtr.ToPointer();

            // Create an UnmanagedMemoryStream object using a pointer
 to unmanaged memory.
            UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr,
 message.Length, message.Length, FileAccess.Write);

            // Write the data.
            writeStream.Write(message, 0, message.Length);

            // Close the stream.
            writeStream.Close();

            // Create another UnmanagedMemoryStream object using a pointer
 to unmanaged memory.
            UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr,
 message.Length, message.Length, FileAccess.Read);

        // Create a byte array to hold data from unmanaged memory.
            byte[] outMessage = new byte[message.Length];

            // Read from unmanaged memory to the byte array.
            readStream.Read(outMessage, 0, message.Length);

            // Close the stream.
            readStream.Close();

            // Display the data to the console.
            Console.WriteLine(UnicodeEncoding.Unicode.GetString(outMessage));

            // Free the block of unmanaged memory.
            Marshal.FreeHGlobal(memIntPtr);

            Console.ReadLine();
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
UnmanagedMemoryStream クラス
UnmanagedMemoryStream メンバ
System.IO 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からUnmanagedMemoryStream.Read メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からUnmanagedMemoryStream.Read メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からUnmanagedMemoryStream.Read メソッド を検索

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

辞書ショートカット

すべての辞書の索引

UnmanagedMemoryStream.Read メソッドのお隣キーワード
検索ランキング

   

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



UnmanagedMemoryStream.Read メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS