Repository navigation
Expand file tree
/
Copy pathByteQueue.cs
More file actions
211 lines (186 loc) · 6.84 KB
/
Copy pathByteQueue.cs
File metadata and controls
211 lines (186 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.IO;
namespace Org.BouncyCastle.Crypto.Tls
{
/// <remarks>
/// A queue for bytes.
/// <p>
/// This file could be more optimized.
/// </p>
/// </remarks>
public class ByteQueue
{
/// <returns>The smallest number which can be written as 2^x which is bigger than i.</returns>
public static int NextTwoPow(
int i)
{
/*
* This code is based of a lot of code I found on the Internet
* which mostly referenced a book called "Hacking delight".
*
*/
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
return i + 1;
}
/**
* The initial size for our buffer.
*/
private const int DefaultCapacity = 1024;
/**
* The buffer where we store our data.
*/
private byte[] databuf;
/**
* How many bytes at the beginning of the buffer are skipped.
*/
private int skipped = 0;
/**
* How many bytes in the buffer are valid data.
*/
private int available = 0;
private bool readOnlyBuf = false;
public ByteQueue()
: this(DefaultCapacity)
{
}
public ByteQueue(int capacity)
{
this.databuf = capacity == 0 ? TlsUtilities.EmptyBytes : new byte[capacity];
}
public ByteQueue(byte[] buf, int off, int len)
{
this.databuf = buf;
this.skipped = off;
this.available = len;
this.readOnlyBuf = true;
}
/// <summary>Add some data to our buffer.</summary>
/// <param name="data">A byte-array to read data from.</param>
/// <param name="offset">How many bytes to skip at the beginning of the array.</param>
/// <param name="len">How many bytes to read from the array.</param>
public void AddData(
byte[] data,
int offset,
int len)
{
if (readOnlyBuf)
throw new InvalidOperationException("Cannot add data to read-only buffer");
if ((skipped + available + len) > databuf.Length)
{
int desiredSize = ByteQueue.NextTwoPow(available + len);
if (desiredSize > databuf.Length)
{
byte[] tmp = new byte[desiredSize];
Array.Copy(databuf, skipped, tmp, 0, available);
databuf = tmp;
}
else
{
Array.Copy(databuf, skipped, databuf, 0, available);
}
skipped = 0;
}
Array.Copy(data, offset, databuf, skipped + available, len);
available += len;
}
/// <summary>The number of bytes which are available in this buffer.</summary>
public int Available
{
get { return available; }
}
/// <summary>Copy some bytes from the beginning of the data to the provided <c cref="Stream">Stream</c>.</summary>
/// <param name="output">The <c cref="Stream">Stream</c> to copy the bytes to.</param>
/// <param name="length">How many bytes to copy.</param>
/// <exception cref="InvalidOperationException">If insufficient data is available.</exception>
/// <exception cref="IOException">If there is a problem copying the data.</exception>
public void CopyTo(Stream output, int length)
{
if (length > available)
throw new InvalidOperationException("Cannot copy " + length + " bytes, only got " + available);
output.Write(databuf, skipped, length);
}
/// <summary>Read data from the buffer.</summary>
/// <param name="buf">The buffer where the read data will be copied to.</param>
/// <param name="offset">How many bytes to skip at the beginning of buf.</param>
/// <param name="len">How many bytes to read at all.</param>
/// <param name="skip">How many bytes from our data to skip.</param>
public void Read(
byte[] buf,
int offset,
int len,
int skip)
{
if ((buf.Length - offset) < len)
{
throw new ArgumentException("Buffer size of " + buf.Length + " is too small for a read of " + len + " bytes");
}
if ((available - skip) < len)
{
throw new InvalidOperationException("Not enough data to read");
}
Array.Copy(databuf, skipped + skip, buf, offset, len);
}
/// <summary>Return a <c cref="MemoryStream">MemoryStream</c> over some bytes at the beginning of the data.</summary>
/// <param name="length">How many bytes will be readable.</param>
/// <returns>A <c cref="MemoryStream">MemoryStream</c> over the data.</returns>
/// <exception cref="InvalidOperationException">If insufficient data is available.</exception>
public MemoryStream ReadFrom(int length)
{
if (length > available)
throw new InvalidOperationException("Cannot read " + length + " bytes, only got " + available);
int position = skipped;
available -= length;
skipped += length;
return new MemoryStream(databuf, position, length, false);
}
/// <summary>Remove some bytes from our data from the beginning.</summary>
/// <param name="i">How many bytes to remove.</param>
public void RemoveData(
int i)
{
if (i > available)
{
throw new InvalidOperationException("Cannot remove " + i + " bytes, only got " + available);
}
/*
* Skip the data.
*/
available -= i;
skipped += i;
}
public void RemoveData(byte[] buf, int off, int len, int skip)
{
Read(buf, off, len, skip);
RemoveData(skip + len);
}
public byte[] RemoveData(int len, int skip)
{
byte[] buf = new byte[len];
RemoveData(buf, 0, len, skip);
return buf;
}
public void Shrink()
{
if (available == 0)
{
databuf = TlsUtilities.EmptyBytes;
skipped = 0;
}
else
{
int desiredSize = ByteQueue.NextTwoPow(available);
if (desiredSize < databuf.Length)
{
byte[] tmp = new byte[desiredSize];
Array.Copy(databuf, skipped, tmp, 0, available);
databuf = tmp;
skipped = 0;
}
}
}
}
}