EndOfStreamException Class
The exception that is thrown when reading is attempted past the end of a stream.
For a list of all members of this type, see EndOfStreamException Members.
System.Object
System.Exception
System.SystemException
System.IO.IOException
System.IO.EndOfStreamException
[Visual Basic] <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
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
EndOfStreamException uses the HRESULT COR_E_ENDOFSTREAM which has the value 0x80070026.
Example
[Visual Basic, C#, C++] The following code example shows how to read and write Double data to memory by using the BinaryReader and BinaryWriter classes on top of the MemoryStream class.
[Visual Basic] 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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.IO
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework
Assembly: Mscorlib (in Mscorlib.dll)
See Also
EndOfStreamException Members | System.IO Namespace | Exception | Handling and Throwing Exceptions | Working with I/O | Reading Text from a File | Writing Text to a File