-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryDataBlock.cs
87 lines (75 loc) · 2.28 KB
/
MemoryDataBlock.cs
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
using System;
namespace Be.Windows.Forms
{
internal sealed class MemoryDataBlock : DataBlock
{
byte[] _data;
public MemoryDataBlock(byte data)
{
_data = new byte[] { data };
}
public MemoryDataBlock(byte[] data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
_data = (byte[])data.Clone();
}
public override long Length
{
get
{
return _data.LongLength;
}
}
public byte[] Data
{
get
{
return _data;
}
}
public void AddByteToEnd(byte value)
{
byte[] newData = new byte[_data.LongLength + 1];
_data.CopyTo(newData, 0);
newData[newData.LongLength - 1] = value;
_data = newData;
}
public void AddByteToStart(byte value)
{
byte[] newData = new byte[_data.LongLength + 1];
newData[0] = value;
_data.CopyTo(newData, 1);
_data = newData;
}
public void InsertBytes(long position, byte[] data)
{
byte[] newData = new byte[_data.LongLength + data.LongLength];
if (position > 0)
{
Array.Copy(_data, 0, newData, 0, position);
}
Array.Copy(data, 0, newData, position, data.LongLength);
if (position < _data.LongLength)
{
Array.Copy(_data, position, newData, position + data.LongLength, _data.LongLength - position);
}
_data = newData;
}
public override void RemoveBytes(long position, long count)
{
byte[] newData = new byte[_data.LongLength - count];
if (position > 0)
{
Array.Copy(_data, 0, newData, 0, position);
}
if (position + count < _data.LongLength)
{
Array.Copy(_data, position + count, newData, position, newData.LongLength - position);
}
_data = newData;
}
}
}