forked from renmengye/base62-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitStream.cs
More file actions
210 lines (189 loc) · 6.48 KB
/
BitStream.cs
File metadata and controls
210 lines (189 loc) · 6.48 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
using System;
using System.IO;
namespace Base62
{
/// <summary>
/// Utility that read and write bits in byte array
/// </summary>
public class BitStream : Stream
{
private byte[] Source { get; set; }
/// <summary>
/// Initialize the stream with capacity
/// </summary>
/// <param name="capacity">Capacity of the stream</param>
public BitStream(int capacity)
{
this.Source = new byte[capacity];
}
/// <summary>
/// Initialize the stream with a source byte array
/// </summary>
/// <param name="source"></param>
public BitStream(byte[] source)
{
this.Source = source;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Flush()
{
throw new NotImplementedException();
}
/// <summary>
/// Bit length of the stream
/// </summary>
public override long Length
{
get { return Source.Length * 8; }
}
/// <summary>
/// Bit position of the stream
/// </summary>
public override long Position { get; set; }
/// <summary>
/// Read the stream to the buffer
/// </summary>
/// <param name="buffer">Buffer</param>
/// <param name="offset">Offset bit start position of the stream</param>
/// <param name="count">Number of bits to read</param>
/// <returns>Number of bits read</returns>
public override int Read(byte[] buffer, int offset, int count)
{
// Temporary position cursor
long tempPos = this.Position;
tempPos += offset;
// Buffer byte position and in-byte position
int readPosCount = 0, readPosMod = 0;
// Stream byte position and in-byte position
long posCount = tempPos >> 3;
int posMod = (int)(tempPos - ((tempPos >> 3) << 3));
while (tempPos < this.Position + offset + count && tempPos < this.Length)
{
// Copy the bit from the stream to buffer
if ((((int)this.Source[posCount]) & (0x1 << (7 - posMod))) != 0)
{
buffer[readPosCount] = (byte)((int)(buffer[readPosCount]) | (0x1 << (7 - readPosMod)));
}
else
{
buffer[readPosCount] = (byte)((int)(buffer[readPosCount]) & (0xffffffff - (0x1 << (7 - readPosMod))));
}
// Increment position cursors
tempPos++;
if (posMod == 7)
{
posMod = 0;
posCount++;
}
else
{
posMod++;
}
if (readPosMod == 7)
{
readPosMod = 0;
readPosCount++;
}
else
{
readPosMod++;
}
}
int bits = (int)(tempPos - this.Position - offset);
this.Position = tempPos;
return bits;
}
/// <summary>
/// Set up the stream position
/// </summary>
/// <param name="offset">Position</param>
/// <param name="origin">Position origin</param>
/// <returns>Position after setup</returns>
public override long Seek(long offset, SeekOrigin origin)
{
switch (origin)
{
case (SeekOrigin.Begin):
{
this.Position = offset;
break;
}
case (SeekOrigin.Current):
{
this.Position += offset;
break;
}
case (SeekOrigin.End):
{
this.Position = this.Length + offset;
break;
}
}
return this.Position;
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
/// <summary>
/// Write from buffer to the stream
/// </summary>
/// <param name="buffer"></param>
/// <param name="offset">Offset start bit position of buffer</param>
/// <param name="count">Number of bits</param>
public override void Write(byte[] buffer, int offset, int count)
{
// Temporary position cursor
long tempPos = this.Position;
// Buffer byte position and in-byte position
int readPosCount = offset >> 3, readPosMod = offset - ((offset >> 3) << 3);
// Stream byte position and in-byte position
long posCount = tempPos >> 3;
int posMod = (int)(tempPos - ((tempPos >> 3) << 3));
while (tempPos < this.Position + count && tempPos < this.Length)
{
// Copy the bit from buffer to the stream
if ((((int)buffer[readPosCount]) & (0x1 << (7 - readPosMod))) != 0)
{
this.Source[posCount] = (byte)((int)(this.Source[posCount]) | (0x1 << (7 - posMod)));
}
else
{
this.Source[posCount] = (byte)((int)(this.Source[posCount]) & (0xffffffff - (0x1 << (7 - posMod))));
}
// Increment position cursors
tempPos++;
if (posMod == 7)
{
posMod = 0;
posCount++;
}
else
{
posMod++;
}
if (readPosMod == 7)
{
readPosMod = 0;
readPosCount++;
}
else
{
readPosMod++;
}
}
this.Position = tempPos;
}
}
}