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 bool TryGet(ref byte Element)
89 if (_Elements.Count <= _Index)
94 Element = _Elements[_Index++];
99 public byte GetCurrent()
101 return _Elements[_Index];
104 public byte[] Get(int nCount)
106 Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of elements.");
108 var result = _Elements.GetRange(_Index, nCount).ToArray();
114 public bool TryGet(int nCount, ref byte[] Elements)
116 if (Count - Index < nCount)
121 Elements = _Elements.GetRange(_Index, nCount).ToArray();
127 public byte[] GetCurrent(int nCount)
129 Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of elements.");
131 var result = _Elements.GetRange(_Index, nCount).ToArray();
136 public bool TryGetCurrent(int nCount, ref byte[] Elements)
138 if (Count - Index < nCount)
143 Elements = _Elements.GetRange(_Index, nCount).ToArray();
149 /// Current index value
153 get { return _Index; }
158 get { return _Elements.Count; }
161 public ulong GetVarInt()
168 return BitConverter.ToUInt16(Get(2), 0);
170 return BitConverter.ToUInt32(Get(4), 0);
172 return BitConverter.ToUInt64(Get(8), 0);