Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BinaryReader.cs « System.IO « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 573ab00c416f08b35e37f38ba1be45a54f40ce68 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// System.IO.BinaryReader
//
// Author:
//   Matt Kimball (matt@kimball.net)
//

using System;
using System.Text;
using System.Globalization;

namespace System.IO {
	public class BinaryReader : IDisposable {
		Stream m_stream;
		Encoding m_encoding;
		int m_encoding_max_byte;

		byte[] m_buffer;
		int m_buffer_used;
		int m_buffer_pos;


		public BinaryReader(Stream input) : this(input, Encoding.UTF8) {
		}

		public BinaryReader(Stream input, Encoding encoding) {
			if (input == null || encoding == null) 
				throw new ArgumentNullException(Locale.GetText ("Input or Encoding is a null reference."));
			if (!input.CanRead)
				throw new ArgumentException(Locale.GetText ("The stream doesn't support reading."));

			m_stream = input;
			m_encoding = encoding;
			m_encoding_max_byte = m_encoding.GetMaxByteCount(1);
			m_buffer = new byte [32];
		}

		public virtual Stream BaseStream {
			get {
				return m_stream;
			}
		}

		public virtual void Close() {
			Dispose();
			m_stream.Close();
		}

		public virtual void Dispose() {
			m_buffer = null;
			m_buffer_used = 0;
		}

		protected virtual void FillBuffer(int bytes) {
			if (!EnsureBuffered(m_buffer_used - m_buffer_pos + bytes)) {
				throw new EndOfStreamException();
			}
		}

		public virtual int PeekChar() {
			EnsureBuffered(m_encoding_max_byte);
			
			int i;
			for (i = 1; m_encoding.GetCharCount(m_buffer, m_buffer_pos, i) == 0; i++) {
				if (m_buffer_pos + i >= m_buffer_used) {
					return -1;
				}
			}

			char[] decode = m_encoding.GetChars(m_buffer, m_buffer_pos, i);
			return decode[0];
		}

		public virtual int Read() {
			char[] decode = new char[1];

			Read(decode, 0, 1);
			return decode[0];
		}

		public virtual int Read(byte[] buffer, int index, int count) {
			if (buffer == null) {
				throw new ArgumentNullException();
			}
			if (buffer.Length - index < count) {
				throw new ArgumentException();
			}
			if (index < 0 || count < 0) {
				throw new ArgumentOutOfRangeException();
			}

			EnsureBuffered(count);
			
			if (m_buffer_used - m_buffer_pos < count) {
				count = m_buffer_used - m_buffer_pos;
			}

			Array.Copy(m_buffer, m_buffer_pos, buffer, index, count);

			ConsumeBuffered(count);
			return count;
		}

		public virtual int Read(char[] buffer, int index, int count) {
			if (buffer == null) {
				throw new ArgumentNullException();
			}
			if (buffer.Length - index < count) {
				throw new ArgumentException();
			}
			if (index < 0 || count < 0) {
				throw new ArgumentOutOfRangeException();
			}

			EnsureBuffered(m_encoding_max_byte * count);

			int i;		
			for (i = 1; m_encoding.GetCharCount(m_buffer, m_buffer_pos, i) < count; i++) {
				if (m_buffer_pos + i >= m_buffer_used) {
					break;
				}
			}
			
			count = m_encoding.GetCharCount(m_buffer, m_buffer_pos, i);

			char[] dec = m_encoding.GetChars(m_buffer, m_buffer_pos, i);
			Array.Copy(dec, 0, buffer, index, count);

			ConsumeBuffered(i);
			return count;
		}

		protected int Read7BitEncodedInt() {
			int ret = 0;
			int shift = 0;
			int count = 0;
			byte b;

			do {
				if (!EnsureBuffered(++count)) {
					throw new EndOfStreamException();
				}
				b = m_buffer[m_buffer_pos + count - 1];
				
				ret = ret | ((b & 0x7f) << shift);
				shift += 7;
			} while ((b & 0x80) == 0x80);

			ConsumeBuffered(count);
			return ret;
		}

		public virtual bool ReadBoolean() {
			if (!EnsureBuffered(1)) {
				throw new EndOfStreamException();
			}

			// Return value:
			//  true if the byte is non-zero; otherwise false.
			bool ret = (m_buffer[m_buffer_pos] != 0);
			ConsumeBuffered(1);
			return ret;
		}

		public virtual byte ReadByte() {
			if (!EnsureBuffered(1)) {
				throw new EndOfStreamException();
			}

			byte ret = m_buffer[m_buffer_pos];
			ConsumeBuffered(1);
			return ret;
		}

		public virtual byte[] ReadBytes(int count) {
			if (count < 0) {
				throw new ArgumentOutOfRangeException();
			}

			EnsureBuffered(count);

			if (count > m_buffer_used - m_buffer_pos) {
				count = m_buffer_used - m_buffer_pos;
			}

			if (count == 0) {
				throw new EndOfStreamException();
			}

			byte[] buf = new byte[count];
			Read(buf, 0, count);
			return buf;
		}

		public virtual char ReadChar() {
			char[] buf = ReadChars(1);
			return buf[0];
		}

		public virtual char[] ReadChars(int count) {
			if (count < 0) {
				throw new ArgumentOutOfRangeException();
			}

			char[] full = new char[count];
			count = Read(full, 0, count);
			
			if (count != full.Length) {
				char[] ret = new char[count];
				Array.Copy(full, 0, ret, 0, count);
				return ret;
			} else {
				return full;
			}
		}

