Replace ByteQueue with its simplified version, InstructionQueue, since it's used...
[NovacoinLibrary.git] / Novacoin / InstructionQueue.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.Diagnostics.Contracts;
22 using System.IO;
23
24 namespace Novacoin
25 {
26     [Serializable]
27     public class InstructionQueueException : Exception
28     {
29         public InstructionQueueException()
30         {
31         }
32
33         public InstructionQueueException(string message)
34             : base(message)
35         {
36         }
37
38         public InstructionQueueException(string message, Exception inner)
39             : base(message, inner)
40         {
41         }
42     }
43
44     /// <summary>
45     /// Stream of instructions.
46     /// </summary>
47     public class InstructionQueue : IDisposable
48     {
49         private bool disposed = false;
50
51         private MemoryStream _Stream;
52         private BinaryReader _Reader;
53
54         public InstructionQueue(ref List<byte> List, int Start)
55         {
56             _Stream = new MemoryStream(List.ToArray());
57             _Stream.Seek(Start, SeekOrigin.Begin);
58             _Reader = new BinaryReader(_Stream);
59         }
60
61         public InstructionQueue(ref List<byte> List)
62         {
63             _Stream = new MemoryStream(List.ToArray());
64             _Reader = new BinaryReader(_Stream);
65         }
66
67         ~InstructionQueue()
68         {
69             Dispose(false);
70         }
71
72         public void Dispose()
73         {
74             Dispose(true);
75             GC.SuppressFinalize(this);
76         }
77
78         protected virtual void Dispose(bool disposing)
79         {
80             if (!disposed)
81             {
82                 if (disposing)
83                 {
84                     _Reader.Dispose();
85                     _Stream.Dispose();
86                 }
87
88                 disposed = true;
89             }
90         }
91
92         public byte Get()
93         {
94             if (_Stream.Position == _Stream.Length)
95             {
96                 throw new InstructionQueueException("No instructions left.");
97             }
98
99             return _Reader.ReadByte();
100         }
101
102         public bool TryGet(ref byte Element)
103         {
104             if (_Stream.Position == _Stream.Length)
105             {
106                 return false;
107             }
108
109             Element = _Reader.ReadByte();
110
111             return true;
112         }
113
114         public byte[] Get(int nCount)
115         {
116             Contract.Requires<ArgumentException>(Count - Index >= nCount, "nCount is greater than amount of instructions.");
117
118             return _Reader.ReadBytes(nCount);
119         }
120
121         public bool TryGet(int nCount, ref byte[] Elements)
122         {
123             Elements = _Reader.ReadBytes(nCount);
124             return (Elements.Length == nCount);
125         }
126
127         /// <summary>
128         /// Current index value
129         /// </summary>
130         public int Index
131         {
132             get { return (int)_Stream.Position; }
133         }
134
135         /// <summary>
136         /// Stream length
137         /// </summary>
138         public int Count
139         {
140             get { return (int)_Stream.Length; }
141         }
142     }
143 }
144
145