Skip to content

Commit

Permalink
Merge pull request #205 from SceneGate/feature/195-binary-file
Browse files Browse the repository at this point in the history
✨  Implement constructor to create BinaryFormat from a file
  • Loading branch information
pleonex committed Nov 28, 2023
2 parents 94024b1 + d087db6 commit dc2ee45
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/Yarhl.UnitTests/IO/BinaryFormatTests.cs
Expand Up @@ -176,6 +176,29 @@ public void MemoryConstructor()
format.Dispose();
}

[Test]
public void ConstructorFromFile()
{
string tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
File.WriteAllBytes(tempFile, new byte[] { 0xCA, 0xFE });

try {
using var format = new BinaryFormat(tempFile, FileOpenMode.ReadWrite);
Assert.That(format.Stream.BaseStream, Is.InstanceOf<LazyFileStream>());
Assert.That(format.Stream.Position, Is.EqualTo(0));
Assert.That(format.Stream.Length, Is.EqualTo(2));
Assert.That(format.Stream.ReadByte(), Is.EqualTo(0xCA));
} finally {
File.Delete(tempFile);
}
}

[Test]
public void ConstructorFromFileInvalidParams()
{
Assert.That(() => new BinaryFormat(null, FileOpenMode.Read), Throws.ArgumentNullException);
}

[Test]
public void Clone()
{
Expand Down
28 changes: 21 additions & 7 deletions src/Yarhl/IO/BinaryFormat.cs
Expand Up @@ -46,8 +46,7 @@ public BinaryFormat()
/// </param>
public BinaryFormat(Stream stream)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
ArgumentNullException.ThrowIfNull(stream);

Stream = stream as DataStream ?? DataStreamFactory.FromStream(stream);
}
Expand All @@ -67,16 +66,30 @@ public BinaryFormat(Stream stream)
/// <param name="length">Length of the substream.</param>
public BinaryFormat(Stream stream, long offset, long length)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
if (offset < 0 || offset > stream.Length)
ArgumentNullException.ThrowIfNull(stream);
if (offset < 0 || offset > stream.Length) {
throw new ArgumentOutOfRangeException(nameof(offset));
if (length < 0 || offset + length > stream.Length)
}

if (length < 0 || offset + length > stream.Length) {
throw new ArgumentOutOfRangeException(nameof(length));
}

Stream = DataStreamFactory.FromStream(stream, offset, length);
}

/// <summary>
/// Initializes a new instance of the <see cref="BinaryFormat"/> class.
/// </summary>
/// <param name="path">The path of the file.</param>
/// <param name="mode">The mode to open the file.</param>
public BinaryFormat(string path, FileOpenMode mode)
{
ArgumentNullException.ThrowIfNull(path);

Stream = DataStreamFactory.FromFile(path, mode);
}

/// <summary>
/// Gets the stream.
/// </summary>
Expand Down Expand Up @@ -126,8 +139,9 @@ public void Dispose()
/// </param>
protected virtual void Dispose(bool disposing)
{
if (Disposed)
if (Disposed) {
return;
}

Disposed = true;
if (disposing) {
Expand Down

0 comments on commit dc2ee45

Please sign in to comment.