corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5  
6 namespace CSJ2K.Util
7 {
8 internal class EndianBinaryReader : BinaryReader
9 {
10 private bool _bigEndian = false;
11  
12 // Summary:
13 // Initializes a new instance of the System.IO.BinaryReader class based on the
14 // supplied stream and using System.Text.UTF8Encoding.
15 //
16 // Parameters:
17 // input:
18 // A stream.
19 //
20 // Exceptions:
21 // System.ArgumentException:
22 // The stream does not support reading, the stream is null, or the stream is
23 // already closed.
24 public EndianBinaryReader(Stream input) : base(input)
25 {
26  
27 }
28 //
29 // Summary:
30 // Initializes a new instance of the System.IO.BinaryReader class based on the
31 // supplied stream and a specific character encoding.
32 //
33 // Parameters:
34 // encoding:
35 // The character encoding.
36 //
37 // input:
38 // The supplied stream.
39 //
40 // Exceptions:
41 // System.ArgumentNullException:
42 // encoding is null.
43 //
44 // System.ArgumentException:
45 // The stream does not support reading, the stream is null, or the stream is
46 // already closed.
47 public EndianBinaryReader(Stream input, Encoding encoding) : base(input, encoding)
48 {
49  
50 }
51  
52 public EndianBinaryReader(Stream input, Encoding encoding, bool bigEndian)
53 : base(input, encoding)
54 {
55 _bigEndian = bigEndian;
56 }
57  
58 public EndianBinaryReader(Stream input, bool bigEndian) : base(input, bigEndian ? Encoding.BigEndianUnicode : Encoding.ASCII)
59 {
60 _bigEndian = bigEndian;
61 }
62  
63 // Summary:
64 // Exposes access to the underlying stream of the System.IO.BinaryReader.
65 //
66 // Returns:
67 // The underlying stream associated with the BinaryReader.
68 //public virtual Stream BaseStream { get; }
69  
70 // Summary:
71 // Closes the current reader and the underlying stream.
72 //public virtual void Close();
73 //
74 // Summary:
75 // Releases the unmanaged resources used by the System.IO.BinaryReader and optionally
76 // releases the managed resources.
77 //
78 // Parameters:
79 // disposing:
80 // true to release both managed and unmanaged resources; false to release only
81 // unmanaged resources.
82 //protected virtual void Dispose(bool disposing);
83 //
84 // Summary:
85 // Fills the internal buffer with the specified number of bytes read from the
86 // stream.
87 //
88 // Parameters:
89 // numBytes:
90 // The number of bytes to be read.
91 //
92 // Exceptions:
93 // System.IO.EndOfStreamException:
94 // The end of the stream is reached before numBytes could be read.
95 //
96 // System.IO.IOException:
97 // An I/O error occurs.
98 //protected virtual void FillBuffer(int numBytes);
99 //
100 // Summary:
101 // Returns the next available character and does not advance the byte or character
102 // position.
103 //
104 // Returns:
105 // The next available character, or -1 if no more characters are available or
106 // the stream does not support seeking.
107 //
108 // Exceptions:
109 // System.IO.IOException:
110 // An I/O error occurs.
111 //public virtual int PeekChar();
112 //
113 // Summary:
114 // Reads characters from the underlying stream and advances the current position
115 // of the stream in accordance with the Encoding used and the specific character
116 // being read from the stream.
117 //
118 // Returns:
119 // The next character from the input stream, or -1 if no characters are currently
120 // available.
121 //
122 // Exceptions:
123 // System.ObjectDisposedException:
124 // The stream is closed.
125 //
126 // System.IO.IOException:
127 // An I/O error occurs.
128 //public virtual int Read();
129 //
130 // Summary:
131 // Reads count bytes from the stream with index as the starting point in the
132 // byte array.
133 //
134 // Parameters:
135 // count:
136 // The number of characters to read.
137 //
138 // buffer:
139 // The buffer to read data into.
140 //
141 // index:
142 // The starting point in the buffer at which to begin reading into the buffer.
143 //
144 // Returns:
145 // The number of characters read into buffer. This might be less than the number
146 // of bytes requested if that many bytes are not available, or it might be zero
147 // if the end of the stream is reached.
148 //
149 // Exceptions:
150 // System.ArgumentNullException:
151 // buffer is null.
152 //
153 // System.IO.IOException:
154 // An I/O error occurs.
155 //
156 // System.ObjectDisposedException:
157 // The stream is closed.
158 //
159 // System.ArgumentOutOfRangeException:
160 // index or count is negative.
161 //
162 // System.ArgumentException:
163 // The buffer length minus index is less than count.
164 //public virtual int Read(byte[] buffer, int index, int count);
165 //
166 // Summary:
167 // Reads count characters from the stream with index as the starting point in
168 // the character array.
169 //
170 // Parameters:
171 // count:
172 // The number of characters to read.
173 //
174 // buffer:
175 // The buffer to read data into.
176 //
177 // index:
178 // The starting point in the buffer at which to begin reading into the buffer.
179 //
180 // Returns:
181 // The total number of characters read into the buffer. This might be less than
182 // the number of characters requested if that many characters are not currently
183 // available, or it might be zero if the end of the stream is reached.
184 //
185 // Exceptions:
186 // System.ArgumentNullException:
187 // buffer is null.
188 //
189 // System.IO.IOException:
190 // An I/O error occurs.
191 //
192 // System.ObjectDisposedException:
193 // The stream is closed.
194 //
195 // System.ArgumentOutOfRangeException:
196 // index or count is negative.
197 //
198 // System.ArgumentException:
199 // The buffer length minus index is less than count.
200 //public virtual int Read(char[] buffer, int index, int count);
201 //
202 // Summary:
203 // Reads in a 32-bit integer in compressed format.
204 //
205 // Returns:
206 // A 32-bit integer in compressed format.
207 //
208 // Exceptions:
209 // System.ObjectDisposedException:
210 // The stream is closed.
211 //
212 // System.IO.IOException:
213 // An I/O error occurs.
214 //
215 // System.FormatException:
216 // The stream is corrupted.
217 //
218 // System.IO.EndOfStreamException:
219 // The end of the stream is reached.
220 //protected internal int Read7BitEncodedInt();
221 //
222 // Summary:
223 // Reads a Boolean value from the current stream and advances the current position
224 // of the stream by one byte.
225 //
226 // Returns:
227 // true if the byte is nonzero; otherwise, false.
228 //
229 // Exceptions:
230 // System.ObjectDisposedException:
231 // The stream is closed.
232 //
233 // System.IO.IOException:
234 // An I/O error occurs.
235 //
236 // System.IO.EndOfStreamException:
237 // The end of the stream is reached.
238 //public virtual bool ReadBoolean();
239 //
240 // Summary:
241 // Reads the next byte from the current stream and advances the current position
242 // of the stream by one byte.
243 //
244 // Returns:
245 // The next byte read from the current stream.
246 //
247 // Exceptions:
248 // System.ObjectDisposedException:
249 // The stream is closed.
250 //
251 // System.IO.IOException:
252 // An I/O error occurs.
253 //
254 // System.IO.EndOfStreamException:
255 // The end of the stream is reached.
256 //public virtual byte ReadByte();
257 //
258 // Summary:
259 // Reads count bytes from the current stream into a byte array and advances
260 // the current position by count bytes.
261 //
262 // Parameters:
263 // count:
264 // The number of bytes to read.
265 //
266 // Returns:
267 // A byte array containing data read from the underlying stream. This might
268 // be less than the number of bytes requested if the end of the stream is reached.
269 //
270 // Exceptions:
271 // System.ObjectDisposedException:
272 // The stream is closed.
273 //
274 // System.IO.IOException:
275 // An I/O error occurs.
276 //
277 // System.ArgumentOutOfRangeException:
278 // count is negative.
279 //public virtual byte[] ReadBytes(int count);
280 //
281 // Summary:
282 // Reads the next character from the current stream and advances the current
283 // position of the stream in accordance with the Encoding used and the specific
284 // character being read from the stream.
285 //
286 // Returns:
287 // A character read from the current stream.
288 //
289 // Exceptions:
290 // System.ObjectDisposedException:
291 // The stream is closed.
292 //
293 // System.IO.IOException:
294 // An I/O error occurs.
295 //
296 // System.IO.EndOfStreamException:
297 // The end of the stream is reached.
298 //
299 // System.ArgumentException:
300 // A surrogate character was read.
301 //public virtual char ReadChar();
302 //
303 // Summary:
304 // Reads count characters from the current stream, returns the data in a character
305 // array, and advances the current position in accordance with the Encoding
306 // used and the specific character being read from the stream.
307 //
308 // Parameters:
309 // count:
310 // The number of characters to read.
311 //
312 // Returns:
313 // A character array containing data read from the underlying stream. This might
314 // be less than the number of characters requested if the end of the stream
315 // is reached.
316 //
317 // Exceptions:
318 // System.ObjectDisposedException:
319 // The stream is closed.
320 //
321 // System.IO.IOException:
322 // An I/O error occurs.
323 //
324 // System.IO.EndOfStreamException:
325 // The end of the stream is reached.
326 //
327 // System.ArgumentOutOfRangeException:
328 // count is negative.
329 //public virtual char[] ReadChars(int count);
330 //
331 // Summary:
332 // Reads a decimal value from the current stream and advances the current position
333 // of the stream by sixteen bytes.
334 //
335 // Returns:
336 // A decimal value read from the current stream.
337 //
338 // Exceptions:
339 // System.ObjectDisposedException:
340 // The stream is closed.
341 //
342 // System.IO.IOException:
343 // An I/O error occurs.
344 //
345 // System.IO.EndOfStreamException:
346 // The end of the stream is reached.
347 public override decimal ReadDecimal()
348 {
349 if (_bigEndian)
350 {
351 // TODO: Is the whole thing reversed or just the individual ints?
352 // Maybe we should just call ReadInt32 4 times?
353 byte[] buf = this.ReadBytes(16);
354 Array.Reverse(buf);
355 int[] decimalints = new int[4];
356 decimalints[0]=BitConverter.ToInt32(buf, 0);
357 decimalints[1]=BitConverter.ToInt32(buf, 4);
358 decimalints[2]=BitConverter.ToInt32(buf, 8);
359 decimalints[3]=BitConverter.ToInt32(buf, 12);
360 return new decimal(decimalints);
361 }
362 else return base.ReadDecimal();
363 }
364 //
365 // Summary:
366 // Reads an 8-byte floating point value from the current stream and advances
367 // the current position of the stream by eight bytes.
368 //
369 // Returns:
370 // An 8-byte floating point value read from the current stream.
371 //
372 // Exceptions:
373 // System.ObjectDisposedException:
374 // The stream is closed.
375 //
376 // System.IO.IOException:
377 // An I/O error occurs.
378 //
379 // System.IO.EndOfStreamException:
380 // The end of the stream is reached.
381 public override double ReadDouble()
382 {
383 if (_bigEndian)
384 {
385 byte[] buf = this.ReadBytes(8);
386 Array.Reverse(buf);
387 return BitConverter.ToDouble(buf, 0);
388 }
389 else
390 return base.ReadDouble();
391 }
392 //
393 // Summary:
394 // Reads a 2-byte signed integer from the current stream and advances the current
395 // position of the stream by two bytes.
396 //
397 // Returns:
398 // A 2-byte signed integer read from the current stream.
399 //
400 // Exceptions:
401 // System.ObjectDisposedException:
402 // The stream is closed.
403 //
404 // System.IO.IOException:
405 // An I/O error occurs.
406 //
407 // System.IO.EndOfStreamException:
408 // The end of the stream is reached.
409 public override short ReadInt16()
410 {
411 if (_bigEndian)
412 {
413 byte[] buf = this.ReadBytes(2);
414 Array.Reverse(buf);
415 return BitConverter.ToInt16(buf, 0);
416 }
417 else
418 return base.ReadInt16();
419 }
420 //
421 // Summary:
422 // Reads a 4-byte signed integer from the current stream and advances the current
423 // position of the stream by four bytes.
424 //
425 // Returns:
426 // A 4-byte signed integer read from the current stream.
427 //
428 // Exceptions:
429 // System.ObjectDisposedException:
430 // The stream is closed.
431 //
432 // System.IO.IOException:
433 // An I/O error occurs.
434 //
435 // System.IO.EndOfStreamException:
436 // The end of the stream is reached.
437 public override int ReadInt32()
438 {
439 if (_bigEndian)
440 {
441 byte[] buf = this.ReadBytes(4);
442 Array.Reverse(buf);
443 return BitConverter.ToInt32(buf, 0);
444 }
445 else
446 return base.ReadInt32();
447 }
448 //
449 // Summary:
450 // Reads an 8-byte signed integer from the current stream and advances the current
451 // position of the stream by eight bytes.
452 //
453 // Returns:
454 // An 8-byte signed integer read from the current stream.
455 //
456 // Exceptions:
457 // System.ObjectDisposedException:
458 // The stream is closed.
459 //
460 // System.IO.IOException:
461 // An I/O error occurs.
462 //
463 // System.IO.EndOfStreamException:
464 // The end of the stream is reached.
465 public override long ReadInt64()
466 {
467 if (_bigEndian)
468 {
469 byte[] buf = this.ReadBytes(8);
470 Array.Reverse(buf);
471 return BitConverter.ToInt64(buf, 0);
472 }
473 else
474 return base.ReadInt64();
475 }
476 //
477 // Summary:
478 // Reads a signed byte from this stream and advances the current position of
479 // the stream by one byte.
480 //
481 // Returns:
482 // A signed byte read from the current stream.
483 //
484 // Exceptions:
485 // System.ObjectDisposedException:
486 // The stream is closed.
487 //
488 // System.IO.IOException:
489 // An I/O error occurs.
490 //
491 // System.IO.EndOfStreamException:
492 // The end of the stream is reached.
493 //[CLSCompliant(false)]
494 //public virtual byte Readbyte();
495 //
496 // Summary:
497 // Reads a 4-byte floating point value from the current stream and advances
498 // the current position of the stream by four bytes.
499 //
500 // Returns:
501 // A 4-byte floating point value read from the current stream.
502 //
503 // Exceptions:
504 // System.ObjectDisposedException:
505 // The stream is closed.
506 //
507 // System.IO.IOException:
508 // An I/O error occurs.
509 //
510 // System.IO.EndOfStreamException:
511 // The end of the stream is reached.
512 public override float ReadSingle()
513 {
514 if (_bigEndian)
515 {
516 byte[] buf = this.ReadBytes(4);
517 Array.Reverse(buf);
518 return BitConverter.ToSingle(buf, 0);
519 }
520 else
521 return base.ReadSingle();
522 }
523 //
524 // Summary:
525 // Reads a string from the current stream. The string is prefixed with the length,
526 // encoded as an integer seven bits at a time.
527 //
528 // Returns:
529 // The string being read.
530 //
531 // Exceptions:
532 // System.ObjectDisposedException:
533 // The stream is closed.
534 //
535 // System.IO.IOException:
536 // An I/O error occurs.
537 //
538 // System.IO.EndOfStreamException:
539 // The end of the stream is reached.
540 //public virtual string ReadString();
541 //
542 // Summary:
543 // Reads a 2-byte unsigned integer from the current stream using little-endian
544 // encoding and advances the position of the stream by two bytes.
545 //
546 // Returns:
547 // A 2-byte unsigned integer read from this stream.
548 //
549 // Exceptions:
550 // System.ObjectDisposedException:
551 // The stream is closed.
552 //
553 // System.IO.IOException:
554 // An I/O error occurs.
555 //
556 // System.IO.EndOfStreamException:
557 // The end of the stream is reached.
558 public override ushort ReadUInt16()
559 {
560 if (_bigEndian)
561 {
562 byte[] buf = this.ReadBytes(2);
563 Array.Reverse(buf);
564 return BitConverter.ToUInt16(buf, 0);
565 }
566 else
567 return base.ReadUInt16();
568 }
569 //
570 // Summary:
571 // Reads a 4-byte unsigned integer from the current stream and advances the
572 // position of the stream by four bytes.
573 //
574 // Returns:
575 // A 4-byte unsigned integer read from this stream.
576 //
577 // Exceptions:
578 // System.ObjectDisposedException:
579 // The stream is closed.
580 //
581 // System.IO.IOException:
582 // An I/O error occurs.
583 //
584 // System.IO.EndOfStreamException:
585 // The end of the stream is reached.
586 public override uint ReadUInt32()
587 {
588 if (_bigEndian)
589 {
590 byte[] buf = this.ReadBytes(4);
591 Array.Reverse(buf);
592 return BitConverter.ToUInt32(buf, 0);
593 }
594 else
595 return base.ReadUInt32();
596 }
597 //
598 // Summary:
599 // Reads an 8-byte unsigned integer from the current stream and advances the
600 // position of the stream by eight bytes.
601 //
602 // Returns:
603 // An 8-byte unsigned integer read from this stream.
604 //
605 // Exceptions:
606 // System.ObjectDisposedException:
607 // The stream is closed.
608 //
609 // System.IO.IOException:
610 // An I/O error occurs.
611 //
612 // System.IO.EndOfStreamException:
613 // The end of the stream is reached.
614 public override ulong ReadUInt64()
615 {
616 if (_bigEndian)
617 {
618 byte[] buf = this.ReadBytes(8);
619 Array.Reverse(buf);
620 return BitConverter.ToUInt64(buf, 0);
621 }
622 else
623 return base.ReadUInt64();
624 }
625 }
626 }