From e23b1d5b12447ed462722467b70d4c01cfb8534f Mon Sep 17 00:00:00 2001 From: Jimmy Xie Date: Sun, 3 Mar 2019 11:28:15 +0800 Subject: [PATCH] add support for .NET Framework 3.5/4 --- Brotli.NET/Brotli.Core/Brotli.Core.csproj | 6 +-- .../Brotli.Core/Implement/BrotliStream.cs | 41 ++++++++++++++++++- .../Interop/NativeLibraryLoader.cs | 14 ++++++- Brotli.NET/Brotli.NET.sln | 2 +- .../FX45BroltliTest.cs} | 2 +- .../FX45TestBrotli.csproj} | 10 ++--- .../Properties/AssemblyInfo.cs | 0 .../packages.config | 2 +- Brotli.NET/TestBrotli/TestBrotli.csproj | 2 +- 9 files changed, 65 insertions(+), 14 deletions(-) rename Brotli.NET/{FXTestBrotli/FXBroltliTest.cs => FX45TestBrotli/FX45BroltliTest.cs} (97%) rename Brotli.NET/{FXTestBrotli/FXTestBrotli.csproj => FX45TestBrotli/FX45TestBrotli.csproj} (90%) rename Brotli.NET/{FXTestBrotli => FX45TestBrotli}/Properties/AssemblyInfo.cs (100%) rename Brotli.NET/{FXTestBrotli => FX45TestBrotli}/packages.config (82%) diff --git a/Brotli.NET/Brotli.Core/Brotli.Core.csproj b/Brotli.NET/Brotli.Core/Brotli.Core.csproj index 8749b71..9fe5714 100644 --- a/Brotli.NET/Brotli.Core/Brotli.Core.csproj +++ b/Brotli.NET/Brotli.Core/Brotli.Core.csproj @@ -1,14 +1,14 @@ - net45;netstandard2.0 - 2.0.3.2 + net35;net40;net45;netstandard2.0 + 2.0.4.0 true Brotli.NET Jinjun Xie Jinjun Xie Copyright Jinjun Xie 2016 - + https://mit-license.org/ https://github.com/XieJJ99/brotli.net Brotli Compress Decompress .NET Standard Stream Fix load issues on .NET Framework Projets. diff --git a/Brotli.NET/Brotli.Core/Implement/BrotliStream.cs b/Brotli.NET/Brotli.Core/Implement/BrotliStream.cs index d1f1b11..cb1e615 100644 --- a/Brotli.NET/Brotli.Core/Implement/BrotliStream.cs +++ b/Brotli.NET/Brotli.Core/Implement/BrotliStream.cs @@ -215,9 +215,10 @@ protected override void Dispose(bool disposing) } } + public void TruncateBeginning(MemoryStream ms, int numberOfBytesToRemove) { -#if NETCORE +#if NETSTANDARD2_0 ArraySegment buf; if(ms.TryGetBuffer(out buf)) { @@ -369,4 +370,42 @@ public override void Write(byte[] buffer, int offset, int count) } } } + +#if NET35 + /// + /// Improve compability issue on FX35 + /// + 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 } diff --git a/Brotli.NET/Brotli.Core/Interop/NativeLibraryLoader.cs b/Brotli.NET/Brotli.Core/Interop/NativeLibraryLoader.cs index bbb6cab..fc1b4ae 100644 --- a/Brotli.NET/Brotli.Core/Interop/NativeLibraryLoader.cs +++ b/Brotli.NET/Brotli.Core/Interop/NativeLibraryLoader.cs @@ -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."); @@ -111,7 +115,11 @@ public T GetNativeMethodDelegate(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) { @@ -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; } diff --git a/Brotli.NET/Brotli.NET.sln b/Brotli.NET/Brotli.NET.sln index 3c659d0..06ed289 100644 --- a/Brotli.NET/Brotli.NET.sln +++ b/Brotli.NET/Brotli.NET.sln @@ -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 diff --git a/Brotli.NET/FXTestBrotli/FXBroltliTest.cs b/Brotli.NET/FX45TestBrotli/FX45BroltliTest.cs similarity index 97% rename from Brotli.NET/FXTestBrotli/FXBroltliTest.cs rename to Brotli.NET/FX45TestBrotli/FX45BroltliTest.cs index 8a896b7..f4f9cd0 100644 --- a/Brotli.NET/FXTestBrotli/FXBroltliTest.cs +++ b/Brotli.NET/FX45TestBrotli/FX45BroltliTest.cs @@ -5,7 +5,7 @@ namespace FXTestBrotli { [TestClass] - public class FXBroltliTest + public class FX45BroltliTest { public Boolean ArrayEqual(Byte[] a1, Byte[] a2) { diff --git a/Brotli.NET/FXTestBrotli/FXTestBrotli.csproj b/Brotli.NET/FX45TestBrotli/FX45TestBrotli.csproj similarity index 90% rename from Brotli.NET/FXTestBrotli/FXTestBrotli.csproj rename to Brotli.NET/FX45TestBrotli/FX45TestBrotli.csproj index 5f4c41d..dfb9cdb 100644 --- a/Brotli.NET/FXTestBrotli/FXTestBrotli.csproj +++ b/Brotli.NET/FX45TestBrotli/FX45TestBrotli.csproj @@ -40,8 +40,8 @@ 4 - - ..\packages\Brotli.NET.2.0.3.1\lib\net45\Brotli.Core.dll + + ..\packages\Brotli.NET.2.0.4\lib\net45\Brotli.Core.dll ..\packages\MSTest.TestFramework.1.4.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll @@ -56,7 +56,7 @@ - + @@ -72,8 +72,8 @@ - + - + \ No newline at end of file diff --git a/Brotli.NET/FXTestBrotli/Properties/AssemblyInfo.cs b/Brotli.NET/FX45TestBrotli/Properties/AssemblyInfo.cs similarity index 100% rename from Brotli.NET/FXTestBrotli/Properties/AssemblyInfo.cs rename to Brotli.NET/FX45TestBrotli/Properties/AssemblyInfo.cs diff --git a/Brotli.NET/FXTestBrotli/packages.config b/Brotli.NET/FX45TestBrotli/packages.config similarity index 82% rename from Brotli.NET/FXTestBrotli/packages.config rename to Brotli.NET/FX45TestBrotli/packages.config index e74d57d..a64b262 100644 --- a/Brotli.NET/FXTestBrotli/packages.config +++ b/Brotli.NET/FX45TestBrotli/packages.config @@ -1,6 +1,6 @@  - + diff --git a/Brotli.NET/TestBrotli/TestBrotli.csproj b/Brotli.NET/TestBrotli/TestBrotli.csproj index 46ca7d6..e613219 100644 --- a/Brotli.NET/TestBrotli/TestBrotli.csproj +++ b/Brotli.NET/TestBrotli/TestBrotli.csproj @@ -17,7 +17,7 @@ - +