using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace GisSharpBlog.NetTopologySuite.IO
{
///
/// Extends the class to allow writing values in the BigEndian format.
///
///
/// While extends
/// adding methods for writing integer values ()
/// and double values () in the BigEndian format,
/// this implementation overrides methods, such
/// and and more,
/// for writing values in the BigEndian format.
///
public class BEBinaryWriter : BinaryWriter
{
///
/// Initializes a new instance of the class.
///
/// The supplied stream.
/// output is null.
///
/// The stream does not support writing, or the stream is already closed.
public BEBinaryWriter(Stream output) : base(output) {}
///
/// Initializes a new instance of the class.
///
/// The supplied stream.
/// The character encoding.
/// output or encoding is null.
///
/// The stream does not support writing, or the stream is already closed.
public BEBinaryWriter(Stream output, Encoding encoding) : base(output, encoding) {}
///
/// Writes a two-byte signed integer to the current stream using BigEndian encoding
/// and advances the stream position by two bytes.
///
/// The two-byte signed integer to write.
/// The stream is closed.
/// An I/O error occurs.
public override void Write(short value)
{
byte[] bytes = BitConverter.GetBytes(value);
Debug.Assert(bytes.Length == 2);
Array.Reverse(bytes, 0, 2);
Write(bytes);
}
///
/// Writes a two-byte unsigned integer to the current stream using BigEndian encoding
/// and advances the stream position by two bytes.
///
/// The two-byte unsigned integer to write.
/// The stream is closed.
/// An I/O error occurs.
public override void Write(ushort value)
{
byte[] bytes = BitConverter.GetBytes(value);
Debug.Assert(bytes.Length == 2);
Array.Reverse(bytes, 0, 2);
Write(bytes);
}
///
/// Writes a four-byte signed integer to the current stream using BigEndian encoding
/// and advances the stream position by four bytes.
///
/// The four-byte signed integer to write.
/// The stream is closed.
/// An I/O error occurs.
public override void Write(int value)
{
byte[] bytes = BitConverter.GetBytes(value);
Debug.Assert(bytes.Length == 4);
Array.Reverse(bytes, 0, 4);
Write(bytes);
}
///
/// Writes a four-byte unsigned integer to the current stream using BigEndian encoding
/// and advances the stream position by four bytes.
///
/// The four-byte unsigned integer to write.
/// The stream is closed.
/// An I/O error occurs.
public override void Write(uint value)
{
byte[] bytes = BitConverter.GetBytes(value);
Debug.Assert(bytes.Length == 4);
Array.Reverse(bytes, 0, 4);
Write(bytes);
}
///
/// Writes an eight-byte signed integer to the current stream using BigEndian encoding
/// and advances the stream position by eight bytes.
///
/// The eight-byte signed integer to write.
/// The stream is closed.
/// An I/O error occurs.
public override void Write(long value)
{
byte[] bytes = BitConverter.GetBytes(value);
Debug.Assert(bytes.Length == 8);
Array.Reverse(bytes, 0, 8);
Write(bytes);
}
///
/// Writes an eight-byte unsigned integer to the current stream using BigEndian encoding
/// and advances the stream position by eight bytes.
///
/// The eight-byte unsigned integer to write.
/// The stream is closed.
/// An I/O error occurs.
public override void Write(ulong value)
{
byte[] bytes = BitConverter.GetBytes(value);
Debug.Assert(bytes.Length == 8);
Array.Reverse(bytes, 0, 8);
Write(bytes);
}
///
/// Writes a four-byte floating-point value to the current stream using BigEndian encoding
/// and advances the stream position by four bytes.
///
/// The four-byte floating-point value to write.
/// The stream is closed.
/// An I/O error occurs.
public override void Write(float value)
{
byte[] bytes = BitConverter.GetBytes(value);
Debug.Assert(bytes.Length == 4);
Array.Reverse(bytes, 0, 4);
Write(bytes);
}
///
/// Writes an eight-byte floating-point value to the current stream using BigEndian encoding
/// and advances the stream position by eight bytes.
///
/// The eight-byte floating-point value to write.
/// The stream is closed.
/// An I/O error occurs.
public override void Write(double value)
{
byte[] bytes = BitConverter.GetBytes(value);
Debug.Assert(bytes.Length == 8);
Array.Reverse(bytes, 0, 8);
Write(bytes);
}
///
/// Writes a length-prefixed string to this stream in the current encoding
/// of the ,
/// and advances the current position of the stream in accordance
/// with the encoding used and the specific characters being written to the stream.
///
/// The value to write.
/// The stream is closed.
/// An I/O error occurs.
/// value is null.
[Obsolete("Not implemented")]
public override void Write(string value)
{
throw new NotImplementedException();
}
///
/// Writes a decimal value to the current stream and advances the stream position by sixteen bytes.
///
/// The decimal value to write.
/// The stream is closed.
/// An I/O error occurs.
[Obsolete("Not implemented")]
public override void Write(decimal value)
{
throw new NotImplementedException();
}
}
}