Blame |
Last modification |
View Log
| RSS feed
using System;
using System.IO;
using System.Linq;
using System.Text;
namespace EndianHandling
{
public class EndianBinaryReader
{
/* ===[ Notes ]====================================================== */
/* See notes in EndianBinaryWriter.cs.
*
*/
/* ===[ Fields ]===================================================== */
protected Stream input
;
protected Encoding encoding
;
protected BinaryReader reader
;
protected bool bigEnd
;
/* ===[ Constructors ]=============================================== */
public EndianBinaryReader
(Stream input
)
: this(input, Encoding
.GetEncoding(EndianBinaryWriter
.DEFAULT_ENCODING)) { }
public EndianBinaryReader
(Stream input, Encoding encoding
)
: this(input, encoding, EndianBinaryWriter
.DEFAULT_BIG_ENDIAN) { }
public EndianBinaryReader
(Stream input, Encoding encoding,
bool bigEndian
)
{
this.input = input
;
this.encoding = encoding
;
this.reader = new BinaryReader
(input, encoding
);
this.bigEnd = bigEndian
;
}
/* ===[ Properties ]================================================= */
public bool BigEndian
{
get
{ return bigEnd
; }
set
{ bigEnd
= value
; }
}
public bool LittleEndian
{
get
{ return !bigEnd
; }
set
{ bigEnd
= !value
; }
}
/* ===[ Methods ]==================================================== */
/* ===[ 8-bit ]============ */
public uint ReadByte
(out byte data
)
{
data
= reader
.ReadByte();
return sizeof(byte);
}
public uint ReadSByte
(out sbyte data
)
{
data
= reader
.ReadSByte();
return sizeof(sbyte);
}
public uint ReadBoolean
(out bool data
)
{
data
= reader
.ReadByte() != 0x00
;
return sizeof(bool);
}
/* ===[ 16-bit ]=========== */
public uint ReadInt16
(out short data
)
{
data
= BitConverter
.ToInt16(EndianBytes
(sizeof(short)),
0);
return sizeof(short);
}
public uint ReadUInt16
(out ushort data
)
{
data
= BitConverter
.ToUInt16(EndianBytes
(sizeof(ushort)),
0);
return sizeof(ushort);
}
public uint ReadChar
(out char data
)
{
data
= BitConverter
.ToChar(EndianBytes
(sizeof(char)),
0);
return sizeof(char);
}
/* ===[ 32-bit ]=========== */
public uint ReadInt32
(out int data
)
{
data
= BitConverter
.ToInt32(EndianBytes
(sizeof(int)),
0);
return sizeof(int);
}
public uint ReadUInt32
(out uint data
)
{
data
= BitConverter
.ToUInt32(EndianBytes
(sizeof(uint)),
0);
return sizeof(uint);
}
public uint ReadSingle
(out float data
)
{
data
= BitConverter
.ToSingle(EndianBytes
(sizeof(float)),
0);
return sizeof(float);
}
/* ===[ 64-bit ]=========== */
public uint ReadInt64
(out long data
)
{
data
= BitConverter
.ToInt64(EndianBytes
(sizeof(long)),
0);
return sizeof(long);
}
public uint ReadUInt64
(out ulong data
)
{
data
= BitConverter
.ToUInt64(EndianBytes
(sizeof(ulong)),
0);
return sizeof(ulong);
}
public uint ReadDouble
(out double data
)
{
data
= BitConverter
.ToDouble(EndianBytes
(sizeof(double)),
0);
return sizeof(double);
}
/* ===[ N-bit ]============ */
public uint ReadBytes
(int count,
out byte[] data
)
{
data
= reader
.ReadBytes(count
);
return (uint)count
;
}
public uint ReadChars
(int count,
out char[] data
)
{
uint sum
= 0;
data
= new char[count
];
for (uint i
= 0; i
< count
; i
++)
sum
+= ReadChar
(out data
[i
]);
return sum
;
}
public uint ReadString
(int count,
out string data
)
{
data
= encoding
.GetString(reader
.ReadBytes(count
),
0, count
);
return (uint)count
;
}
/* ======================== */
protected byte[] EndianBytes
(int count
)
{
return (BitConverter
.IsLittleEndian ^ bigEnd
)
? reader
.ReadBytes(count
)
: reader
.ReadBytes(count
).Reverse().ToArray();
}
public void Close
()
{
this.reader.Close();
}
/* ===[ EOF ]======================================================== */
}
}