Add README.md
[NovacoinLibrary.git] / Novacoin / WrappedList.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace Novacoin
8 {
9     public class WrappedListException : Exception
10     {
11         public WrappedListException()
12         {
13         }
14
15         public WrappedListException(string message)
16             : base(message)
17         {
18         }
19
20         public WrappedListException(string message, Exception inner)
21             : base(message, inner)
22         {
23         }
24     }
25
26     public class ByteQueue
27     {
28         private int Index;
29         private List<byte> Elements;
30
31         public ByteQueue(IList<byte> List, int Start)
32         {
33             Elements = new List<byte>(List);
34             Index = Start;
35         }
36
37         public ByteQueue(IList<byte> List)
38         {
39             Elements = new List<byte>(List);
40             Index = 0;
41         }
42
43         public byte Get()
44         {
45             if (Elements.Count <= Index)
46             {
47                 throw new WrappedListException("No elements left.");
48             }
49
50             return Elements[Index++];
51         }
52
53         public byte GetCurrent()
54         {
55             return Elements[Index];
56         }
57
58         public byte[] Get(int Count)
59         {
60             if (Elements.Count - Index < Count)
61             {
62                 throw new WrappedListException("Unable to read requested amount of data.");
63             }
64
65             byte[] result = Elements.Skip(Index).Take(Count).ToArray();
66             Index += Count;
67
68             return result;
69         }
70
71         public byte[] GetCurrent(int Count)
72         {
73             if (Elements.Count - Index < Count)
74             {
75                 throw new WrappedListException("Unable to read requested amount of data.");
76             }
77
78             byte[] result = Elements.Skip(Index).Take(Count).ToArray();
79
80             return result;
81         }
82
83         public IEnumerable<byte> GetEnumerable(int Count)
84         {
85             if (Elements.Count - Index < Count)
86             {
87                 throw new WrappedListException("Unable to read requested amount of data.");
88             }
89
90             IEnumerable<byte> result = Elements.Skip(Index).Take(Count);
91             Index += Count;
92
93             return result;
94         }
95
96         public ulong GetVarInt()
97         {
98             byte prefix = Get();
99
100             switch (prefix)
101             {
102                 case 0xfd: // ushort
103                     return BitConverter.ToUInt16(Get(2), 0);
104                 case 0xfe: // uint
105                     return BitConverter.ToUInt32(Get(4), 0);
106                 case 0xff: // ulong
107                     return BitConverter.ToUInt64(Get(8), 0);
108                 default:
109                     return prefix;
110             }
111         }
112     }
113 }