jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
using System;
using System.Collections.Generic;
using System.Text;
 
namespace HH.WMS.Utils.NPOI.Util
{
    public class ByteBuffer
    {
        private byte[] buffer;
        private int mark = -1; //useless.
        private int position = 0;
        private int limit;
        private int capacity;
        private int offset;  //not sure about this.
 
        private ByteBuffer(int mark, int pos, int lim, int cap, byte[] buffer, int offset)
        {
            if (cap < 0)
                throw new ArgumentException();
            this.capacity = cap;
 
            this.Limit = lim;
            this.Position = pos;
            if (mark >= 0)
            {
                if (mark > pos)
                    throw new ArgumentException();
                this.mark = mark;
            }
 
            this.buffer = buffer;
            this.offset = offset;
        }
 
        public ByteBuffer(byte[] buffer, int off, int len)
            :this(-1, off, off+len, buffer.Length, buffer, 0)
        {
        }
 
        public ByteBuffer(int capacity, int limit)
            :this(-1, 0, limit, capacity, new byte[capacity], 0)
        {
        }
 
        protected ByteBuffer(byte[] buffer, int mark, int pos, int lim, int cap, int off)
            : this(mark, pos, lim, cap, buffer, off)
        {
        }
 
        public int Position
        {
            get { return position; }
            set
            {
                if (value < 0 || value > limit)
                    throw new ArgumentException();
                position = value;
                if (mark > position)
                    mark = -1;
            }
        }
 
        public int Limit
        {
            get { return limit; }
            set
            {
                if ((value > capacity) || (value < 0))
                    throw new ArgumentException();
                limit = value;
 
                if (position > limit)
                    position = limit;
                if (mark > limit)
                    mark = -1;
            }
        }
 
 
        //allocate
        public static ByteBuffer CreateBuffer(int capacity)
        {
            if (capacity < 0)
                throw new ArgumentException();
            return new ByteBuffer(capacity, capacity);
        }
 
        //wrap
        public static ByteBuffer CreateBuffer(byte[] buffer, int offset, int length)
        {
            try
            {
                return new ByteBuffer(buffer, offset, length);
            }
            catch (ArgumentException)
            {
                throw new IndexOutOfRangeException();
            }
 
        }
 
        public static ByteBuffer CreateBuffer(byte[] buffer)
        {
            return CreateBuffer(buffer, 0, buffer.Length);
        }
 
        public ByteBuffer Slice()  //This is not correct! Leon
        {
            return new ByteBuffer(buffer, -1, 0, this.Remain, this.Remain, this.Position + offset);
        }
 
        public ByteBuffer Duplicate()
        {
            return null;
        }
 
        protected int NextGetIndex()
        {
            if (position >= limit)
                throw new IndexOutOfRangeException();
            return position++;
        }
 
        protected int NextGetIndex(int nb)
        {
            if (limit - position < nb)
                throw new IndexOutOfRangeException();
            int p = position;
            position += nb;
            return p;
        }
 
        protected int NextPutIndex()
        {
            return NextGetIndex();
        }
 
        protected int NextPutIndex(int nb)
        {
            return NextGetIndex(nb);
        }
 
        protected int Index(int i)
        {
            return i + offset;
        }
 
        protected int CheckIndex(int i)
        {
            if(i < 0 || (i >= limit))
                throw new IndexOutOfRangeException();
            return i;
        }
 
        protected int CheckIndex(int i, int nb)
        {
            if ((i < 0) || (nb > limit - i))
                throw new IndexOutOfRangeException();
            return i;
        }
 
        public byte Read()
        {
            return buffer[Index(NextGetIndex())];
        }
 
        public byte this[int index]
        {
            get
            {
                return buffer[Index(CheckIndex(index))];
            }
            set
            {
             //   buffer[Index(NextPutIndex(index))] = value;
                if (index < 0 || index >= limit)
                    throw new IndexOutOfRangeException();
                buffer[Index(index)] = value;
            }
        }
 
        protected void CheckBounds(int off, int len, int size)
        {
            if ((off | len | (off + len) | (size - (off + len))) < 0)
                throw new IndexOutOfRangeException();
        }
 
        public ByteBuffer Read(byte[] buf)
        {
            return Read(buf, 0, buf.Length);
        }
 
 
        public ByteBuffer Read(byte[] dst, int offset, int length)
        {
            CheckBounds(offset, length, dst.Length);
            if (length > Remain)
                throw new ArgumentException();
 
            //for (int i = offset; i < offset + length; i++)
            //    dst[i] = Read();
            System.Array.Copy(buffer, Index(this.position), dst, offset, length);
            this.Position = Position + length;
            return this;
        }
 
