Just add some syntactic sugar
[NovacoinLibrary.git] / Novacoin / ByteQueue.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using System;
20 using System.Collections.Generic;
21
22 namespace Novacoin
23 {
24     public class ByteQueueException : Exception
25     {
26         public ByteQueueException()
27         {
28         }
29
30         public ByteQueueException(string message)
31             : base(message)
32         {
33         }
34
35         public ByteQueueException(string message, Exception inner)
36             : base(message, inner)
37         {
38         }
39     }
40
41     public class ByteQueue
42     {
43         private int Index;
44         private List<byte> Elements;
45
46         public ByteQueue(IList<byte> List, int Start)
47         {
48             Elements = new List<byte>(List);
49             Index = Start;
50         }
51
52         public ByteQueue(IList<byte> List)
53         {
54             Elements = new List<byte>(List);
55             Index = 0;
56         }
57
58         public byte Get()
59         {
60             if (Elements.Count <= Index)
61             {
62                 throw new ByteQueueException("No elements left.");
63             }
64
65             return Elements[Index++];
66         }
67
68         public byte GetCurrent()
69         {
70             return Elements[Index];
71         }
72
73         public byte[] Get(int Count)
74         {
75             if (Elements.Count - Index < Count)
76             {
77                 throw new ByteQueueException("Unable to read requested amount of data.");
78             }
79
80             var result = Elements.GetRange(Index, Count).ToArray();
81             Index += Count;
82
83             return result;
84         }
85
86         public byte[] GetCurrent(int Count)
87         {
88             if (Elements.Count - Index < Count)
89             {
90                 throw new ByteQueueException("Unable to read requested amount of data.");
91             }
92
93             var result = Elements.GetRange(Index, Count).ToArray();
94
95             return result;
96         }
97
98         /// <summary>
99         /// Current index value
100         /// </summary>
101         public int CurrentIndex
102         {
103             get { return Index; }
104         }
105
106         public ulong GetVarInt()
107         {
108             byte prefix = Get();
109
110             switch (prefix)
111             {
112                 case 0xfd: // ushort
113                     return BitConverter.ToUInt16(Get(2), 0);
114                 case 0xfe: // uint
115                     return BitConverter.ToUInt32(Get(4), 0);
116                 case 0xff: // ulong
117                     return BitConverter.ToUInt64(Get(8), 0);
118                 default:
119                     return prefix;
120             }
121         }
122     }
123 }