-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamRW.cs
132 lines (109 loc) · 3.5 KB
/
StreamRW.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace OpenMcdf
{
internal class StreamRW
{
private byte[] buffer = new byte[8];
private Stream stream;
public StreamRW(Stream stream)
{
this.stream = stream;
}
public long Seek(long offset)
{
return stream.Seek(offset, SeekOrigin.Begin);
}
public byte ReadByte()
{
return (byte)this.stream.ReadByte();
}
public ushort ReadUInt16()
{
this.stream.Read(buffer, 0, 2);
return (ushort)(buffer[0] | (buffer[1] << 8));
}
public int ReadInt32()
{
this.stream.Read(buffer, 0, 4);
return (int)(buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24));
}
public uint ReadUInt32()
{
this.stream.Read(buffer, 0, 4);
return (uint)(buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24));
}
public long ReadInt64()
{
this.stream.Read(buffer, 0, 8);
uint ls = (uint)(buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24));
uint ms = (uint)((buffer[4]) | (buffer[5] << 8) | (buffer[6] << 16) | (buffer[7] << 24));
return (long)((ms << 32) | ls);
}
public ulong ReadUInt64()
{
this.stream.Read(buffer, 0, 8);
return (ulong)(buffer[0] | (buffer[1] << 8) | (buffer[2] << 16) | (buffer[3] << 24) | (buffer[4] << 32) | (buffer[5] << 40) | (buffer[6] << 48) | (buffer[7] << 56));
}
public byte[] ReadBytes(int count)
{
byte[] result = new byte[count];
this.stream.Read(result, 0, count);
return result;
}
public byte[] ReadBytes(int count, out int r_count)
{
byte[] result = new byte[count];
r_count = this.stream.Read(result, 0, count);
return result;
}
public void Write(byte b)
{
this.stream.WriteByte(b);
}
public void Write(ushort value)
{
buffer[0] = (byte)value;
buffer[1] = (byte)(value >> 8);
this.stream.Write(buffer, 0, 2);
}
public void Write(int value)
{
buffer[0] = (byte)value;
buffer[1] = (byte)(value >> 8);
buffer[2] = (byte)(value >> 16);
buffer[3] = (byte)(value >> 24);
this.stream.Write(buffer, 0, 4);
}
public void Write(long value)
{
buffer[0] = (byte)value;
buffer[1] = (byte)(value >> 8);
buffer[2] = (byte)(value >> 16);
buffer[3] = (byte)(value >> 24);
buffer[4] = (byte)(value >> 32);
buffer[5] = (byte)(value >> 40);
buffer[6] = (byte)(value >> 48);
buffer[7] = (byte)(value >> 56);
this.stream.Write(buffer, 0, 8);
}
public void Write(uint value)
{
buffer[0] = (byte)value;
buffer[1] = (byte)(value >> 8);
buffer[2] = (byte)(value >> 16);
buffer[3] = (byte)(value >> 24);
this.stream.Write(buffer, 0, 4);
}
public void Write(byte[] value)
{
this.stream.Write(value, 0, value.Length);
}
public void Close()
{
//Nothing to do ;-)
}
}
}