-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathBitReader.cs
More file actions
76 lines (63 loc) · 1.53 KB
/
Copy pathBitReader.cs
File metadata and controls
76 lines (63 loc) · 1.53 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
using System;
using System.IO;
namespace Pathfinder.Util
{
public class BitReader : IDisposable
{
private BinaryReader Reader;
private byte currentByte = 0;
private int offset = 8;
public BitReader(Stream stream)
{
Reader = new BinaryReader(stream);
}
public byte ReadByte()
{
if (offset == 8)
{
return Reader.ReadByte();
}
return (byte)ReadBitsDepth(8);
}
public byte[] ReadBytes(int count)
{
byte[] ret = new byte[count];
for (int i = 0; i < count; i++)
{
ret[i] = ReadByte();
}
return ret;
}
public ushort ReadUInt16()
{
ushort ret = 0;
for (int i = 0; i < 2 ; i++)
{
ret |= (ushort)(ReadByte() << i * 8);
}
return ret;
}
public int ReadBit()
{
if (offset == 8)
{
currentByte = Reader.ReadByte();
offset = 0;
}
return (currentByte >> offset++) & 1;
}
public uint ReadBitsDepth(int bitDepth)
{
byte ret = 0;
for (int i = 0; i < bitDepth; i++)
{
ret |= (byte)(ReadBit() << i);
}
return ret;
}
public void Dispose()
{
Reader.Dispose();
}
}
}