📄 binaryreader.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "BinaryReader"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
' CopyRight (c) 2005 Kelly Ethridge
'
' This file is part of VBCorLib.
'
' VBCorLib is free software; you can redistribute it and/or modify
' it under the terms of the GNU Library General Public License as published by
' the Free Software Foundation; either version 2.1 of the License, or
' (at your option) any later version.
'
' VBCorLib is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU Library General Public License for more details.
'
' You should have received a copy of the GNU Library General Public License
' along with Foobar; if not, write to the Free Software
' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'
' Module: BinaryReader
'
''
' Provides a set of methods to read typed information from a Stream.
'
' @remarks
' <p>The bytes from a stream can be read and cast to a specific datatype. Each
' datatype will determine the number of bytes to be read from the stream. Once
' the number of bytes have been read, they are assembled into the requested
' datatype, advancing the position in the stream the required number of bytes.</p>
' <p>Characters can be read from the Stream using the provided Encoding system.
' The default encoding system is equal to New UTF8Encoding.</p>
' <p>To create a new <b>BinaryReader</b> use the public constructor.
' <pre>
' Set reader = Cor.NewBinaryReader(MyFileStream)
'
' - or -
'
' Set reader = NewBinaryReader(MyFileStream)
' </pre></p>
' @see Constructors
' @see BinaryWriter
'
Option Explicit
Implements IObject
Private Const DEF_CAPACITY As Long = 16
Private Const MIN_DATE As Double = -657434#
Private Const MAX_DATE As Double = 2958465.99998843
Private mStream As Stream
Private mEncoding As Encoding
Private mIsOpen As Boolean
Private mBuffer() As Byte
Private mCapacity As Long
Private mPtrBuffer As Long
Private mOneChar(0) As Integer
''
' Returns the underlying Stream being read by the Reader.
'
' @return The underlying Stream.
'
Public Property Get BaseStream() As Stream
Set BaseStream = mStream
End Property
''
' Closes the reader and underlying Stream.
'
Public Sub CloseReader()
Call mStream.CloseStream
End Sub
''
' Fills either a Byte array or Integer array with the specified number of elements. Or returns
' the next character to be decoded from the stream.
'
' @param Buffer The array to write the data to.
' @param Index The first index in <i>Buffer</i> to start writing data.
' @param Count The number of elements to be written to the array.
' @return The actual number of elements written to the array. This could be less than the requested amount.
' @remarks If a Byte array is passed in, then the bytes from the underlying stream are
' copied to the array. If an Integer array is passed in, then enough bytes are read from
' the stream to produce the requested number of decoded characters. The decoded characters
' are placed in the array starting at <i>Index</i>.
' <p>If all parameters are missing then a single character will be decoded from the stream and returned.</p>
' <p>All parameters must either be missing or supplied or an exception is thrown.</p>
'
Public Function Read(Optional ByRef Buffer As Variant, Optional ByRef Index As Variant, Optional ByRef Count As Variant) As Long
Select Case CLng(IsMissing(Buffer)) + CLng(IsMissing(Index)) + CLng(IsMissing(Count))
Case -3: Read = ReadOneChar
Case 0: Read = ReadBuffer(Buffer, Index, Count)
Case Else: Throw Cor.NewInvalidOperationException("All parameters must be supplied or missing.")
End Select
End Function
''
' Returns the next character to be decoded from the byte stream. The position
' in the stream is not advanced after the read.
'
' @return The next character to be Read, or -1 if no more characters are in the stream.
' @remarks This function requires the stream to support Seeking. If it does not,
' then -1 will always be returned.
'
Public Function PeekChar() As Long
Call VerifyIsOpen
If mStream.CanSeek Then
Dim pos As Long
pos = mStream.Position
PeekChar = Read
mStream.Position = pos
Else
PeekChar = -1
End If
End Function
''
' Reads the next byte in the stream, and advances the position one byte.
'
' @return The next byte in the stream.
Public Function ReadByte() As Byte
Call FillBuffer(1)
ReadByte = mBuffer(0)
End Function
''
' Reads the next decode character in the stream, and advances the position
' the number of bytes requirece to assemble a single character.
'
' @return The next character in the stream.
' @remarks If no characters are left in the stream, and EndOfStreamException is thrown.
'
Public Function ReadChar() As Long
ReadChar = Read
If ReadChar = -1 Then Throw New EndOfStreamException
End Function
''
' Reads the specified number of bytes from the stream and returns them in an array.
'
' @param Count The number of bytes to read from the stream.
' @return A byte array containing the bytes read from the stream.
' @remarks If not enough bytes are in the stream, then the remaining
' bytes are returned. If there are no bytes in the stream, then an
' empty array is returned, not a null array.
'
Public Function ReadBytes(ByVal Count As Long) As Byte()
Call VerifyIsOpen
If Count < 0 Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_NeedNonNegNum), "Count", Count)
Dim ret() As Byte
If Count > 0 Then
ReDim ret(0 To Count - 1)
Dim num As Long
num = mStream.ReadBlock(ret, 0, Count)
If num = 0 Then Exit Function
If num <> Count Then ReDim Preserve ret(0 To num - 1)
Else
ret = Cor.NewBytes()
End If
ReadBytes = ret
End Function
''
' Returns a specified number of characters decoded from the stream.
'
' @param Count The number of characters to decode from the stream.
' @return The array of characters decoded from the stream.
' @remarks If not enough characters could be decoded from the stream,
' then only the remaining characters are returned. If no characters
' could be decoded, then an empty array is returned, not a null array.
'
Public Function ReadChars(ByVal Count As Long) As Integer()
Call VerifyIsOpen
If Count < 0 Then _
Throw Cor.NewArgumentOutOfRangeException(Environment.GetResourceString(ArgumentOutOfRange_NeedNonNegNum), "Count", Count)
Dim ret() As Integer
If Count > 0 Then
ReDim ret(0 To Count - 1)
Dim CharsRead As Long
CharsRead = ReadCharBytes(ret, 0, Count)
If CharsRead = 0 Then Throw New EndOfStreamException
If Count <> CharsRead Then ReDim Preserve ret(0 To CharsRead - 1)
Else
ret = Cor.NewIntegers()
End If
ReadChars = ret
End Function
''
' Returns a boolean from the stream.
'
' @return A boolean value.
' @remarks A single byte is read from the stream. If the byte
' is non-zero, then True is returned, otherwise False.
'
Public Function ReadBoolean() As Boolean
Call VerifyIsOpen
ReadBoolean = (ReadByte <> 0)
End Function
''
' Reads 4 bytes from the stream and returns them as a Long datatype.
'
' @return A Long datatype read from the stream.
'
Public Function ReadLong() As Long
Call FillBuffer(4)
ReadLong = MemLong(mPtrBuffer)
End Function
''
' Reads 2 bytes from the stream and returns them as an Integer datatype.
'
' @return An Integer datatype read from the stream.
'
Public Function ReadInteger() As Integer
Call FillBuffer(2)
ReadInteger = MemWord(mPtrBuffer)
End Function
''
' Reads 8 bytes from the stream and returns them as a Double datatype.
'
' @return A Double datatype read from the stream.
'
Public Function ReadDouble() As Double
Call FillBuffer(8)
ReadDouble = AsDouble(ByVal mPtrBuffer)
End Function
''
' Reads 8 bytes from the stream and returns them as a Date datatype.
'
' @return A Date datatype read from the stream.
' @remarks It is possible to read a combination of 8 bytes that
' convert into an invalid Date. This will cause and Overflow error.
'
Public Function ReadDate() As Date
Dim PotentialDate As Double
PotentialDate = ReadDouble
If PotentialDate < MIN_DATE Or PotentialDate > MAX_DATE Then Throw New OverflowException
ReadDate = PotentialDate
End Function
''
' Reads 8 bytes from the stream and returns them as a Currency datatype.
'
' @return A Currency datatype.
'
Public Function ReadCurrency() As Currency
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -