~ubuntu-branches/ubuntu/wily/openmcdf/wily

« back to all changes in this revision

Viewing changes to src/TESTOpenMCDF/StreamDataProvider.cs

  • Committer: Package Import Robot
  • Author(s): Mathieu Malaterre
  • Date: 2013-04-08 11:02:15 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130408110215-aleqo4zhjc3qgxnb
Tags: 1.5.4-1
* New upstream: 1.5.4
  - Use Hexbox for hexadecimal viewing

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Text;
 
4
using Be.Windows.Forms;
 
5
using OpenMcdf;
 
6
 
 
7
namespace StructuredStorageExplorer
 
8
{
 
9
    public class StreamDataProvider : IByteProvider
 
10
    {
 
11
        /// <summary>
 
12
        /// Modifying stream
 
13
        /// </summary>
 
14
        CFStream _modifiedStream;
 
15
 
 
16
        /// <summary>
 
17
        /// Contains information about changes.
 
18
        /// </summary>
 
19
        bool _hasChanges;
 
20
 
 
21
        /// <summary>
 
22
        /// Contains a byte collection.
 
23
        /// </summary>
 
24
        ByteCollection _bytes;
 
25
 
 
26
 
 
27
        /// <summary>
 
28
        /// Initializes a new instance of the DynamicByteProvider class.
 
29
        /// </summary>
 
30
        /// <param name="bytes"></param>
 
31
        public StreamDataProvider(CFStream modifiedStream)
 
32
        {
 
33
            _bytes = new ByteCollection(modifiedStream.GetData());
 
34
            _modifiedStream = modifiedStream;
 
35
        }
 
36
 
 
37
        /// <summary>
 
38
        /// Raises the Changed event.
 
39
        /// </summary>
 
40
        void OnChanged(EventArgs e)
 
41
        {
 
42
            _hasChanges = true;
 
43
 
 
44
            if (Changed != null)
 
45
                Changed(this, e);
 
46
        }
 
47
 
 
48
        /// <summary>
 
49
        /// Raises the LengthChanged event.
 
50
        /// </summary>
 
51
        void OnLengthChanged(EventArgs e)
 
52
        {
 
53
            if (LengthChanged != null)
 
54
                LengthChanged(this, e);
 
55
        }
 
56
 
 
57
        /// <summary>
 
58
        /// Gets the byte collection.
 
59
        /// </summary>
 
60
        public ByteCollection Bytes
 
61
        {
 
62
            get { return _bytes; }
 
63
        }
 
64
 
 
65
        #region IByteProvider Members
 
66
        /// <summary>
 
67
        /// True, when changes are done.
 
68
        /// </summary>
 
69
        public bool HasChanges()
 
70
        {
 
71
            return _hasChanges;
 
72
        }
 
73
 
 
74
        /// <summary>
 
75
        /// Applies changes.
 
76
        /// </summary>
 
77
        public void ApplyChanges()
 
78
        {
 
79
            _hasChanges = false;
 
80
 
 
81
            _modifiedStream.SetData(this._bytes.ToArray());
 
82
        }
 
83
 
 
84
        /// <summary>
 
85
        /// Occurs, when the write buffer contains new changes.
 
86
        /// </summary>
 
87
        public event EventHandler Changed;
 
88
 
 
89
        /// <summary>
 
90
        /// Occurs, when InsertBytes or DeleteBytes method is called.
 
91
        /// </summary>
 
92
        public event EventHandler LengthChanged;
 
93
 
 
94
 
 
95
        /// <summary>
 
96
        /// Reads a byte from the byte collection.
 
97
        /// </summary>
 
98
        /// <param name="index">the index of the byte to read</param>
 
99
        /// <returns>the byte</returns>
 
100
        public byte ReadByte(long index)
 
101
        { return _bytes[(int)index]; }
 
102
 
 
103
        /// <summary>
 
104
        /// Write a byte into the byte collection.
 
105
        /// </summary>
 
106
        /// <param name="index">the index of the byte to write.</param>
 
107
        /// <param name="value">the byte</param>
 
108
        public void WriteByte(long index, byte value)
 
109
        {
 
110
            _bytes[(int)index] = value;
 
111
            OnChanged(EventArgs.Empty);
 
112
        }
 
113
 
 
114
        /// <summary>
 
115
        /// Deletes bytes from the byte collection.
 
116
        /// </summary>
 
117
        /// <param name="index">the start index of the bytes to delete.</param>
 
118
        /// <param name="length">the length of bytes to delete.</param>
 
119
        public void DeleteBytes(long index, long length)
 
120
        {
 
121
            int internal_index = (int)Math.Max(0, index);
 
122
            int internal_length = (int)Math.Min((int)Length, length);
 
123
            _bytes.RemoveRange(internal_index, internal_length);
 
124
 
 
125
            OnLengthChanged(EventArgs.Empty);
 
126
            OnChanged(EventArgs.Empty);
 
127
        }
 
128
 
 
129
        /// <summary>
 
130
        /// Inserts byte into the byte collection.
 
131
        /// </summary>
 
132
        /// <param name="index">the start index of the bytes in the byte collection</param>
 
133
        /// <param name="bs">the byte array to insert</param>
 
134
        public void InsertBytes(long index, byte[] bs)
 
135
        {
 
136
            _bytes.InsertRange((int)index, bs);
 
137
 
 
138
            OnLengthChanged(EventArgs.Empty);
 
139
            OnChanged(EventArgs.Empty);
 
140
        }
 
141
 
 
142
        /// <summary>
 
143
        /// Gets the length of the bytes in the byte collection.
 
144
        /// </summary>
 
145
        public long Length
 
146
        {
 
147
            get
 
148
            {
 
149
                return _bytes.Count;
 
150
            }
 
151
        }
 
152
 
 
153
        /// <summary>
 
154
        /// Returns true
 
155
        /// </summary>
 
156
        public bool SupportsWriteByte()
 
157
        {
 
158
            return true;
 
159
        }
 
160
 
 
161
        /// <summary>
 
162
        /// Returns true
 
163
        /// </summary>
 
164
        public bool SupportsInsertBytes()
 
165
        {
 
166
            return true;
 
167
        }
 
168
 
 
169
        /// <summary>
 
170
        /// Returns true
 
171
        /// </summary>
 
172
        public bool SupportsDeleteBytes()
 
173
        {
 
174
            return true;
 
175
        }
 
176
        #endregion
 
177
    }
 
178
}