Skip to content

Commit

Permalink
Added IsBinary extension method for determining whether a given FileI…
Browse files Browse the repository at this point in the history
…nfo contains binary or text data
  • Loading branch information
NimaAra committed Aug 1, 2017
1 parent d67af04 commit 21b1709
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
108 changes: 108 additions & 0 deletions Easy.Common.Tests.Unit/FileAndDirectoryExtensions/FileIsBinaryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
namespace Easy.Common.Tests.Unit.FileAndDirectoryExtensions
{
using System.IO;
using Easy.Common.Extensions;
using NUnit.Framework;
using Shouldly;

[TestFixture]
internal sealed class FileIsBinaryTests
{
[Test]
public void When_checking_an_empty_file()
{
var tmpFile = new FileInfo(Path.GetTempFileName());
try
{
tmpFile.IsBinary().ShouldBeFalse();
} finally
{
tmpFile.Delete();
}
}

[Test]
public void When_checking_a_non_empty_text_file()
{
var tmpFile = new FileInfo(Path.GetTempFileName());
try
{
File.WriteAllText(tmpFile.FullName, "Foo");
tmpFile.IsBinary().ShouldBeFalse();
} finally
{
tmpFile.Delete();
}
}

[Test]
public void When_checking_a_text_file_with_tab_only()
{
var tmpFile = new FileInfo(Path.GetTempFileName());
try
{
File.WriteAllText(tmpFile.FullName, new string('\t', 1));
tmpFile.IsBinary().ShouldBeFalse();
} finally
{
tmpFile.Delete();
}
}

[Test]
public void When_checking_a_text_file_with_carriange_return_only()
{
var tmpFile = new FileInfo(Path.GetTempFileName());
try
{
File.WriteAllText(tmpFile.FullName, new string('\r', 1));
tmpFile.IsBinary().ShouldBeFalse();
} finally
{
tmpFile.Delete();
}
}

[Test]
public void When_checking_a_text_file_with_newline_only()
{
var tmpFile = new FileInfo(Path.GetTempFileName());
try
{
File.WriteAllText(tmpFile.FullName, new string('\n', 1));
tmpFile.IsBinary().ShouldBeFalse();
} finally
{
tmpFile.Delete();
}
}

[Test]
public void When_checking_an_empty_binary_file()
{
var tmpFile = new FileInfo(Path.GetTempFileName());
try
{
File.WriteAllBytes(tmpFile.FullName, new byte[0]);
tmpFile.IsBinary().ShouldBeFalse();
} finally
{
tmpFile.Delete();
}
}

[Test]
public void When_checking_a_non_empty_binary_file()
{
var tmpFile = new FileInfo(Path.GetTempFileName());
try
{
File.WriteAllBytes(tmpFile.FullName, new byte[] {1});
tmpFile.IsBinary().ShouldBeTrue();
} finally
{
tmpFile.Delete();
}
}
}
}
37 changes: 37 additions & 0 deletions Easy.Common/Extensions/FileAndDirectoryInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;

/// <summary>
/// Provides a set of useful methods for working with <see cref="FileInfo"/> and <see cref="DirectoryInfo"/>.
/// </summary>
public static class FileAndDirectoryInfoExtensions
{
private const char CarriageReturn = '\r';
private const char NewLine = '\n';
private const char Tab = '\t';

private static readonly ThreadLocal<char[]> CharacterBuffer = new ThreadLocal<char[]>(() => new char[256]);

/// <summary>
/// Returns the size in bytes of the <paramref name="directoryInfo"/> represented by the <paramref name="directoryInfo"/> instance.
/// </summary>
Expand Down Expand Up @@ -150,5 +157,35 @@ public static IEnumerable<FileInfo> EnumerateFilesSafe(this DirectoryInfo direct
return Enumerable.Empty<FileInfo>();
}
}

/// <summary>
/// Determines if the given <paramref name="file"/> is binary or a text file.
/// </summary>
[DebuggerStepThrough]
public static bool IsBinary(this FileSystemInfo file)
{
var buffer = CharacterBuffer.Value;

using (var fileStream = File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var reader = new StreamReader(fileStream))
{
var read = reader.ReadBlock(buffer, 0, buffer.Length);
return ContainsBinary(buffer, read);
}
}

// ReSharper disable once SuggestBaseTypeForParameter
private static bool ContainsBinary(char[] bytes, int count)
{
for (var i = 0; i < count; i++)
{
var c = bytes[i];
if (char.IsControl(c) && c != CarriageReturn && c != NewLine && c != Tab)
{
return true;
}
}
return false;
}
}
}

0 comments on commit 21b1709

Please # to comment.