-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicByteProvider.cs
177 lines (154 loc) · 3.9 KB
/
DynamicByteProvider.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
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
using System;
using System.Collections.Generic;
namespace Be.Windows.Forms
{
/// <summary>
/// Byte provider for a small amount of data.
/// </summary>
public class DynamicByteProvider : IByteProvider
{
/// <summary>
/// Contains information about changes.
/// </summary>
bool _hasChanges;
/// <summary>
/// Contains a byte collection.
/// </summary>
List<byte> _bytes;
/// <summary>
/// Initializes a new instance of the DynamicByteProvider class.
/// </summary>
/// <param name="data"></param>
public DynamicByteProvider(byte[] data) : this(new List<Byte>(data))
{
}
/// <summary>
/// Initializes a new instance of the DynamicByteProvider class.
/// </summary>
/// <param name="bytes"></param>
public DynamicByteProvider(List<Byte> bytes)
{
_bytes = bytes;
}
/// <summary>
/// Raises the Changed event.
/// </summary>
void OnChanged(EventArgs e)
{
_hasChanges = true;
if(Changed != null)
Changed(this, e);
}
/// <summary>
/// Raises the LengthChanged event.
/// </summary>
void OnLengthChanged(EventArgs e)
{
if(LengthChanged != null)
LengthChanged(this, e);
}
/// <summary>
/// Gets the byte collection.
/// </summary>
public List<Byte> Bytes
{
get { return _bytes; }
}
#region IByteProvider Members
/// <summary>
/// True, when changes are done.
/// </summary>
public bool HasChanges()
{
return _hasChanges;
}
/// <summary>
/// Applies changes.
/// </summary>
public void ApplyChanges()
{
_hasChanges = false;
}
/// <summary>
/// Occurs, when the write buffer contains new changes.
/// </summary>
public event EventHandler Changed;
/// <summary>
/// Occurs, when InsertBytes or DeleteBytes method is called.
/// </summary>
public event EventHandler LengthChanged;
/// <summary>
/// Reads a byte from the byte collection.
/// </summary>
/// <param name="index">the index of the byte to read</param>
/// <returns>the byte</returns>
public byte ReadByte(long index)
{ return _bytes[(int)index]; }
/// <summary>
/// Write a byte into the byte collection.
/// </summary>
/// <param name="index">the index of the byte to write.</param>
/// <param name="value">the byte</param>
public void WriteByte(long index, byte value)
{
_bytes[(int)index] = value;
OnChanged(EventArgs.Empty);
}
/// <summary>
/// Deletes bytes from the byte collection.
/// </summary>
/// <param name="index">the start index of the bytes to delete.</param>
/// <param name="length">the length of bytes to delete.</param>
public void DeleteBytes(long index, long length)
{
int internal_index = (int)Math.Max(0, index);
int internal_length = (int)Math.Min((int)Length, length);
_bytes.RemoveRange(internal_index, internal_length);
OnLengthChanged(EventArgs.Empty);
OnChanged(EventArgs.Empty);
}
/// <summary>
/// Inserts byte into the byte collection.
/// </summary>
/// <param name="index">the start index of the bytes in the byte collection</param>
/// <param name="bs">the byte array to insert</param>
public void InsertBytes(long index, byte[] bs)
{
_bytes.InsertRange((int)index, bs);
OnLengthChanged(EventArgs.Empty);
OnChanged(EventArgs.Empty);
}
/// <summary>
/// Gets the length of the bytes in the byte collection.
/// </summary>
public long Length
{
get
{
return _bytes.Count;
}
}
/// <summary>
/// Returns true
/// </summary>
public bool SupportsWriteByte()
{
return true;
}
/// <summary>
/// Returns true
/// </summary>
public bool SupportsInsertBytes()
{
return true;
}
/// <summary>
/// Returns true
/// </summary>
public bool SupportsDeleteBytes()
{
return true;
}
#endregion
}
}