EndOfStreamException クラス
ストリームの末尾を越えて読み込もうとしたときにスローされる例外。
この型のすべてのメンバの一覧については、EndOfStreamException メンバ を参照してください。
System.Object
System.Exception
System.SystemException
System.IO.IOException
System.IO.EndOfStreamException
<Serializable>
Public Class EndOfStreamException Inherits IOException
[C#]
[Serializable]
public class EndOfStreamException : IOException
[C++]
[Serializable]
public __gc class EndOfStreamException : public IOException
[JScript]
public
Serializable
class EndOfStreamException extends IOException
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
EndOfStreamException は、値 0x80070026 の HRESULT COR_E_ENDOFSTREAM を使用します。
使用例
[Visual Basic, C#, C++] 次に示すのは、 MemoryStream クラス上で BinaryReader クラスと BinaryWriter クラスを使用し、メモリに対して Double データの読み取りと書き込みを実行するコード例です。
Imports System
Imports System.IO
Public Class BinaryRW
Shared Sub Main()
Dim i As Integer
Const upperBound As Integer = 1000
' Create random data to write to the stream.
Dim dataArray(upperBound) As Double
Dim randomGenerator As New Random()
For i = 0 To upperBound
dataArray(i) = 100.1 * randomGenerator.NextDouble()
Next i
Dim binWriter As New BinaryWriter(New MemoryStream())
Try
' Write data to the stream.
Console.WriteLine("Writing data to the stream.")
For i = 0 To upperBound
binWriter.Write(dataArray(i))
Next i
' Create a reader using the stream from the writer.
Dim binReader As New BinaryReader(binWriter.BaseStream)
' Return to the beginning of the stream.
binReader.BaseStream.Position = 0
' Read and verify the data.
Try
Console.WriteLine("Verifying the written data.")
For i = 0 To upperBound
If binReader.ReadDouble() <> dataArray(i) Then
Console.WriteLine("Error writing data.")
Exit For
End If
Next i
Console.WriteLine("The data was written and verified.")
Catch ex As EndOfStreamException
Console.WriteLine("Error writing data: {0}.", _
ex.GetType().Name)
End Try
Finally
binWriter.Close()
End Try
End Sub
End Class
[C#]
using System;
using System.IO;
class BinaryRW
{
static void Main()
{
int i;
const int arrayLength = 1000;
// Create random data to write to the stream.
Random randomGenerator = new Random();
double[] dataArray = new double[arrayLength];
for(i = 0; i < arrayLength; i++)
{
dataArray[i] = 100.1 * randomGenerator.NextDouble();
}
using(BinaryWriter binWriter =
new BinaryWriter(new MemoryStream()))
{
// Write the data to the stream.
Console.WriteLine("Writing data to the stream.");
for(i = 0; i < arrayLength; i++)
{
binWriter.Write(dataArray[i]);
}
// Create a reader using the stream from the writer.
using(BinaryReader binReader =
new BinaryReader(binWriter.BaseStream))
{
try
{
// Return to the beginning of the stream.
binReader.BaseStream.Position = 0;
// Read and verify the data.
Console.WriteLine("Verifying the written data.");
for(i = 0; i < arrayLength; i++)
{
if(binReader.ReadDouble() != dataArray[i])
{
Console.WriteLine("Error writing data.");
break;
}
}
Console.WriteLine("The data was written " +
"and verified.");
}
catch(EndOfStreamException e)
{
Console.WriteLine("Error writing data: {0}.",
e.GetType().Name);
}
}
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
void main()
{
int i;
const int arrayLength = 1000;
// Create random data to write to the stream.
double dataArray __gc[] = new double __gc[arrayLength];
Random* randomGenerator = new Random();
for(i = 0; i < arrayLength; i++)
{
dataArray[i] = 100.1 * randomGenerator->NextDouble();
}
BinaryWriter* binWriter =
new BinaryWriter(new MemoryStream());
try
{
// Write data to the stream.
Console::WriteLine(S"Writing data to the stream.");
i = 0;
for(i = 0; i < arrayLength; i++)
{
binWriter->Write(dataArray[i]);
}
// Create a reader using the stream from the writer.
BinaryReader* binReader =
new BinaryReader(binWriter->BaseStream);
// Return to the beginning of the stream.
binReader->BaseStream->Position = 0;
try
{
// Read and verify the data.
i = 0;
Console::WriteLine(S"Verifying the written data.");
for(i = 0; i < arrayLength; i++)
{
if(binReader->ReadDouble() != dataArray[i])
{
Console::WriteLine(S"Error writing data.");
break;
}
}
Console::WriteLine(S"The data was written and verified.");
}
catch(EndOfStreamException* e)
{
Console::WriteLine(S"Error writing data: {0}.",
e->GetType()->Name);
}
}
__finally
{
binWriter->Close();
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.IO
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
アセンブリ: Mscorlib (Mscorlib.dll 内)
参照
EndOfStreamException メンバ | System.IO 名前空間 | Exception | 例外の処理とスロー | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み