using SabreTools.Numerics.Extensions;
namespace SabreTools.IO.Extensions
{
///
/// Extensions for ReadOnlyBitStream
///
public static class ReadOnlyBitStreamExtensions
{
///
/// Read a byte, if possible
///
/// The next byte, null on error or end of stream
/// Assumes the stream is byte-aligned
public static byte? ReadByte(this ReadOnlyBitStream stream)
{
try
{
stream.Discard();
return stream._source.ReadByteValue();
}
catch
{
return null;
}
}
///
/// Read a UInt16, if possible
///
/// The next UInt16, null on error or end of stream
/// Assumes the stream is byte-aligned
public static ushort? ReadUInt16(this ReadOnlyBitStream stream)
{
try
{
stream.Discard();
return stream._source.ReadUInt16();
}
catch
{
return null;
}
}
///
/// Read a UInt32, if possible
///
/// The next UInt32, null on error or end of stream
/// Assumes the stream is byte-aligned
public static uint? ReadUInt32(this ReadOnlyBitStream stream)
{
try
{
stream.Discard();
return stream._source.ReadUInt32();
}
catch
{
return null;
}
}
///
/// Read a UInt64, if possible
///
/// The next UInt64, null on error or end of stream
/// Assumes the stream is byte-aligned
public static ulong? ReadUInt64(this ReadOnlyBitStream stream)
{
try
{
stream.Discard();
return stream._source.ReadUInt64();
}
catch
{
return null;
}
}
///
/// Read bytes, if possible
///
/// Number of bytes to read
/// The next bytes, null on error or end of stream
/// Assumes the stream is byte-aligned
public static byte[]? ReadBytes(this ReadOnlyBitStream stream, int bytes)
{
try
{
stream.Discard();
return stream._source.ReadBytes(bytes);
}
catch
{
return null;
}
}
}
}