using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace GisSharpBlog.NetTopologySuite.IO
{
///
/// Extends the class to allow reading of integers and doubles
/// in the Big Endian format.
///
///
/// The BinaryReader uses Little Endian format when reading binary streams.
///
public class BigEndianBinaryReader : BinaryReader
{
///
/// Initializes a new instance of the BigEndianBinaryReader class
/// based on the supplied stream and using UTF8Encoding.
///
///
public BigEndianBinaryReader(Stream stream) : base(stream) {}
///
/// Initializes a new instance of the BigEndianBinaryReader class
/// based on the supplied stream and a specific character encoding.
///
///
///
public BigEndianBinaryReader(Stream input, Encoding encoding) : base(input, encoding) {}
///
/// Reads a 4-byte signed integer using the big-endian layout
/// from the current stream and advances the current position of the stream by four bytes.
///
///
public int ReadInt32BE()
{
// big endian
byte[] byteArray = new byte[4];
int iBytesRead = Read(byteArray, 0, 4);
Debug.Assert(iBytesRead == 4);
Array.Reverse(byteArray);
return BitConverter.ToInt32(byteArray, 0);
}
///
/// Reads a 8-byte signed double using the big-endian layout
/// from the current stream and advances the current position of the stream by eight bytes.
///
///
public double ReadDoubleBE()
{
// big endian
byte[] byteArray = new byte[8];
int iBytesRead = Read(byteArray, 0, 8);
Debug.Assert(iBytesRead == 8);
Array.Reverse(byteArray);
return BitConverter.ToDouble(byteArray, 0);
}
}
}