mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-04-30 10:50:09 +00:00
174 lines
5.5 KiB
C#
174 lines
5.5 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using SabreTools.Numerics.Extensions;
|
|
|
|
namespace SabreTools.Text.Extensions
|
|
{
|
|
/// <summary>
|
|
/// Extensions for Streams
|
|
/// </summary>
|
|
public static class StreamWriterExtensions
|
|
{
|
|
#region Write Null Terminated
|
|
|
|
/// <summary>
|
|
/// Write a null-terminated string to the stream
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write a null-terminated ASCII string to the stream
|
|
/// </summary>
|
|
public static bool WriteNullTerminatedAnsiString(this Stream stream, string? value)
|
|
=> stream.WriteNullTerminatedString(value, Encoding.ASCII);
|
|
|
|
#if NET5_0_OR_GREATER
|
|
/// <summary>
|
|
/// Write a null-terminated Latin1 string to the stream
|
|
/// </summary>
|
|
public static bool WriteNullTerminatedLatin1String(this Stream stream, string? value)
|
|
=> stream.WriteNullTerminatedString(value, Encoding.Latin1);
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// Write a null-terminated UTF-8 string to the stream
|
|
/// </summary>
|
|
public static bool WriteNullTerminatedUTF8String(this Stream stream, string? value)
|
|
=> stream.WriteNullTerminatedString(value, Encoding.UTF8);
|
|
|
|
/// <summary>
|
|
/// Write a null-terminated UTF-16 (Unicode) string to the stream
|
|
/// </summary>
|
|
public static bool WriteNullTerminatedUnicodeString(this Stream stream, string? value)
|
|
=> stream.WriteNullTerminatedString(value, Encoding.Unicode);
|
|
|
|
/// <summary>
|
|
/// Write a null-terminated UTF-16 (Unicode) string to the stream
|
|
/// </summary>
|
|
public static bool WriteNullTerminatedBigEndianUnicodeString(this Stream stream, string? value)
|
|
=> stream.WriteNullTerminatedString(value, Encoding.BigEndianUnicode);
|
|
|
|
/// <summary>
|
|
/// Write a null-terminated UTF-32 string to the stream
|
|
/// </summary>
|
|
public static bool WriteNullTerminatedUTF32String(this Stream stream, string? value)
|
|
=> stream.WriteNullTerminatedString(value, Encoding.UTF32);
|
|
|
|
#endregion
|
|
|
|
#region Write Prefixed
|
|
|
|
//// <summary>
|
|
/// Write a byte-prefixed ASCII string to the stream
|
|
/// </summary>
|
|
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
|
|
//// <summary>
|
|
/// Write a byte-prefixed Latin1 string to the stream
|
|
/// </summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Write a ushort-prefixed Unicode string to the stream
|
|
/// </summary>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write a ushort-prefixed Unicode string to the stream
|
|
/// </summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Write an array of bytes to the stream
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
}
|