2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
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.
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.
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/>.
20 using System.Collections.Generic;
21 using System.Diagnostics.Contracts;
26 public class ByteQueueException : Exception
28 public ByteQueueException()
32 public ByteQueueException(string message)
37 public ByteQueueException(string message, Exception inner)
38 : base(message, inner)
46 /// TODO: rewrite using MemoryStream
48 public class ByteQueue
51 private List<byte> _Elements;
53 public ByteQueue(byte[] List, int Start)
55 _Elements = new List<byte>(List);
59 public ByteQueue(byte[] List)
61 _Elements = new List<byte>(List);
65 public ByteQueue(List<byte> List, int Start)
67 _Elements = new List<byte>(List);
71 public ByteQueue(List<byte> List)
73 _Elements = new List<byte>(List);
79 if (_Elements.Count <= _Index)
81 throw new ByteQueueException("No elements left.");
84 return _Elements[_Index++];
87 public byte GetCurrent()
89 return _Elements[_Index];
92 public byte[] Get(int nCount)
94 Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of elements.");
96 var result = _Elements.GetRange(_Index, nCount).ToArray();
102 public byte[] GetCurrent(int nCount)
104 Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of elements.");
106 var result = _Elements.GetRange(_Index, nCount).ToArray();
112 /// Current index value
116 get { return _Index; }
121 get { return _Elements.Count; }
124 public ulong GetVarInt()
131 return BitConverter.ToUInt16(Get(2), 0);
133 return BitConverter.ToUInt32(Get(4), 0);
135 return BitConverter.ToUInt64(Get(8), 0);