comparison IO/BinaryReaderBigEndian.cs @ 30:23fd4247fc1c

* Add "big-endian" binary reader that reads ints/shorts in big-endian format * Add methods to String Manipulation to output a byte string as hex characters no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 19 Apr 2009 11:31:47 +0000
parents
children 267cd5ce66ff
comparison
equal deleted inserted replaced
29:e38192f55d2d 30:23fd4247fc1c
1 // This file (FileStreamstream.cs) is a part of the IBBoard project and is copyright 2009 IBBoard
2 //
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
4 //
5
6 using System;
7 using System.Collections.Generic;
8 using System.IO;
9 using System.Text;
10
11 namespace IBBoard
12 {
13 /// <summary>
14 /// A custom replacement for the BinaryReader that reads numbers as big-endian and also provides methods to read byte delimited strings.
15 /// </summary>
16 public class BinaryReaderBigEndian
17 {
18 private Stream stream;
19 private Encoding encoding;
20
21 public BinaryReaderBigEndian(Stream input) : this(input, Encoding.UTF8)
22 {
23 }
24
25 public BinaryReaderBigEndian(Stream input, Encoding encoding)
26 {
27 stream = input;
28 this.encoding = encoding;
29 }
30
31 public byte[] ReadBytes(int byteCount)
32 {
33 byte[] bytes = new byte[byteCount];
34 stream.Read(bytes, 0, byteCount);
35 return bytes;
36 }
37
38 private byte[] ReadBytesForNumber(int byteCount)
39 {
40 byte[] bytes = ReadBytes(byteCount);
41 int halfCount = byteCount / 2;
42
43 for (int i = 0, j = byteCount - 1; i < halfCount; i++, j--)
44 {
45 byte temp = bytes[i];
46 bytes[i] = bytes[j];
47 bytes[j] = temp;
48 }
49
50 return bytes;
51 }
52
53 public byte ReadByte()
54 {
55 return (byte)stream.ReadByte();
56 }
57
58 public short ReadShort()
59 {
60 return BitConverter.ToInt16(ReadBytesForNumber(2), 0);
61 }
62
63 public ushort ReadUShort()
64 {
65 return BitConverter.ToUInt16(ReadBytesForNumber(2), 0);
66 }
67
68 public int ReadInt()
69 {
70 return BitConverter.ToInt32(ReadBytesForNumber(4), 0);
71 }
72
73 public uint ReadUInt()
74 {
75 return BitConverter.ToUInt32(ReadBytesForNumber(4), 0);
76 }
77
78 public string ReadDelimitedString(byte delimiter)
79 {
80 List<byte> bytes = new List<byte>();
81
82 while (!EndOfStream)
83 {
84 byte b = ReadByte();
85
86 if (b!=delimiter)
87 {
88 bytes.Add(b);
89 }
90 else
91 {
92 break;
93 }
94 }
95
96 return encoding.GetString(bytes.ToArray());
97 }
98
99 public string ReadString(int length)
100 {
101 byte[] bytes = ReadBytes(length);
102 return encoding.GetString(bytes);
103 }
104
105 public string ReadUShortLengthString()
106 {
107 ushort length = ReadUShort();
108 return ReadString(length);
109 }
110
111 public string ReadIntLengthString()
112 {
113 int length = ReadInt();
114 return ReadString(length);
115 }
116
117 public void Seek(int distance)
118 {
119 stream.Seek(distance, SeekOrigin.Begin);
120 }
121
122 public void Move(int distance)
123 {
124 stream.Seek(distance, SeekOrigin.Current);
125 }
126
127 public void Close()
128 {
129 stream.Close();
130 }
131
132 public bool EndOfStream
133 {
134 get { return stream.Length - 1 == stream.Position; }
135 }
136 }
137 }