		unsafe public virtual decimal ReadDecimal() {
			if (!EnsureBuffered(16)) {
				throw new EndOfStreamException();
			}

			decimal ret;
			byte* ret_ptr = (byte *)&ret;
			for (int i = 0; i < 16; i++) {
				ret_ptr[i] = m_buffer[m_buffer_pos + i];
			}

			ConsumeBuffered(16);
			return ret;
		}

		public virtual double ReadDouble() {
			if (!EnsureBuffered(8)) {
				throw new EndOfStreamException();
			}

			double ret = BitConverter.ToDouble(m_buffer, m_buffer_pos);
			ConsumeBuffered(8);
			return ret;
		}

		public virtual short ReadInt16() {
			if (!EnsureBuffered(2)) {
				throw new EndOfStreamException();
			}

			short ret = (short) (m_buffer[m_buffer_pos] | (m_buffer[m_buffer_pos + 1] << 8));
			ConsumeBuffered(2);
			return ret;
		}

		public virtual int ReadInt32() {
			if (!EnsureBuffered(4)) {
				throw new EndOfStreamException();
			}

			int ret = (m_buffer[m_buffer_pos]             |
			           (m_buffer[m_buffer_pos + 1] << 8)  |
			           (m_buffer[m_buffer_pos + 2] << 16) |
			           (m_buffer[m_buffer_pos + 3] << 24)
			          );
			ConsumeBuffered(4);
			return ret;
		}

		public virtual long ReadInt64() {
			if (!EnsureBuffered(8)) {
				throw new EndOfStreamException();
			}

			uint ret_low  = (uint) (m_buffer[m_buffer_pos]            |
			                       (m_buffer[m_buffer_pos + 1] << 8)  |
			                       (m_buffer[m_buffer_pos + 2] << 16) |
			                       (m_buffer[m_buffer_pos + 3] << 24)
			                       );
			uint ret_high = (uint) (m_buffer[m_buffer_pos + 4]        |
			                       (m_buffer[m_buffer_pos + 5] << 8)  |
			                       (m_buffer[m_buffer_pos + 6] << 16) |
			                       (m_buffer[m_buffer_pos + 7] << 24)
			                       );
			ConsumeBuffered(8);
			return (long) ((((ulong) ret_high) << 32) | ret_low);
		}

		[CLSCompliant(false)]
		unsafe public virtual sbyte ReadSByte() {
			if (!EnsureBuffered(1)) {
				throw new EndOfStreamException();
			}

			sbyte ret;
			byte* ret_ptr = (byte *)&ret;
			ret_ptr[0] = m_buffer[m_buffer_pos];

			ConsumeBuffered(1);
			return ret;
		}

		public virtual string ReadString() {
			int len = Read7BitEncodedInt();

			char[] str = ReadChars(len);
			string ret = "";
			for (int i = 0; i < str.Length; i++) {
				ret = ret + str[i];
			}

			return ret;
		}

		public virtual float ReadSingle() {
			if (!EnsureBuffered(4)) {
				throw new EndOfStreamException();
			}

			float ret = BitConverter.ToSingle(m_buffer, m_buffer_pos);
			ConsumeBuffered(4);
			return ret;
		}

		[CLSCompliant(false)]
		public virtual ushort ReadUInt16() {
			if (!EnsureBuffered(2)) {
				throw new EndOfStreamException();
			}

			ushort ret = (ushort) (m_buffer[m_buffer_pos] | (m_buffer[m_buffer_pos + 1] << 8));
			ConsumeBuffered(2);
			return ret;
		}

		[CLSCompliant(false)]
		public virtual uint ReadUInt32() {
			if (!EnsureBuffered(4)) {
				throw new EndOfStreamException();
			}

			uint ret = (uint) (m_buffer[m_buffer_pos]            |
			                  (m_buffer[m_buffer_pos + 1] << 8)  |
			                  (m_buffer[m_buffer_pos + 2] << 16) |
			                  (m_buffer[m_buffer_pos + 3] << 24)
			                  );
			ConsumeBuffered(4);
			return ret;
		}

		[CLSCompliant(false)]
		public virtual ulong ReadUInt64() {
			if (!EnsureBuffered(8)) {
				throw new EndOfStreamException();
			}

			uint ret_low  = (uint) (m_buffer[m_buffer_pos]            |
			                       (m_buffer[m_buffer_pos + 1] << 8)  |
			                       (m_buffer[m_buffer_pos + 2] << 16) |
			                       (m_buffer[m_buffer_pos + 3] << 24)
			                       );
			uint ret_high = (uint) (m_buffer[m_buffer_pos + 4]        |
			                       (m_buffer[m_buffer_pos + 5] << 8)  |
			                       (m_buffer[m_buffer_pos + 6] << 16) |
			                       (m_buffer[m_buffer_pos + 7] << 24)
			                       );
			ConsumeBuffered(8);
			return (((ulong) ret_high) << 32) | ret_low;
		}

		
		bool EnsureBuffered(int bytes) {
			int needed = bytes - (m_buffer_used - m_buffer_pos);
			if (needed < 0)
				return true;

			if (m_buffer_used + needed > m_buffer.Length) {
				byte[] old_buffer = m_buffer;
				m_buffer = new byte[m_buffer_used + needed];
				Array.Copy(old_buffer, 0, m_buffer, 0, m_buffer_used);
				m_buffer_pos = m_buffer_used;
			}

			int n = m_stream.Read(m_buffer, m_buffer_used, needed);
			if (n == 0) return false;

			m_buffer_used += n;

			return (m_buffer_used >= m_buffer_pos + bytes);
		}


		void ConsumeBuffered(int bytes) {
			m_buffer_pos += bytes;
		}
	}
}