Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class ByteArrayToFileConverter
{
public static void SaveByteArrayToFileWithBinaryWriter(byte[] data, string filePath)
{
using var writer = new BinaryWriter(File.OpenWrite(filePath));
using var writer = new BinaryWriter(File.Create(filePath));
writer.Write(data);
}
public static void SaveByteArrayToFileWithFileStream(byte[] data, string filePath)
Expand All @@ -15,4 +15,16 @@ public static void SaveByteArrayToFileWithFileStream(byte[] data, string filePat

public static void SaveByteArrayToFileWithStaticMethod(byte[] data, string filePath)
=> File.WriteAllBytes(filePath, data);

public static Task SaveByteArrayToFileAsync(byte[] data, string filePath, CancellationToken token = default)
=> File.WriteAllBytesAsync(filePath, data, token);

public static void AppendByteArrayToFile(byte[] data, string filePath)
=> File.AppendAllBytes(filePath, data);

public static void SaveByteArrayToFileAtOffset(byte[] data, string filePath, long offset)
{
using var handle = File.OpenHandle(filePath, FileMode.OpenOrCreate, FileAccess.Write);
RandomAccess.Write(handle, data, offset);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.1" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.1" />
<PackageReference Include="coverlet.collector" Version="10.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using ByteArrayToFile;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace ByteArrayToFileTest
{
[TestClass]
public class ByteArrayToFileUnitTest
{
const string Data = "Byte Array To File Test";
readonly byte[] _dataInBytes = Encoding.UTF8.GetBytes(Data);
readonly string _filePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

[TestCleanup]
public void DeleteFile() => File.Delete(_filePath);

[TestMethod]
public void GivenBinaryWriter_WhenSavingByteArray_ThenLoadCorrectData()
{
ByteArrayToFileConverter.SaveByteArrayToFileWithBinaryWriter(_dataInBytes, _filePath);

Assert.AreEqual(Data, File.ReadAllText(_filePath));
}

[TestMethod]
public void GivenBinaryWriter_WhenSavingShorterArrayOverLongerFile_ThenFileIsTruncated()
{
var longer = Encoding.UTF8.GetBytes("This is a test of the OpenWrite method");
var shorter = Encoding.UTF8.GetBytes("Second run");

ByteArrayToFileConverter.SaveByteArrayToFileWithBinaryWriter(longer, _filePath);
ByteArrayToFileConverter.SaveByteArrayToFileWithBinaryWriter(shorter, _filePath);

Assert.AreEqual(shorter.Length, new FileInfo(_filePath).Length);
Assert.AreEqual("Second run", File.ReadAllText(_filePath));
}

[TestMethod]
public void GivenFileStream_WhenSavingByteArray_ThenLoadCorrectData()
{
ByteArrayToFileConverter.SaveByteArrayToFileWithFileStream(_dataInBytes, _filePath);

Assert.AreEqual(Data, File.ReadAllText(_filePath));
}

[TestMethod]
public void GivenStaticFileMethod_WhenSavingByteArray_ThenLoadCorrectData()
{
ByteArrayToFileConverter.SaveByteArrayToFileWithStaticMethod(_dataInBytes, _filePath);

Assert.AreEqual(Data, File.ReadAllText(_filePath));
}

[TestMethod]
public async Task GivenWriteAllBytesAsync_WhenSavingByteArray_ThenLoadCorrectData()
{
await ByteArrayToFileConverter.SaveByteArrayToFileAsync(_dataInBytes, _filePath);

Assert.AreEqual(Data, File.ReadAllText(_filePath));
}

[TestMethod]
public void GivenAppendAllBytes_WhenAppendingByteArray_ThenBytesAreAddedAtTheEnd()
{
ByteArrayToFileConverter.SaveByteArrayToFileWithStaticMethod([1, 2, 3], _filePath);
ByteArrayToFileConverter.AppendByteArrayToFile([4, 5], _filePath);

CollectionAssert.AreEqual(new byte[] { 1, 2, 3, 4, 5 }, File.ReadAllBytes(_filePath));
}

[TestMethod]
public void GivenRandomAccess_WhenWritingAtOffset_ThenBytesAreReplacedInPlace()
{
File.WriteAllText(_filePath, "AAAAAAAAAA");

ByteArrayToFileConverter.SaveByteArrayToFileAtOffset(Encoding.UTF8.GetBytes("BB"), _filePath, 4);

Assert.AreEqual("AAAABBAAAA", File.ReadAllText(_filePath));
}
}
}

This file was deleted.

Loading