using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace GisSharpBlog.NetTopologySuite.IO
{
///
/// Extends the class to allow reading values in the BigEndian format.
///
///
/// While extends
/// adding methods for reading integer values ()
/// and double values () in the BigEndian format,
/// this implementation overrides methods, such
/// and and more,
/// for reading values in the BigEndian format.
///
public class BEBinaryReader : BinaryReader
{
///
/// Initializes a new instance of the class.
///
/// The stream.
public BEBinaryReader(Stream stream) : base(stream) { }
///
/// Initializes a new instance of the class.
///
/// The supplied stream.
/// The character encoding.
/// encoding is null.
/// The stream does not support reading, the stream is null, or the stream is already closed.
public BEBinaryReader(Stream input, Encoding encoding) : base(input, encoding) { }
///
/// Reads a 2-byte signed integer from the current stream using big endian encoding
/// and advances the current position of the stream by two bytes.
///
///
/// A 2-byte signed integer read from the current stream.
///
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
public override short ReadInt16()
{
byte[] byteArray = new byte[2];
int iBytesRead = Read(byteArray, 0, 2);
Debug.Assert(iBytesRead == 2);
Array.Reverse(byteArray);
return BitConverter.ToInt16(byteArray, 0);
}
///
/// Reads a 2-byte unsigned integer from the current stream using big endian encoding
/// and advances the position of the stream by two bytes.
///
///
/// A 2-byte unsigned integer read from this stream.
///
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
public override ushort ReadUInt16()
{
byte[] byteArray = new byte[2];
int iBytesRead = Read(byteArray, 0, 2);
Debug.Assert(iBytesRead == 2);
Array.Reverse(byteArray);
return BitConverter.ToUInt16(byteArray, 0);
}
///
/// Reads a 4-byte signed integer from the current stream using big endian encoding
/// and advances the current position of the stream by four bytes.
///
///
/// A 4-byte signed integer read from the current stream.
///
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
public override int ReadInt32()
{
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 4-byte unsigned integer from the current stream using big endian encoding
/// and advances the position of the stream by four bytes.
///
///
/// A 4-byte unsigned integer read from this stream.
///
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
public override uint ReadUInt32()
{
byte[] byteArray = new byte[4];
int iBytesRead = Read(byteArray, 0, 4);
Debug.Assert(iBytesRead == 4);
Array.Reverse(byteArray);
return BitConverter.ToUInt32(byteArray, 0);
}
///
/// Reads an 8-byte signed integer from the current stream using big endian encoding
/// and advances the current position of the stream by eight bytes.
///
///
/// An 8-byte signed integer read from the current stream.
///
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
public override long ReadInt64()
{
byte[] byteArray = new byte[8];
int iBytesRead = Read(byteArray, 0, 8);
Debug.Assert(iBytesRead == 8);
Array.Reverse(byteArray);
return BitConverter.ToInt64(byteArray, 0);
}
///
/// Reads an 8-byte unsigned integer from the current stream using big endian encoding
/// and advances the position of the stream by eight bytes.
///
///
/// An 8-byte unsigned integer read from this stream.
///
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
public override ulong ReadUInt64()
{
byte[] byteArray = new byte[8];
int iBytesRead = Read(byteArray, 0, 8);
Debug.Assert(iBytesRead == 8);
Array.Reverse(byteArray);
return BitConverter.ToUInt64(byteArray, 0);
}
///
/// Reads a 4-byte floating point value from the current stream using big endian encoding
/// and advances the current position of the stream by four bytes.
///
///
/// A 4-byte floating point value read from the current stream.
///
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
public override float ReadSingle()
{
byte[] byteArray = new byte[4];
int iBytesRead = Read(byteArray, 0, 4);
Debug.Assert(iBytesRead == 4);
Array.Reverse(byteArray);
return BitConverter.ToSingle(byteArray, 0);
}
///
/// Reads an 8-byte floating point value from the current stream using big endian encoding
/// and advances the current position of the stream by eight bytes.
///
///
/// An 8-byte floating point value read from the current stream.
///
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
public override double ReadDouble()
{
byte[] byteArray = new byte[8];
int iBytesRead = Read(byteArray, 0, 8);
Debug.Assert(iBytesRead == 8);
Array.Reverse(byteArray);
return BitConverter.ToDouble(byteArray, 0);
}
///
/// Reads a string from the current stream.
/// The string is prefixed with the length, encoded as an integer seven bits at a time.
///
/// The string being read.
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
[Obsolete("Not implemented")]
public override string ReadString()
{
throw new NotImplementedException();
}
///
/// Reads a decimal value from the current stream
/// and advances the current position of the stream by sixteen bytes.
///
///
/// A decimal value read from the current stream.
///
/// The stream is closed.
/// An I/O error occurs.
/// The end of the stream is reached.
[Obsolete("Not implemented")]
public override decimal ReadDecimal()
{
throw new NotImplementedException();
}
}
}