Repository navigation
Expand file tree
/
Copy pathUltimateByteArrayClass.cs
More file actions
325 lines (277 loc) · 9.94 KB
/
Copy pathUltimateByteArrayClass.cs
File metadata and controls
325 lines (277 loc) · 9.94 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using System;
using System.Collections.Generic;
namespace RTSPLibrary
{
public class UltimateByteArrayClass : IEnumerable<byte>
{
private int iBlock_Size = 32000; // 每次增加 32k
private int iBufferMax = -1; // 最大可配置空間
private int iCurrentSize = 0; // 目前陣列大小
protected internal byte[] iInternalArray = (byte[])Array.CreateInstance(typeof(byte), 0);
public byte[] InternalBuffer
{
get
{
return iInternalArray;
}
}
public int Count
{
get
{
return iCurrentSize;
}
set
{
// If value < 0 Then
// Throw New Exception("value < 0:" & value)
// End If
iCurrentSize = Math.Min(Math.Max(value, 0), iBufferMax);
}
}
/// <summary>
/// ''' Don't use, very slow, please change code to access InternalBuffer
/// ''' </summary>
/// ''' <param name="Index"></param>
/// ''' <value></value>
/// ''' <returns></returns>
/// ''' <remarks></remarks>
public byte this[int Index]
{
get
{
if ((Index >= 0) & (Index < iCurrentSize))
return iInternalArray[Index];
else
throw new IndexOutOfRangeException();
}
set
{
if ((Index >= 0) & (Index < iCurrentSize))
iInternalArray[Index] = value;
else
throw new IndexOutOfRangeException();
}
}
public void AddRange(UltimateByteArrayClass B)
{
this.AddRange(B.iInternalArray, 0, B.Count);
}
public void AddRange(UltimateByteArrayClass B, int offsetIndex, int Length)
{
this.AddRange(B.iInternalArray, offsetIndex, Length);
}
public void AddRange(byte[] B)
{
this.AddRange(B, 0, B.Length);
}
public void AddRange(byte[] B, int index, int length)
{
if (length > 0)
{
lock (iInternalArray)
{
CheckInternalBuffer(iCurrentSize + length);
if (iBufferMax != -1)
{
if (length >= iBufferMax)
{
// 來源資料長度已超過 Buffer 容量
// 直接擷取最後一段
Array.Copy(B, (length - iBufferMax) + index, iInternalArray, 0, iBufferMax);
iCurrentSize = iBufferMax;
}
else
{
// 來源資料長度尚未超過容量
int FreeSize;
FreeSize = (iBufferMax - length);
if (iCurrentSize <= FreeSize)
{
try
{
Array.Copy(B, index, iInternalArray, iCurrentSize, length);
}
catch (Exception ex)
{
throw new Exception("index:" + index + ",iCurrentSize:" + iCurrentSize + ",length:" + length + ",internalArray:" + iInternalArray.Length + ",B:" + B.Length + "\r\n" + ex.ToString());
}
iCurrentSize += length;
}
else
{
// 空間不足
// tmpBytes = Array.CreateInstance(GetType(Byte), iArrayLength)
// iInt_Buffer.CopyTo(iInt_Buffer.Count - FreeSize, tmpBytes, 0, FreeSize)
// Array.Copy(B, Index, tmpBytes, FreeSize, Length)
Array.Copy(iInternalArray, iCurrentSize - FreeSize, iInternalArray, 0, FreeSize);
Array.Copy(B, index, iInternalArray, FreeSize, length);
}
}
}
else
{
Array.Copy(B, index, iInternalArray, iCurrentSize, length);
iCurrentSize += length;
}
}
}
}
public void RemoveRange(int index, int length)
{
lock (iInternalArray)
{
if (iCurrentSize > (length + index))
Array.Copy(iInternalArray, index + length, iInternalArray, index, iCurrentSize - (index + length));
if (length <= iCurrentSize)
iCurrentSize -= length;
else
// Throw New Exception("length > iCurrentsize:" & length & "," & iCurrentSize)
iCurrentSize = 0;
}
}
public void CopyTo(int ArrayIndex, byte[] Destination, int DestinationIndex, int CopyLength)
{
if (CopyLength > 0)
Array.Copy(iInternalArray, ArrayIndex, Destination, DestinationIndex, CopyLength);
}
public void Clear()
{
iCurrentSize = 0;
}
public byte[] ToArray()
{
byte[] RetValue = null;
RetValue = (byte[])Array.CreateInstance(typeof(byte), iCurrentSize);
Array.Copy(iInternalArray, 0, RetValue, 0, RetValue.Length);
return RetValue;
}
private byte[] CopyArraySegment(byte[] Src, int Index, int Length)
{
if ((Index == 0) & (Length == Src.Length))
return Src;
else
{
byte[] iPartOfArray = null;
iPartOfArray = (byte[])Array.CreateInstance(typeof(byte), Length);
Array.Copy(Src, Index, iPartOfArray, 0, iPartOfArray.Length);
return iPartOfArray;
}
}
private void CheckInternalBuffer(int totalSizeRequire)
{
if ((iInternalArray.Length < iBufferMax) | (iBufferMax == -1))
{
bool needAlloc = false;
int newSize;
int oldSize;
oldSize = iInternalArray.Length;
if (totalSizeRequire > iInternalArray.Length)
needAlloc = true;
if (needAlloc)
{
if ((totalSizeRequire % iBlock_Size) != 0)
newSize = iBlock_Size * ((totalSizeRequire / iBlock_Size) + 1);
else
newSize = iBlock_Size * (totalSizeRequire / iBlock_Size);
if ((newSize > oldSize))
{
byte[] newBuffer = null;
newBuffer = (byte[])Array.CreateInstance(typeof(byte), newSize);
if (iCurrentSize > 0)
Array.Copy(iInternalArray, 0, newBuffer, 0, iCurrentSize);
iInternalArray = newBuffer;
}
}
}
}
public UltimateByteArrayClass(int MaxBufferSize)
{
iBufferMax = MaxBufferSize;
}
public UltimateByteArrayClass(int MaxBufferSize, byte[] Source)
{
iBufferMax = MaxBufferSize;
this.AddRange(Source);
}
public UltimateByteArrayClass(byte[] Source)
{
iBufferMax = -1;
this.AddRange(Source);
}
public UltimateByteArrayClass()
{
iBufferMax = -1;
}
public IEnumerator<byte> GetEnumeratorOfByte()
{
return (IEnumerator<byte>)this.GetEnumerator();
}
IEnumerator<byte> IEnumerable<byte>.GetEnumerator()
{
return (IEnumerator<byte>)this.GetEnumerator();
}
public System.Collections.IEnumerator GetEnumerator()
{
return new ArrayEnumerator(this);
}
public class ArrayEnumerator : IEnumerator<byte>
{
private UltimateByteArrayClass iCreator;
private int iPosition = -1;
protected internal ArrayEnumerator(UltimateByteArrayClass Creator)
{
iCreator = Creator;
}
public object Current
{
get
{
if ((iPosition >= 0) & (iPosition <= iCreator.Count))
return iCreator[iPosition];
else
throw new IndexOutOfRangeException();
}
}
public bool MoveNext()
{
iPosition += 1;
return iPosition < iCreator.Count;
}
public void Reset()
{
iPosition = -1;
}
public byte CurrentOfByte
{
get
{
return (byte)this.Current;
}
}
byte IEnumerator<byte>.Current
{
get
{
return (byte)this.Current;
}
}
private bool disposedValue;
// IDisposable
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
iCreator = null;
}
this.disposedValue = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
}