-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamReader.cs
More file actions
204 lines (161 loc) · 5 KB
/
StreamReader.cs
File metadata and controls
204 lines (161 loc) · 5 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
using System;
using System.IO;
namespace IrbImgFormat
{
class StreamReader
{
private static Logging logging = new Logging("StreamReader");
private BinaryReader m_reader;
private bool m_eof;
private int m_offset;
private int m_blocklen;
private string filename;
public StreamReader(string filename)
{
this.m_eof = true;
this.m_offset = 0;
Open(filename);
}
/// <summary>
/// return the filename of the stream
/// </summary>
/// <returns></returns>
public string GetFileName()
{
try
{
return System.IO.Path.GetFileName(filename);
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// Open the stream
/// </summary>
/// <param name="filename"></param>
private void Open(string filename)
{
this.filename = filename;
//- Datei öffnen
try
{
m_reader = new System.IO.BinaryReader(System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read));
this.m_eof = false;
m_blocklen = (int)m_reader.BaseStream.Length; //- Beschränkung auf max 2GB Dateigröße
}
catch (System.Exception ex)
{
logging.addError("IO.BinaryReader fail", ex.Message, filename);
return;
}
}
/// <summary>
/// Close the stream
/// </summary>
public void Close()
{
if (m_reader != null) m_reader.Close();
}
/// <summary>
/// is the stream pointer at the end of the file?
/// </summary>
public bool Eof
{
get
{
return ((m_reader == null) || (m_eof));
}
}
/// <summary>
/// Sets the stream read position
/// </summary>
/// <param name="offset"></param>
public void SetOffset(int offset)
{
if ((offset < m_blocklen) && (offset >= 0))
{
m_offset = offset;
m_reader.BaseStream.Seek(m_offset, System.IO.SeekOrigin.Begin);
m_eof = false;
}
else
{
m_eof = true;
if (offset < 0) m_offset = 0;
if (offset >= m_blocklen) m_offset = m_blocklen;
m_reader.BaseStream.Seek(m_offset, System.IO.SeekOrigin.Begin);
}
}
/// <summary>
/// Read length bytes from the stream
/// </summary>
public byte[] ReadByte(int length, int offset)
{
if (length < 0) length = 0;
if (offset > -1)
{
m_reader.BaseStream.Seek(offset, System.IO.SeekOrigin.Begin);
m_offset = offset;
}
byte[] dataArray = null;
if (m_offset < m_blocklen)
{
if ((m_offset + length) > m_blocklen)
{
m_eof = true;
length = m_blocklen - m_offset;
}
dataArray = new byte[length];
try
{
length = m_reader.Read(dataArray, 0, length);
}
catch (Exception e)
{
logging.addError("readByte(): length: " + length + " / offset: " + offset + " - " + e.Message);
length = 0;
m_eof = true;
}
if (length != dataArray.Length) System.Array.Resize(ref dataArray, length);
}
else
{
if (length > 0)
{
logging.addWarning("ReadStr:EOF!");
length = 0;
}
m_eof = true;
}
if (length > 0) m_offset = m_offset + length;
return dataArray;
}
/// <summary>
/// Read a string from the stream
/// </summary>
public string ReadStr(int length, int offset = -1)
{
string outVal = "";
byte[] tmpData = this.ReadByte(length, offset);
if (tmpData != null)
{
outVal = System.Text.Encoding.Default.GetString(tmpData);
}
return outVal;
}
/// <summary>
/// Read big ending int from the stream
/// </summary>
public int ReadIntBE(int offset = -1)
{
byte[] tmpData = this.ReadByte(4, offset);
if ((tmpData == null) || (tmpData.Length != 4))
{
return 0;
}
return (int)(tmpData[0] | (tmpData[1] << 8) | (tmpData[2] << 16) | (tmpData[3] << 24));
}
}
}