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;
27 public class InstructionQueueException : Exception
29 public InstructionQueueException()
33 public InstructionQueueException(string message)
38 public InstructionQueueException(string message, Exception inner)
39 : base(message, inner)
45 /// Stream of instructions.
47 public class InstructionQueue : IDisposable
49 private bool disposed = false;
51 private MemoryStream _Stream;
52 private BinaryReader _Reader;
54 public InstructionQueue(ref List<byte> List, int Start)
56 _Stream = new MemoryStream(List.ToArray());
57 _Stream.Seek(Start, SeekOrigin.Begin);
58 _Reader = new BinaryReader(_Stream);
61 public InstructionQueue(ref List<byte> List)
63 _Stream = new MemoryStream(List.ToArray());
64 _Reader = new BinaryReader(_Stream);
75 GC.SuppressFinalize(this);
78 protected virtual void Dispose(bool disposing)
94 if (_Stream.Position == _Stream.Length)
96 throw new InstructionQueueException("No instructions left.");
99 return _Reader.ReadByte();
102 public bool TryGet(ref byte Element)
104 if (_Stream.Position == _Stream.Length)
109 Element = _Reader.ReadByte();
114 public byte[] Get(int nCount)
116 Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of instructions.");
118 return _Reader.ReadBytes(nCount);
121 public bool TryGet(int nCount, ref byte[] Elements)
123 Elements = _Reader.ReadBytes(nCount);
124 return (Elements.Length == nCount);
128 /// Current index value
132 get { return (int)_Stream.Position; }
140 get { return (int)_Stream.Length; }