~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Sharpen/Sharpen/ByteBuffer.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace Sharpen
 
2
{
 
3
        using System;
 
4
 
 
5
        public class ByteBuffer
 
6
        {
 
7
                private byte[] buffer;
 
8
                private DataConverter c;
 
9
                private int capacity;
 
10
                private int index;
 
11
                private int limit;
 
12
                private int mark;
 
13
                private int offset;
 
14
                private ByteOrder order;
 
15
 
 
16
                public ByteBuffer ()
 
17
                {
 
18
                        this.c = DataConverter.BigEndian;
 
19
                }
 
20
 
 
21
                private ByteBuffer (byte[] buf, int start, int len)
 
22
                {
 
23
                        this.buffer = buf;
 
24
                        this.offset = 0;
 
25
                        this.limit = start + len;
 
26
                        this.index = start;
 
27
                        this.mark = start;
 
28
                        this.capacity = buf.Length;
 
29
                        this.c = DataConverter.BigEndian;
 
30
                }
 
31
 
 
32
                public static ByteBuffer Allocate (int size)
 
33
                {
 
34
                        return new ByteBuffer (new byte[size], 0, size);
 
35
                }
 
36
 
 
37
                public static ByteBuffer AllocateDirect (int size)
 
38
                {
 
39
                        return Allocate (size);
 
40
                }
 
41
 
 
42
                public byte[] Array ()
 
43
                {
 
44
                        return buffer;
 
45
                }
 
46
 
 
47
                public int ArrayOffset ()
 
48
                {
 
49
                        return offset;
 
50
                }
 
51
 
 
52
                public int Capacity ()
 
53
                {
 
54
                        return capacity;
 
55
                }
 
56
 
 
57
                private void CheckGetLimit (int inc)
 
58
                {
 
59
                        if ((index + inc) > limit) {
 
60
                                throw new BufferUnderflowException ();
 
61
                        }
 
62
                }
 
63
 
 
64
                private void CheckPutLimit (int inc)
 
65
                {
 
66
                        if ((index + inc) > limit) {
 
67
                                throw new BufferUnderflowException ();
 
68
                        }
 
69
                }
 
70
 
 
71
                public void Clear ()
 
72
                {
 
73
                        index = offset;
 
74
                        limit = offset + capacity;
 
75
                }
 
76
 
 
77
                public void Flip ()
 
78
                {
 
79
                        limit = index;
 
80
                        index = offset;
 
81
                }
 
82
 
 
83
                public byte Get ()
 
84
                {
 
85
                        CheckGetLimit (1);
 
86
                        return buffer[index++];
 
87
                }
 
88
 
 
89
                public void Get (byte[] data)
 
90
                {
 
91
                        Get (data, 0, data.Length);
 
92
                }
 
93
 
 
94
                public void Get (byte[] data, int start, int len)
 
95
                {
 
96
                        CheckGetLimit (len);
 
97
                        for (int i = 0; i < len; i++) {
 
98
                                data[i + start] = buffer[index++];
 
99
                        }
 
100
                }
 
101
 
 
102
                public int GetInt ()
 
103
                {
 
104
                        CheckGetLimit (4);
 
105
                        int num = c.GetInt32 (buffer, index);
 
106
                        index += 4;
 
107
                        return num;
 
108
                }
 
109
 
 
110
                public short GetShort ()
 
111
                {
 
112
                        CheckGetLimit (2);
 
113
                        short num = c.GetInt16 (buffer, index);
 
114
                        index += 2;
 
115
                        return num;
 
116
                }
 
117
 
 
118
                public bool HasArray ()
 
119
                {
 
120
                        return true;
 
121
                }
 
122
 
 
123
                public int Limit ()
 
124
                {
 
125
                        return (limit - offset);
 
126
                }
 
127
 
 
128
                public void Limit (int newLimit)
 
129
                {
 
130
                        limit = newLimit;
 
131
                }
 
132
 
 
133
                public void Mark ()
 
134
                {
 
135
                        mark = index;
 
136
                }
 
137
 
 
138
                public void Order (ByteOrder order)
 
139
                {
 
140
                        this.order = order;
 
141
                        if (order == ByteOrder.BIG_ENDIAN) {
 
142
                                c = DataConverter.BigEndian;
 
143
                        } else {
 
144
                                c = DataConverter.LittleEndian;
 
145
                        }
 
146
                }
 
147
 
 
148
                public int Position ()
 
149
                {
 
150
                        return (index - offset);
 
151
                }
 
152
 
 
153
                public void Position (int pos)
 
154
                {
 
155
                        if ((pos < offset) || (pos > limit)) {
 
156
                                throw new BufferUnderflowException ();
 
157
                        }
 
158
                        index = pos + offset;
 
159
                }
 
160
 
 
161
                public void Put (byte[] data)
 
162
                {
 
163
                        Put (data, 0, data.Length);
 
164
                }
 
165
 
 
166
                public void Put (byte data)
 
167
                {
 
168
                        CheckPutLimit (1);
 
169
                        buffer[index++] = data;
 
170
                }
 
171
 
 
172
                public void Put (byte[] data, int start, int len)
 
173
                {
 
174
                        CheckPutLimit (len);
 
175
                        for (int i = 0; i < len; i++) {
 
176
                                buffer[index++] = data[i + start];
 
177
                        }
 
178
                }
 
179
 
 
180
                public void PutInt (int i)
 
181
                {
 
182
                        Put (c.GetBytes (i));
 
183
                }
 
184
 
 
185
                public void PutShort (short i)
 
186
                {
 
187
                        Put (c.GetBytes (i));
 
188
                }
 
189
 
 
190
                public int Remaining ()
 
191
                {
 
192
                        return (limit - index);
 
193
                }
 
194
 
 
195
                public void Reset ()
 
196
                {
 
197
                        index = mark;
 
198
                }
 
199
 
 
200
                public ByteBuffer Slice ()
 
201
                {
 
202
                        ByteBuffer b = Wrap (buffer, index, buffer.Length - index);
 
203
                        b.offset = index;
 
204
                        b.limit = limit;
 
205
                        b.order = order;
 
206
                        b.c = c;
 
207
                        b.capacity = limit - index;
 
208
                        return b;
 
209
                }
 
210
 
 
211
                public static ByteBuffer Wrap (byte[] buf)
 
212
                {
 
213
                        return new ByteBuffer (buf, 0, buf.Length);
 
214
                }
 
215
 
 
216
                public static ByteBuffer Wrap (byte[] buf, int start, int len)
 
217
                {
 
218
                        return new ByteBuffer (buf, start, len);
 
219
                }
 
220
        }
 
221
}