|
#Region "BytesFormatText"
''' <summary>
''' BytesFormatText
''' </summary>
''' <param name="Bytes"></param>
''' <returns>String</returns>
<Runtime.CompilerServices.Extension>
Private Function BytesFormatText(Bytes() As Byte) As String
Dim len As Integer = Bytes.Length
If len >= 3 AndAlso Bytes(0) = &HEF AndAlso Bytes(1) = &HBB AndAlso Bytes(2) = &HBF Then Return Encoding.UTF8.GetString(Bytes, 3, len - 3)
Dim cs() As Integer = {7, 5, 4, 3, 2, 1, 0, 6, 14, 30, 62, 126}
For i As Integer = 0 To len - 1
Dim bits As Integer = -1
For j As Integer = 0 To 5
If CInt(Bytes(i)) >> cs(j) = cs(j + 6) Then
bits = j
Exit For
End If
Next
If bits = -1 Then Return Encoding.Default.GetString(Bytes)
Do While bits > 0
bits -= 1
i += 1
If i = len OrElse CInt(Bytes(i)) >> 6 <> 2 Then Return Encoding.Default.GetString(Bytes)
Loop
Next
Return Encoding.UTF8.GetString(Bytes)
End Function
#End Region
#Region "读取文件流"
''' <summary>
''' 读取文件流
''' </summary>
''' <param name="path">文件地址</param>
''' <returns>返回Byte文件流</returns>
<Runtime.CompilerServices.Extension>
Public Function ReadFile(path As String) As String
Using FileStream As New FileStream(path, FileMode.Open, FileAccess.Read)
Try
With FileStream
Dim Buffur(.Length - 1) As Byte
.Read(Buffur, 0, .Length)
.Seek(0, SeekOrigin.Begin)
Return Buffur.BytesFormatText
End With
Catch ex As IOException
Throw New IOException(ex.Message)
Return Nothing
End Try
End Using
End Function
#End Region |
|