Add license header.
[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 using System.Linq;
22
23 namespace Novacoin
24 {
25     public class ByteQueueException : Exception
26     {
27         public ByteQueueException()
28         {
29         }
30
31         public ByteQueueException(string message)
32             : base(message)
33         {
34         }
35
36         public ByteQueueException(string message, Exception inner)
37             : base(message, inner)
38         {
39         }
40     }
41
42     public class ByteQueue
43     {
44         private int Index;
45         private List<byte> Elements;
46
47         public ByteQueue(IList<byte> List, int Start)
48         {
49             Elements = new List<byte>(List);
50             Index = Start;
51         }
52
53         public ByteQueue(IList<byte> List)
54         {
55             Elements = new List<byte>(List);
56             Index = 0;
57         }
58
59         public byte Get()
60         {
61             if (Elements.Count <= Index)
62             {
63                 throw new ByteQueueException("No elements left.");
64             }
65
66             return Elements[Index++];
67         }
68
69         public byte GetCurrent()
70         {
71             return Elements[Index];
72         }
73
74         public byte[] Get(int Count)
75         {
76             if (Elements.Count - Index < Count)
77             {
78                 throw new ByteQueueException("Unable to read requested amount of data.");
79             }
80
81             byte[] result = Elements.Skip(Index).Take(Count).ToArray();
82             Index += Count;
83
84             return result;
85         }
86
87         public byte[] GetCurrent(int Count)
88         {
89             if (Elements.Count - Index < Count)
90             {
91                 throw new ByteQueueException("Unable to read requested amount of data.");
92             }
93
94             byte[] result = Elements.Skip(Index).Take(Count).ToArray();
95
96             return result;
97         }
98
99         /// <summary>
100         /// Current index value
101         /// </summary>
102         public int CurrentIndex
103         {
104             get { return Index; }
105         }
106
107         public IEnumerable<byte> GetEnumerable(int Count)
108         {
109             if (Elements.Count - Index < Count)
110             {
111                 throw new ByteQueueException("Unable to read requested amount of data.");
112             }
113
114             IEnumerable<byte> result = Elements.Skip(Index).Take(Count);
115             Index += Count;
116
117             return result;
118         }
119
120         public ulong GetVarInt()
121         {
122             byte prefix = Get();
123
124             switch (prefix)
125             {
126                 case 0xfd: // ushort
127                     return BitConverter.ToUInt16(Get(2), 0);
128                 case 0xfe: // uint
129                     return BitConverter.ToUInt32(Get(4), 0);
130                 case 0xff: // ulong
131                     return BitConverter.ToUInt64(Get(8), 0);
132                 default:
133                     return prefix;
134             }
135         }
136     }
137 }