        public ByteBuffer Write(byte x)
        {
            buffer[Index(NextPutIndex())] = x;
            return this;
        }
 
        public ByteBuffer Write(byte[] src, int offset, int length)
        {
            CheckBounds(offset, length, src.Length);
 
            if (length > Remain)
                throw new IndexOutOfRangeException();
 
            System.Array.Copy(src, offset, buffer, Index(this.Position), length);
 
            this.Position = this.Position + length;
 
            return this;
        }
 
        public ByteBuffer Write(ByteBuffer src)
        {
            if (src == this)
                throw new ArgumentException();
            int n = src.Remain;
 
            if (n > this.Remain)
                throw new IndexOutOfRangeException();
 
            for (int i = 0; i < n; i++)
                Write(src.Read());
            return this;
        }
 
 
        public ByteBuffer Write(byte[] data)
        {
            return Write(data, 0, data.Length);
        }
 
        public int Remain
        {
            get { return limit - position; }
        }
        
        public byte[] Buffer
        {
            get { return buffer; }
        }
 
        public int Offset
        {
            get { return offset; }
        }
 
        public bool HasBuffer
        {
            get { return true; }
        }
 
        public int Length
        {
            get { return capacity; }
        }
    }
}
/*
 
    private byte[] buffer;
        private int start;
        private int position;
       // private int capacity;
        private int length;
 
        public ByteBuffer(byte[] buffer, int start, int count)
        {
            this.buffer = buffer;
            this.start = position = start;
            //length = capacity = start + count;
            length = start + count;
        }
 
        public ByteBuffer(int capacity)
        {
            this.buffer = new byte[capacity];
            start = position = 0;
            length = capacity;
        }
 
        public ByteBuffer(byte[] buffer) : this(buffer, 0, buffer.Length)
        {
 
        }
        public void Write(byte[] data)
        {
            Write(data, 0, data.Length);
        }
 
        public void Write(byte[] data, int off, int count)
        {
            int len = data.Length;
 
            if ((off + count) > len || off < 0 || count < 0)
                throw new IndexOutOfRangeException();
 
            if (count > Remain)
                throw new Exception();
 
            for (int i = off; i < off + count; i++)
                buffer[position++] = data[i];
        }
 
        public ByteBuffer Write(int index, byte val)
        {
            if (index < 0 || index >= length)
                throw new IndexOutOfRangeException();
            buffer[index] = val;
 
            return this;
        }
 
        public int Remain
        {
            get { return length - position; }
        }
 
        public ByteBuffer Slice()  //This is not correct! Leon
        {
            ByteBuffer slice = new ByteBuffer(buffer, start + position, Remain);
            return slice;
        }
 
        public ByteBuffer Read(byte[] buf, int off, int count)
        {
            int len = buf.Length;
 
            if(off < 0 || count < 0 || (off + count > len))
                throw new IndexOutOfRangeException();
            if (count > Remain)
                throw new Exception();
 
            for (int i = off; i < off + count; i++)
                buf[i] = Read();
            return this;
        }
 
        public ByteBuffer Read(byte[] buf)
        {
            return Read(buf, 0, buf.Length);
        }
 
        public int Position
        {
            get { return position; }
            set
            {
                if (value < 0 || value > length)
                    throw new ArgumentException();
                position = value;
            }
        }
 
        public byte Read()
        {
            if (position == length)
                throw new Exception();
 
            return buffer[start + position++];
        }
 
        public byte[] Buffer
        {
            get { return buffer; }
        }
 
        public int Offset
        {
            get { return start; }
        }
 
        public bool HasBuffer
        {
            get { return true; }
        }
 
        public int Length
        {
            get { return length; }
        }
 
        //public int Position
        //{
        //    get { return position; }
        //}
 
        public byte this[int index]
        {
            get 
            {
                if (index < 0 || index > length)
                    throw new IndexOutOfRangeException();
                return buffer[index];
            }
        }
 
        public static ByteBuffer CreateBuffer(int capacity)
        {
            return new ByteBuffer(capacity);
        }
 
        public static ByteBuffer CreateBuffer(byte[] buffer, int start, int count)
        {
            int len = buffer.Length;
 
            if (start < 0 || count < 0 || (start + count > len))
                throw new IndexOutOfRangeException();
            ByteBuffer buf = new ByteBuffer(buffer);
            buf.position = start;
            buf.length = start + count;
 
            return buf;
 
        }
 
*/