Skip to content

Commit

Permalink
add support for .NET Framework 3.5/4
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmy Xie committed Mar 3, 2019
1 parent b14f8a1 commit e23b1d5
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 14 deletions.
6 changes: 3 additions & 3 deletions Brotli.NET/Brotli.Core/Brotli.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<Version>2.0.3.2</Version>
<TargetFrameworks>net35;net40;net45;netstandard2.0</TargetFrameworks>
<Version>2.0.4.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>Brotli.NET</PackageId>
<Authors>Jinjun Xie</Authors>
<Company>Jinjun Xie</Company>
<Copyright>Copyright Jinjun Xie 2016</Copyright>
<PackageLicenseUrl></PackageLicenseUrl>
<PackageLicenseUrl>https://mit-license.org/</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/XieJJ99/brotli.net</PackageProjectUrl>
<PackageTags>Brotli Compress Decompress .NET Standard Stream</PackageTags>
<PackageReleaseNotes>Fix load issues on .NET Framework Projets.
Expand Down
41 changes: 40 additions & 1 deletion Brotli.NET/Brotli.Core/Implement/BrotliStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ protected override void Dispose(bool disposing)
}
}


public void TruncateBeginning(MemoryStream ms, int numberOfBytesToRemove)
{
#if NETCORE
#if NETSTANDARD2_0
ArraySegment<byte> buf;
if(ms.TryGetBuffer(out buf))
{
Expand Down Expand Up @@ -369,4 +370,42 @@ public override void Write(byte[] buffer, int offset, int count)
}
}
}

#if NET35
/// <summary>
/// Improve compability issue on FX35
/// </summary>
public static class StreamCopyExtension
{
public static void CopyTo(this Stream source,Stream destination, int bufferSize=4*1024)
{
if (source==null)
{
throw new ArgumentNullException(nameof(source));
}
if (destination==null)
{
throw new ArgumentNullException(nameof(destination));
}
if (!source.CanRead)
{
throw new InvalidOperationException("source stream is not readable");
}
if (!destination.CanWrite)
{
throw new InvalidOperationException("destination stream is not writeable");
}
if (bufferSize<=0)
{
throw new InvalidOperationException("buffer size should be greate than zero");
}


byte[] buffer = new byte[bufferSize];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
destination.Write(buffer, 0, read);
}
}
#endif
}
14 changes: 13 additions & 1 deletion Brotli.NET/Brotli.Core/Interop/NativeLibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ internal class NativeLibraryLoader
internal static bool Is64Bit = false;
static NativeLibraryLoader()
{
#if NET35 || NET40
IsWindows=true;
#else
IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
IsLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
IsMacOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
IsNetCore= RuntimeInformation.FrameworkDescription.StartsWith(".NET Core");
IsNetCore = RuntimeInformation.FrameworkDescription.StartsWith(".NET Core");
#endif
if (!IsWindows && !IsLinux && !IsMacOSX)
{
throw new InvalidOperationException("Unsupported platform.");
Expand Down Expand Up @@ -111,7 +115,11 @@ public T GetNativeMethodDelegate<T>(string methodName) where T : class

internal static string[] GetPossibleRuntimeDirectories()
{
#if NET35 || NET40
var assemblyDirectory = Path.GetDirectoryName(typeof(LibPathBootStrapper).Assembly.Location);
#else
var assemblyDirectory = Path.GetDirectoryName(typeof(LibPathBootStrapper).GetTypeInfo().Assembly.Location);
#endif
var platform = "win";
if (IsLinux)
{
Expand All @@ -123,7 +131,11 @@ internal static string[] GetPossibleRuntimeDirectories()
}
string runtimesDirectory = string.Format("runtimes/{0}/native", platform).Replace('/',Path.DirectorySeparatorChar);
string runtimesFullDirectory = Path.Combine(assemblyDirectory,runtimesDirectory).Replace('/', Path.DirectorySeparatorChar);
#if NET35
var netCoreAppStyleDirectory = Path.Combine(Path.Combine(assemblyDirectory, "../.."), runtimesDirectory).Replace('/', Path.DirectorySeparatorChar);
#else
var netCoreAppStyleDirectory = Path.Combine(assemblyDirectory, "../..", runtimesDirectory).Replace('/', Path.DirectorySeparatorChar);
#endif
string[] paths = new[] { assemblyDirectory, runtimesFullDirectory, runtimesDirectory, netCoreAppStyleDirectory };
return paths;
}
Expand Down
2 changes: 1 addition & 1 deletion Brotli.NET/Brotli.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Brotli.Core", "Brotli.Core\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestBrotli", "TestBrotli\TestBrotli.csproj", "{1BB4F249-0ED5-49E8-97EC-4A5FF495B296}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FXTestBrotli", "FXTestBrotli\FXTestBrotli.csproj", "{4AF58FA9-B499-424B-BFF7-B8DDDF6481B7}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FX45TestBrotli", "FX45TestBrotli\FX45TestBrotli.csproj", "{4AF58FA9-B499-424B-BFF7-B8DDDF6481B7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace FXTestBrotli
{
[TestClass]
public class FXBroltliTest
public class FX45BroltliTest
{
public Boolean ArrayEqual(Byte[] a1, Byte[] a2)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Brotli.Core, Version=2.0.3.1, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Brotli.NET.2.0.3.1\lib\net45\Brotli.Core.dll</HintPath>
<Reference Include="Brotli.Core, Version=2.0.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Brotli.NET.2.0.4\lib\net45\Brotli.Core.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.4.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
Expand All @@ -56,7 +56,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="FXBroltliTest.cs" />
<Compile Include="FX45BroltliTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -72,8 +72,8 @@
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets'))" />
<Error Condition="!Exists('..\packages\Brotli.NET.2.0.3.1\build\Brotli.NET.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Brotli.NET.2.0.3.1\build\Brotli.NET.targets'))" />
<Error Condition="!Exists('..\packages\Brotli.NET.2.0.4\build\Brotli.NET.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Brotli.NET.2.0.4\build\Brotli.NET.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.4.0\build\net45\MSTest.TestAdapter.targets')" />
<Import Project="..\packages\Brotli.NET.2.0.3.1\build\Brotli.NET.targets" Condition="Exists('..\packages\Brotli.NET.2.0.3.1\build\Brotli.NET.targets')" />
<Import Project="..\packages\Brotli.NET.2.0.4\build\Brotli.NET.targets" Condition="Exists('..\packages\Brotli.NET.2.0.4\build\Brotli.NET.targets')" />
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Brotli.NET" version="2.0.3.1" targetFramework="net45" />
<package id="Brotli.NET" version="2.0.4" targetFramework="net45" />
<package id="MSTest.TestAdapter" version="1.4.0" targetFramework="net471" />
<package id="MSTest.TestFramework" version="1.4.0" targetFramework="net471" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net45" />
Expand Down
2 changes: 1 addition & 1 deletion Brotli.NET/TestBrotli/TestBrotli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Brotli.NET" Version="2.0.3.1" />
<PackageReference Include="Brotli.NET" Version="2.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
Expand Down

0 comments on commit e23b1d5

Please sign in to comment.