3 Commits
1.6.0 ... 1.6.1

Author SHA1 Message Date
Matt Nadareski
3f146d45a8 Bump version 2024-11-29 19:54:37 -05:00
Matt Nadareski
f3689087e6 Add alignment tests 2024-11-28 21:31:38 -05:00
Matt Nadareski
d2d191d86f Add boundary alignment stream extension 2024-11-28 21:17:33 -05:00
3 changed files with 86 additions and 1 deletions

View File

@@ -7,6 +7,63 @@ namespace SabreTools.IO.Test.Extensions
{
public class StreamExtensionsTests
{
#region Align to Boundary
[Fact]
public void AlignToBoundary_Null_False()
{
Stream? stream = null;
byte alignment = 4;
bool actual = stream.AlignToBoundary(alignment);
Assert.False(actual);
}
[Fact]
public void AlignToBoundary_Empty_False()
{
Stream? stream = new MemoryStream([]);
byte alignment = 4;
bool actual = stream.AlignToBoundary(alignment);
Assert.False(actual);
}
[Fact]
public void AlignToBoundary_EOF_False()
{
Stream? stream = new MemoryStream([0x01, 0x02]);
byte alignment = 4;
stream.Position = 1;
bool actual = stream.AlignToBoundary(alignment);
Assert.False(actual);
}
[Fact]
public void AlignToBoundary_TooShort_False()
{
Stream? stream = new MemoryStream([0x01, 0x02]);
byte alignment = 4;
stream.Position = 1;
bool actual = stream.AlignToBoundary(alignment);
Assert.False(actual);
}
[Fact]
public void AlignToBoundary_CanAlign_True()
{
Stream? stream = new MemoryStream([0x01, 0x02, 0x03, 0x04, 0x05]);
byte alignment = 4;
stream.Position = 1;
bool actual = stream.AlignToBoundary(alignment);
Assert.True(actual);
}
#endregion
#region Seek If Possible
[Fact]
public void SeekIfPossible_NonSeekable_CurrentPosition()
{
@@ -47,6 +104,8 @@ namespace SabreTools.IO.Test.Extensions
Assert.Equal(13, actual);
}
#endregion
/// <summary>
/// Represents a hidden non-seekable stream
/// </summary>

View File

@@ -4,6 +4,32 @@ namespace SabreTools.IO.Extensions
{
public static class StreamExtensions
{
/// <summary>
/// Align the stream position to a byte-size boundary
/// </summary>
/// <param name="input">Input stream to try aligning</param>
/// <param name="alignment">Number of bytes to align on</param>
/// <returns>True if the stream could be aligned, false otherwise</returns>
public static bool AlignToBoundary(this Stream? input, byte alignment)
{
// If the stream is invalid
if (input == null || input.Length == 0 || !input.CanRead)
return false;
// If already at the end of the stream
if (input.Position >= input.Length)
return false;
// Align the stream position
while (input.Position % alignment != 0 && input.Position < input.Length)
{
_ = input.ReadByteValue();
}
// Return if the alignment completed
return input.Position % alignment == 0;
}
/// <summary>
/// Seek to a specific point in the stream, if possible
/// </summary>

View File

@@ -7,7 +7,7 @@
<NoWarn>CS0618</NoWarn>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>1.6.0</Version>
<Version>1.6.1</Version>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>