Stream.Read メソッド
アセンブリ: mscorlib (mscorlib.dll 内)
構文
Public MustOverride Function Read ( _ <InAttribute> <OutAttribute> buffer As Byte(), _ offset As Integer, _ count As Integer _ ) As Integer
Dim instance As Stream Dim buffer As Byte() Dim offset As Integer Dim count As Integer Dim returnValue As Integer returnValue = instance.Read(buffer, offset, count)
public: virtual int Read ( [InAttribute] [OutAttribute] array<unsigned char>^ buffer, int offset, int count ) abstract
public abstract int Read ( /** @attribute InAttribute() */ /** @attribute OutAttribute() */ byte[] buffer, int offset, int count )
戻り値
バッファに読み取られた合計バイト数。要求しただけのバイト数を読み取ることができなかった場合、この値は要求したバイト数より小さくなります。ストリームの末尾に到達した場合は 0 (ゼロ) になることがあります。
ファイルの作成およびテキストのファイルへの書き込みの例については、「方法 : ファイルにテキストを書き込む」を参照してください。ファイルからのテキストの読み取りの例については、「方法 : ファイルからテキストを読み取る」を参照してください。バイナリ ファイルの読み取りおよび書き込みの例については、「方法 : 新しく作成されたデータ ファイルに対して読み書きする」を参照してください。
現在のインスタンスが読み取りをサポートしているかどうかを判断するには、CanRead プロパティを使用します。
このメソッドの実装は、現在のストリームから count で指定した最大バイト数だけ読み込み、読み込んだバイトを buffer 内の offset で始まる位置に格納します。ストリームの現在位置が読み込んだバイト数だけ進みます。ただし、例外が発生した場合は、ストリーム内の現在位置はそのまま変わりません。読み込んだバイト数が返されます。現在位置がストリームの末尾にある場合、戻り値は 0 です。読み込み可能なデータがない場合は、少なくとも 1 バイトのデータが読み込み可能になるまでブロックします。Read が 0 を返すのは、ストリーム内にそれ以上データがない場合とそれ以上読み込み可能な対象 (閉じたソケットやファイル終端など) を予期していない場合だけです。メソッドの実装は、ストリームの末尾に到達していない場合でも、要求された数に満たないバイトを返すようにすることができます。
Read を使用してデータ ブロックを読み取る方法を次の例に示します。
Imports System Imports System.IO Imports Microsoft.VisualBasic Public Class Block Public Shared Sub Main() Dim s As New MemoryStream() Dim i As Integer For i = 0 To 99 s.WriteByte(CByte(i)) Next i s.Position = 0 ' Now read in s into a byte buffer. Dim bytes(s.Length) As Byte Dim numBytesToRead As Integer = CInt(s.Length) Dim numBytesRead As Integer = 0 While numBytesToRead > 0 ' Read can return anything from 0 to numBytesToRead. Dim n As Integer = s.Read(bytes, numBytesRead, numBytesToRead) ' The end of the file has been reached. If n = 0 Then Exit While End If numBytesRead += n numBytesToRead -= n End While s.Close() ' numBytesToRead should be 0 now, and numBytesRead should ' equal 100. Console.WriteLine("number of bytes read: " & numBytesRead.ToString()) End Sub End Class
using System; using System.IO; public class Block { public static void Main() { Stream s = new MemoryStream(); for (int i=0; i<100; i++) s.WriteByte((byte)i); s.Position = 0; // Now read s into a byte buffer. byte[] bytes = new byte[s.Length]; int numBytesToRead = (int) s.Length; int numBytesRead = 0; while (numBytesToRead > 0) { // Read may return anything from 0 to numBytesToRead. int n = s.Read(bytes, numBytesRead, numBytesToRead); // The end of the file is reached. if (n==0) break; numBytesRead += n; numBytesToRead -= n; } s.Close(); // numBytesToRead should be 0 now, and numBytesRead should // equal 100. Console.WriteLine("number of bytes read: "+numBytesRead); } }
using namespace System; using namespace System::IO; int main() { Stream^ s = gcnew MemoryStream; for ( int i = 0; i < 100; i++ ) s->WriteByte( (Byte)i ); s->Position = 0; // Now read s into a byte buffer. array<Byte>^bytes = gcnew array<Byte>(s->Length); int numBytesToRead = (int)s->Length; int numBytesRead = 0; while ( numBytesToRead > 0 ) { // Read may return anything from 0 to numBytesToRead. int n = s->Read( bytes, numBytesRead, numBytesToRead ); // The end of the file is reached. if ( n == 0 ) break; numBytesRead += n; numBytesToRead -= n; } s->Close(); // numBytesToRead should be 0 now, and numBytesRead should // equal 100. Console::WriteLine( "number of bytes read: {0}", numBytesRead ); }
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Weblioに収録されているすべての辞書からStream.Read メソッドを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からStream.Read メソッドを検索
- Stream.Read メソッドのページへのリンク