Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Text
Imports System.Net.Sockets
Namespace NETWORK
Public Class BigEndianReader
Implements IDisposable
Private m_BinaryReader As IO.BinaryReader
' Methods
Public ReadOnly Property BaseStream() As IO.Stream
Get
Return m_BinaryReader.BaseStream
End Get
End Property
Public ReadOnly Property BytesAvailable() As Long
Get
Return (m_BinaryReader.BaseStream.Length - m_BinaryReader.BaseStream.Position)
End Get
End Property
Public ReadOnly Property Position() As Long
Get
Return m_BinaryReader.BaseStream.Position
End Get
End Property
Public Sub New()
m_BinaryReader = New IO.BinaryReader(New IO.MemoryStream(), Encoding.UTF8)
End Sub
Public Sub New(content As Byte())
m_BinaryReader = New IO.BinaryReader(New IO.MemoryStream(content), Encoding.UTF8)
End Sub
Public Sub New(stream As IO.Stream)
m_BinaryReader = New IO.BinaryReader(stream, Encoding.UTF8)
End Sub
Public Function ReadBytes(count As Integer) As Byte()
Return m_BinaryReader.ReadBytes(count)
End Function
Public Function ReadBoolean() As Boolean
Return m_BinaryReader.ReadByte() = 1
End Function
Public Function ReadVarShort() As Short
Dim resultVar = 0
For offset As Integer = 0 To 15 Step 7
Dim readByte As Byte = Me.ReadByte()
Dim hasContinuationFlag As Boolean = (readByte And 128) = 128
Dim extractedValue As Integer = (readByte And 127)
If offset > 0 Then
extractedValue = extractedValue << offset
End If
resultVar += extractedValue
If hasContinuationFlag = False Then
If resultVar > 32767 Then
resultVar -= 65536
End If
Return CShort(resultVar)
End If
Next
Throw New Exception("Too much data")
End Function
Public Function ReadVarInt() As Integer
Dim resultVar As Integer = 0
For offset As Integer = 0 To 31 Step 7
Dim readByte__1 As Byte = ReadByte()
Dim hasContinuationFlag As Boolean = (readByte__1 And 128) = 128
Dim extractedValue As Integer = (readByte__1 And 127)
If offset > 0 Then
'TODO: not sure if the if statement is needed
extractedValue = extractedValue << offset
End If
resultVar += extractedValue
If hasContinuationFlag = False Then
Return resultVar
End If
Next
Throw New Exception("Too much data")
End Function
Public Function ReadByte() As Byte
Return m_BinaryReader.ReadByte()
End Function
Public Function ReadDouble() As Double
Return BitConverter.ToDouble(ConvertToBigEndian(8), 0)
End Function
Public Function ReadShort() As Short
Return BitConverter.ToInt16(ConvertToBigEndian(2), 0)
End Function
Public Function ReadInt32() As Integer
Return BitConverter.ToInt32(ConvertToBigEndian(4), 0)
End Function
Public Function ReadSByte() As SByte
Return m_BinaryReader.ReadSByte()
End Function
Public Function ReadSingle() As Single
Return BitConverter.ToSingle(ConvertToBigEndian(4), 0)
End Function
Public Function ReadUTF() As String
Dim stringLength As UShort = ReadUShort()
Dim stringContentInByte As Byte() = ReadBytes(stringLength)
Return Encoding.UTF8.GetString(stringContentInByte)
End Function
Public Function ReadUShort() As UInt16
Return BitConverter.ToUInt16(ConvertToBigEndian(2), 0)
End Function
Public Function ReadUInt32() As UInt32
Return BitConverter.ToUInt32(ConvertToBigEndian(4), 0)
End Function
Public Sub Seek(position As Integer)
m_BinaryReader.BaseStream.Position = position
End Sub
Public Sub Add(data As Byte(), offset As Integer, count As Integer)
Dim pos As Long = m_BinaryReader.BaseStream.Position
m_BinaryReader.BaseStream.Position = m_BinaryReader.BaseStream.Length
m_BinaryReader.BaseStream.Write(data, offset, count)
m_BinaryReader.BaseStream.Position = pos
End Sub
Private Function ConvertToBigEndian(ByVal count As Integer) As Byte()
Dim convertedContent As Byte() = New Byte(count - 1) {}
For index As Integer = count - 1 To 0 Step -1
convertedContent(index) = CByte(m_BinaryReader.BaseStream.ReadByte())
Next
Return convertedContent
End Function
Public Sub Dispose() Implements IDisposable.Dispose
m_BinaryReader.Dispose()
m_BinaryReader = Nothing
End Sub
End Class
End Namespace