mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-02-14 13:46:12 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f146d45a8 | ||
|
|
f3689087e6 | ||
|
|
d2d191d86f |
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user