-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCByteStream.cls
More file actions
276 lines (206 loc) · 6.43 KB
/
CByteStream.cls
File metadata and controls
276 lines (206 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "CByteStream"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'模块:字节数据流类
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private mData() As Byte
Private mCount As Long
Private mSpaceSize As Long
Public Tag As Variant
Private Sub Class_Initialize()
'
End Sub
Private Sub Class_Terminate()
Call Clear
End Sub
'取得流的字节数
Public Property Get Count() As Long
Count = mCount
End Property
'取得/设置数据
Public Property Get Data() As Byte()
Data = mData
End Property
Public Property Let Data(ByRef RHS() As Byte)
Dim L As Long, U As Long
On Error Resume Next
L = LBound(RHS)
If ERR.Number Then Exit Property
On Error GoTo 0
U = UBound(RHS)
mCount = (U - L + 1)
mSpaceSize = mCount
If L = 0 Then
mData = RHS
Else
ReDim mData(0 To mSpaceSize - 1)
CopyMemory mData(0), RHS(L), mCount * 1
End If
End Property
'取得内部数据的指针
Public Property Get DataPtr() As Long
If mCount <= 0 Then
DataPtr = 0
Else
DataPtr = VarPtr(mData(0))
End If
End Property
'清空缓冲区
Public Function Clear() As Boolean
If mCount <= 0 Then Exit Function
mCount = 0
Erase mData
mSpaceSize = 0
Clear = True
End Function
'取得数据
Public Function GetData(ByRef BytArr() As Byte, Optional ByVal cbData As Long = -1) As Long
If (cbData > mCount) Or (cbData = -1) Then
cbData = mCount
End If
If cbData > 0 Then
ReDim BytArr(0 To cbData - 1)
CopyMemory BytArr(0), mData(0), cbData
If cbData < mCount Then
CopyMemory mData(0), mData(cbData), mCount - cbData
End If
mCount = mCount - cbData
End If
GetData = cbData
End Function
'取得数据(使用地址)
Public Function GetData4Ptr(ByVal BufPtr As Long, Optional ByVal cbData As Long = -1) As Long
If (BufPtr And &HFFF&) = 0 Then '低4K是检查无效指针的区域
Exit Function
End If
If (cbData > mCount) Or (cbData = -1) Then
cbData = mCount
End If
If cbData > 0 Then
CopyMemory ByVal BufPtr, mData(0), cbData
If cbData < mCount Then
CopyMemory mData(0), mData(cbData), mCount - cbData
End If
mCount = mCount - cbData
End If
GetData4Ptr = cbData
End Function
'查看数据
Public Function PeekData(ByRef BytArr() As Byte, Optional ByVal START As Long = 0, Optional ByVal cbData As Long = -1) As Long
If START < 0 Then
cbData = cbData + START
START = 0
End If
If (START + cbData > mCount) Or (cbData = -1) Then
cbData = mCount - START
End If
If cbData > 0 Then
ReDim BytArr(0 To cbData - 1)
CopyMemory BytArr(0), mData(START), cbData
End If
PeekData = cbData
End Function
'查看数据(使用地址)
Public Function PeekData4Ptr(ByVal BufPtr As Long, Optional ByVal START As Long = 0, Optional ByVal cbData As Long = -1) As Long
If (BufPtr And &HFFF&) = 0 Then '低4K是检查无效指针的区域
Exit Function
End If
If START < 0 Then
cbData = cbData + START
START = 0
End If
If (START + cbData > mCount) Or (cbData = -1) Then
cbData = mCount - START
End If
If cbData > 0 Then
CopyMemory ByVal BufPtr, mData(START), cbData
End If
PeekData4Ptr = cbData
End Function
'添加数据
Public Function AddData(ByRef BytArr() As Byte) As Long
Dim L As Long, U As Long
Dim cbData As Long
On Error Resume Next
L = LBound(BytArr)
If ERR.Number Then Exit Function
On Error GoTo 0
U = UBound(BytArr)
cbData = U - L + 1
If cbData <= 0 Then
Exit Function
End If
If cbData > 0 Then
mCount = mCount + cbData
If mSpaceSize < mCount Then '分配空间
mSpaceSize = mCount
ReDim Preserve mData(0 To mSpaceSize - 1)
End If
CopyMemory mData(mCount - cbData), BytArr(0), cbData
End If
AddData = cbData
End Function
'添加数据(使用地址)
Public Function AddData4Ptr(ByVal BufPtr As Long, ByVal cbData As Long) As Long
If (BufPtr And &HFFF&) = 0 Then '低4K是检查无效指针的区域
Exit Function
End If
If cbData > 0 Then
mCount = mCount + cbData
If mSpaceSize < mCount Then '分配空间
mSpaceSize = mCount
ReDim Preserve mData(0 To mSpaceSize - 1)
End If
CopyMemory mData(mCount - cbData), ByVal BufPtr, cbData
End If
AddData4Ptr = cbData
End Function
'删除数据
Public Function DeleteData(Optional ByVal START As Long = 0, Optional ByVal cbData As Long = -1) As Long
If START < 0 Then
cbData = cbData + START
START = 0
End If
If (START + cbData > mCount) Or (cbData = -1) Then
cbData = mCount - START
End If
If cbData > 0 Then
If cbData < mCount Then
CopyMemory mData(START), mData(START + cbData), mCount - cbData
End If
mCount = mCount - cbData
End If
DeleteData = cbData
End Function
'克隆自身
Public Function Clone() As CByteStream
Dim Item As CByteStream
Set Item = New CByteStream
Call Item.Clear
Call Item.AddData4Ptr(Me.DataPtr, Me.Count)
Set Clone = Item
End Function
'从别处克隆
Public Function CloneFrom(ByVal Source As CByteStream) As Long
If (Source.DataPtr And &HFFF&) = 0 Then '低4K是检查无效指针的区域
Exit Function
End If
mCount = Source.Count
If mSpaceSize < mCount Then '分配空间
mSpaceSize = mCount
ReDim Preserve mData(0 To mSpaceSize - 1)
End If
CopyMemory mData(0), ByVal Source.DataPtr, mCount
CloneFrom = mCount
End Function