Skip to content

Commit

Permalink
Merge pull request #203 from SceneGate/feature/writeto-truncate
Browse files Browse the repository at this point in the history
馃悰  WriteTo truncates output file
  • Loading branch information
pleonex committed Nov 28, 2023
2 parents 49038fe + b8e3385 commit d3e4e14
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
6 changes: 5 additions & 1 deletion docs/articles/core/binary/datastream.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ internally maps to the .NET enumerations `FileMode` and `FileAccess` as follow:
> hundreds of `DataStream` on different files ready without running out of
> resources in the operative system.
<!-- ignore warning -->

> [!TIP]
> `FileOpenMode` is handy enumeration that covers most use cases. If you require
> any other combination of `FileMode` and `FileAccess` you can create the
Expand All @@ -119,7 +121,9 @@ applies to the content that targets this `DataStream`, not the entire parent

> [!NOTE]
> The method ignores the current position, it will always start writing from the
> start to the end. It will **restore** the current position after comparing.
> start to the end. It will **restore** the current position after writing. It
> will also start **truncate** the output file. It will not append or preserve
> any existing content.
The path should point to the output file. If there is any directory that doesn't
exist, it will create them first.
Expand Down
43 changes: 35 additions & 8 deletions src/Yarhl.UnitTests/IO/DataStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1368,17 +1368,44 @@ public void WriteToWhenLengthZeroCreatesEmptyFile()
{
string tempFile = Path.Combine(
Path.GetTempPath(),
Path.GetRandomFileName(),
Path.GetRandomFileName());

DataStream stream = new DataStream();
stream.WriteTo(tempFile);
try {
var stream = new DataStream();
stream.WriteTo(tempFile);

Assert.That(File.Exists(tempFile), Is.True);
FileStream fs = new FileStream(tempFile, FileMode.Open, FileAccess.Read);
Assert.AreEqual(0, fs.Length);
fs.Dispose();
File.Delete(tempFile);
Assert.That(File.Exists(tempFile), Is.True);

using var fs = new FileStream(tempFile, FileMode.Open, FileAccess.Read);
Assert.That(fs.Length, Is.EqualTo(0));
} finally {
File.Delete(tempFile);
}
}

[Test]
public void WriteToWhenTruncatesExistingFile()
{
string tempFile = Path.Combine(
Path.GetTempPath(),
Path.GetRandomFileName());

byte[] expected = new byte[] { 0xCA, 0xFE };
using var stream = new DataStream();
stream.Write(expected);

try {
// Pre-fill the output file with some long content
File.WriteAllText(tempFile, "hello world!");

// Overwrite with 2 bytes stream content
stream.WriteTo(tempFile);

byte[] actual = File.ReadAllBytes(tempFile);
Assert.That(actual, Is.EquivalentTo(expected));
} finally {
File.Delete(tempFile);
}
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion src/Yarhl/IO/DataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ public void WriteSegmentTo(long start, long length, string fileOut)
}

// We use FileStream so it creates a file even when the length is zero
using var segment = new FileStream(fileOut, FileMode.OpenOrCreate, FileAccess.Write);
using var segment = new FileStream(fileOut, FileMode.Create, FileAccess.Write);
WriteSegmentTo(start, length, segment);
}

Expand Down

0 comments on commit d3e4e14

Please sign in to comment.