using System.IO;
using System.Text;
using SabreTools.Numerics.Extensions;
namespace SabreTools.Text.Extensions
{
///
/// Extensions for Streams
///
public static class StreamWriterExtensions
{
#region Write Null Terminated
///
/// Write a null-terminated string to the stream
///
public static bool WriteNullTerminatedString(this Stream stream, string? value, Encoding encoding)
{
// If the value is null
if (value is null)
return false;
// Add the null terminator and write
value += "\0";
byte[] buffer = encoding.GetBytes(value);
return WriteFromBuffer(stream, buffer);
}
///
/// Write a null-terminated ASCII string to the stream
///
public static bool WriteNullTerminatedAnsiString(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.ASCII);
#if NET5_0_OR_GREATER
///
/// Write a null-terminated Latin1 string to the stream
///
public static bool WriteNullTerminatedLatin1String(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.Latin1);
#endif
///
/// Write a null-terminated UTF-8 string to the stream
///
public static bool WriteNullTerminatedUTF8String(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.UTF8);
///
/// Write a null-terminated UTF-16 (Unicode) string to the stream
///
public static bool WriteNullTerminatedUnicodeString(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.Unicode);
///
/// Write a null-terminated UTF-16 (Unicode) string to the stream
///
public static bool WriteNullTerminatedBigEndianUnicodeString(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.BigEndianUnicode);
///
/// Write a null-terminated UTF-32 string to the stream
///
public static bool WriteNullTerminatedUTF32String(this Stream stream, string? value)
=> stream.WriteNullTerminatedString(value, Encoding.UTF32);
#endregion
#region Write Prefixed
////
/// Write a byte-prefixed ASCII string to the stream
///
public static bool WritePrefixedAnsiString(this Stream stream, string? value)
{
// If the value is null
if (value is null)
return false;
// Get the buffer
byte[] buffer = Encoding.ASCII.GetBytes(value);
// Write the length as a byte
if (!stream.Write((byte)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(stream, buffer);
}
#if NET5_0_OR_GREATER
////
/// Write a byte-prefixed Latin1 string to the stream
///
public static bool WritePrefixedLatin1String(this Stream stream, string? value)
{
// If the value is null
if (value is null)
return false;
// Get the buffer
byte[] buffer = Encoding.Latin1.GetBytes(value);
// Write the length as a byte
if (!stream.Write((byte)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(stream, buffer);
}
#endif
///
/// Write a ushort-prefixed Unicode string to the stream
///
public static bool WritePrefixedUnicodeString(this Stream stream, string? value)
{
// If the value is null
if (value is null)
return false;
// Get the buffer
byte[] buffer = Encoding.Unicode.GetBytes(value);
// Write the length as a ushort
if (!stream.Write((ushort)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(stream, buffer);
}
///
/// Write a ushort-prefixed Unicode string to the stream
///
public static bool WritePrefixedBigEndianUnicodeString(this Stream stream, string? value)
{
// If the value is null
if (value is null)
return false;
// Get the buffer
byte[] buffer = Encoding.BigEndianUnicode.GetBytes(value);
// Write the length as a ushort
if (!stream.Write((ushort)value.Length))
return false;
// Write the buffer
return WriteFromBuffer(stream, buffer);
}
#endregion
///
/// Write an array of bytes to the stream
///
private static bool WriteFromBuffer(Stream stream, byte[] value)
{
// If the stream is not writable
if (!stream.CanWrite)
return false;
// Handle the 0-byte case
if (value.Length == 0)
return true;
// Handle the general case, forcing a write of the correct length
stream.Write(value, 0, value.Length);
return true;
}
}
}