Imports System.Runtime.CompilerServices
Namespace Extensiones
    Public Module BinaryReaderExtensions
        <Extension()>
        Public Function ReadAllBytes(ByVal reader As IO.BinaryReader) As Byte()
            Const bufferSize As Integer = 4095

            Using ms = New IO.MemoryStream()
                Dim buffer As Byte() = New Byte(bufferSize) {}
                Dim count As Integer
                Dim bFinish As Boolean = False

                Do Until bFinish
                    count = reader.Read(buffer, 0, buffer.Length)
                    If count = 0 Then
                        bFinish = True
                    Else
                        ms.Write(buffer, 0, count)
                    End If
                Loop

                'While (count = reader.Read(buffer, 0, buffer.Length)) <> 0
                '    ms.Write(buffer, 0, count)
                'End While

                Return ms.ToArray()
            End Using
        End Function
    End Module
    Public Class LineReader
        Inherits IO.BinaryReader

        Public Sub New(ByVal stream As IO.Stream, ByVal encoding As Text.Encoding)
            MyBase.New(stream, encoding)
        End Sub

        Public currentPos As Integer
        Private stringBuffer As Text.StringBuilder

        Public Function ReadLine() As String
            currentPos = 0
            Dim buf As Char() = New Char(0) {}
            stringBuffer = New Text.StringBuilder()
            Dim lineEndFound As Boolean = False

            While MyBase.Read(buf, 0, 1) > 0
                currentPos += 1

                If buf(0) = Microsoft.VisualBasic.Strings.ChrW(10) Then
                    lineEndFound = True
                Else
                    stringBuffer.Append(buf(0))
                End If

                If lineEndFound Then
                    Return stringBuffer.ToString()
                End If
            End While

            Return stringBuffer.ToString()
        End Function
    End Class

End Namespace