BinaryReader in .Net Framework 4.0

StewartBW 1,560 Reputation points
2025-06-07T11:48:17.93+00:00

Hi experts,

In .NET Framework 4.8, I simply use:

Using MyBinaryReader As New BinaryReader(InputStream, Encoding.ASCII, leaveOpen:=True)

To leave the InputStream open afterward, however, in .NET Framework 4.0 there's no 3rd parameter so can't leaveOpen.

Tried to use:

Dim MyBinaryReader As New BinaryReader(InputStream, Encoding.ASCII)

And MyBinaryReader.Close afterward, however, InputStream still will dispose even if not using MyBinaryReader.Dispose, and only close it, so what's the solution in .NET Framework 4.0 to get rid of this situation?

Thanks.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,872 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 122K Reputation points
    2025-06-07T13:24:31.3933333+00:00

    Try this:

    Using MyBinaryReader As New BinaryReader(New H(InputStream), Encoding.ASCII)
        . . .
    End Using
    

    where H is:

    Class H
        Inherits Stream
    
        Private s As Stream
    
        Public Sub New(s As Stream)
            Me.s = s
        End Sub
    
        Public Overrides ReadOnly Property CanRead As Boolean
            Get
                Return s.CanRead
            End Get
        End Property
    
        Public Overrides ReadOnly Property CanSeek As Boolean
            Get
                Return s.CanSeek
            End Get
        End Property
    
        Public Overrides ReadOnly Property CanWrite As Boolean
            Get
                Return s.CanWrite
            End Get
        End Property
    
        Public Overrides ReadOnly Property Length As Long
            Get
                Return s.Length
            End Get
        End Property
    
        Public Overrides Property Position As Long
            Get
                Return s.Position
            End Get
            Set(value As Long)
                s.Position = value
            End Set
        End Property
    
        Public Overrides Sub Flush()
            s.Flush()
        End Sub
    
        Public Overrides Sub SetLength(value As Long)
            s.SetLength(value)
        End Sub
    
        Public Overrides Sub Write(buffer() As Byte, offset As Integer, count As Integer)
            s.Write(buffer, offset, count)
        End Sub
    
        Public Overrides Function Seek(offset As Long, origin As SeekOrigin) As Long
            Return s.Seek(offset, origin)
        End Function
    
        Public Overrides Function Read(buffer() As Byte, offset As Integer, count As Integer) As Integer
            Return s.Read(buffer, offset, count)
        End Function
    End Class
    

    If required, use InputStream.Seek(0, SeekOrigin.Begin) before rereading the stream.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.