Skip to content

Commit

Permalink
Complete Zaabee.Snappy
Browse files Browse the repository at this point in the history
  • Loading branch information
Mutuduxf committed Sep 1, 2023
1 parent 2679a17 commit 548a820
Show file tree
Hide file tree
Showing 24 changed files with 423 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Zaabee.Compression.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zaabee.XZ.UnitTest", "tests
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zaabee.Snappy", "src\Zaabee.Snappy\Zaabee.Snappy.csproj", "{829CE0E8-6536-4ECF-84F2-20D155A49055}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Zaabee.Snappy.UnitTest", "tests\Zaabee.Snappy.UnitTest\Zaabee.Snappy.UnitTest.csproj", "{73E05D79-6F20-4281-B6D2-41049402CBC5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -63,6 +65,7 @@ Global
{D9663D19-647A-4FC9-A109-D09875640929} = {5BE4F706-456E-4DF8-A606-69F992835B47}
{92B03B14-C246-41D3-A23F-A7F2C1A96C15} = {5BE4F706-456E-4DF8-A606-69F992835B47}
{829CE0E8-6536-4ECF-84F2-20D155A49055} = {158FA242-DD0C-4482-BD54-4E7EDA9C44CA}
{73E05D79-6F20-4281-B6D2-41049402CBC5} = {5BE4F706-456E-4DF8-A606-69F992835B47}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F9369C2B-0732-46F2-B95A-8F109BFE59A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -133,5 +136,9 @@ Global
{829CE0E8-6536-4ECF-84F2-20D155A49055}.Debug|Any CPU.Build.0 = Debug|Any CPU
{829CE0E8-6536-4ECF-84F2-20D155A49055}.Release|Any CPU.ActiveCfg = Release|Any CPU
{829CE0E8-6536-4ECF-84F2-20D155A49055}.Release|Any CPU.Build.0 = Release|Any CPU
{73E05D79-6F20-4281-B6D2-41049402CBC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73E05D79-6F20-4281-B6D2-41049402CBC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73E05D79-6F20-4281-B6D2-41049402CBC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73E05D79-6F20-4281-B6D2-41049402CBC5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
5 changes: 0 additions & 5 deletions src/Zaabee.Snappy/Class1.cs

This file was deleted.

5 changes: 5 additions & 0 deletions src/Zaabee.Snappy/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Global using directives

global using System.Text;
global using Zaabee.Compressor.Abstractions;
global using Zaabee.Extensions;
12 changes: 12 additions & 0 deletions src/Zaabee.Snappy/Snappy.Extensions.Bytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Zaabee.Snappy;

public static partial class SnappyExtensions
{
public static byte[] ToSnappy(
this byte[] rawBytes) =>
SnappyHelper.Compress(rawBytes);

public static byte[] UnSnappy(
this byte[] compressedBytes) =>
SnappyHelper.Decompress(compressedBytes);
}
26 changes: 26 additions & 0 deletions src/Zaabee.Snappy/Snappy.Extensions.Stream.Async.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Zaabee.Snappy;

public static partial class SnappyExtensions
{
public static async Task ToSnappyAsync(
this Stream rawStream,
Stream outputStream,
CancellationToken cancellationToken = default) =>
await SnappyHelper.CompressAsync(rawStream, outputStream, cancellationToken);

public static async Task UnSnappyAsync(
this Stream compressedStream,
Stream outputStream,
CancellationToken cancellationToken = default) =>
await SnappyHelper.DecompressAsync(compressedStream, outputStream, cancellationToken);

public static async Task<MemoryStream> ToSnappyAsync(
this Stream rawStream,
CancellationToken cancellationToken = default) =>
await SnappyHelper.CompressAsync(rawStream, cancellationToken);

public static async Task<MemoryStream> UnSnappyAsync(
this Stream compressedStream,
CancellationToken cancellationToken = default) =>
await SnappyHelper.DecompressAsync(compressedStream, cancellationToken);
}
22 changes: 22 additions & 0 deletions src/Zaabee.Snappy/Snappy.Extensions.Stream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Zaabee.Snappy;

public static partial class SnappyExtensions
{
public static void ToSnappy(
this Stream rawStream,
Stream outputStream) =>
SnappyHelper.Compress(rawStream, outputStream);

public static void UnSnappy(
this Stream compressedStream,
Stream outputStream) =>
SnappyHelper.Decompress(compressedStream, outputStream);

public static MemoryStream ToSnappy(
this Stream rawStream) =>
SnappyHelper.Compress(rawStream);

public static MemoryStream UnSnappy(
this Stream compressedStream) =>
SnappyHelper.Decompress(compressedStream);
}
14 changes: 14 additions & 0 deletions src/Zaabee.Snappy/Snappy.Extensions.String.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Zaabee.Snappy;

public static partial class SnappyExtensions
{
public static byte[] ToSnappy(
this string str,
Encoding? encoding = null) =>
SnappyHelper.Compress(str, encoding);

public static string UnSnappyToString(
this byte[] compressedBytes,
Encoding? encoding = null) =>
SnappyHelper.DecompressToString(compressedBytes, encoding);
}
12 changes: 12 additions & 0 deletions src/Zaabee.Snappy/Snappy.Helper.Bytes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Zaabee.Snappy;

public static partial class SnappyHelper
{
public static byte[] Compress(
byte[] rawBytes) =>
IronSnappy.Snappy.Encode(rawBytes);

public static byte[] Decompress(
byte[] compressedBytes) =>
IronSnappy.Snappy.Decode(compressedBytes);
}
56 changes: 56 additions & 0 deletions src/Zaabee.Snappy/Snappy.Helper.Stream.Async.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace Zaabee.Snappy;

public static partial class SnappyHelper
{
public static async Task<MemoryStream> CompressAsync(
Stream inputStream,
CancellationToken cancellationToken = default)
{
var rawBytes = await inputStream.ReadToEndAsync(cancellationToken);
var compressedBytes = IronSnappy.Snappy.Encode(rawBytes);
inputStream.TrySeek(0, SeekOrigin.Begin);
return new MemoryStream(compressedBytes);
}

public static async Task<MemoryStream> DecompressAsync(
Stream inputStream,
CancellationToken cancellationToken = default)
{
var compressedBytes = await inputStream.ReadToEndAsync(cancellationToken);
var rawBytes = IronSnappy.Snappy.Decode(compressedBytes);
inputStream.TrySeek(0, SeekOrigin.Begin);
return new MemoryStream(rawBytes);
}

public static async Task CompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default)
{
var rawBytes = await inputStream.ReadToEndAsync(cancellationToken);
var compressedBytes = IronSnappy.Snappy.Encode(rawBytes);
#if NETSTANDARD2_0
await outputStream.WriteAsync(compressedBytes, 0, compressedBytes.Length, cancellationToken);
#else
await outputStream.WriteAsync(compressedBytes, cancellationToken);
#endif
inputStream.TrySeek(0, SeekOrigin.Begin);
outputStream.TrySeek(0, SeekOrigin.Begin);
}

public static async Task DecompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default)
{
var compressedBytes = await inputStream.ReadToEndAsync(cancellationToken);
var rawBytes = IronSnappy.Snappy.Decode(compressedBytes);
#if NETSTANDARD2_0
await outputStream.WriteAsync(rawBytes, 0, rawBytes.Length, cancellationToken);
#else
await outputStream.WriteAsync(rawBytes, cancellationToken);
#endif
inputStream.TrySeek(0, SeekOrigin.Begin);
outputStream.TrySeek(0, SeekOrigin.Begin);
}
}
44 changes: 44 additions & 0 deletions src/Zaabee.Snappy/Snappy.Helper.Stream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace Zaabee.Snappy;

public static partial class SnappyHelper
{
public static MemoryStream Compress(
Stream inputStream)
{
var rawBytes = inputStream.ReadToEnd();
var compressedBytes = IronSnappy.Snappy.Encode(rawBytes);
inputStream.TrySeek(0, SeekOrigin.Begin);
return new MemoryStream(compressedBytes);
}

public static MemoryStream Decompress(
Stream inputStream)
{
var compressedBytes = inputStream.ReadToEnd();
var rawBytes = IronSnappy.Snappy.Decode(compressedBytes);
inputStream.TrySeek(0, SeekOrigin.Begin);
return new MemoryStream(rawBytes);
}

public static void Compress(
Stream inputStream,
Stream outputStream)
{
var rawBytes = inputStream.ReadToEnd();
var compressedBytes = IronSnappy.Snappy.Encode(rawBytes);
outputStream.Write(compressedBytes, 0, compressedBytes.Length);
inputStream.TrySeek(0, SeekOrigin.Begin);
outputStream.TrySeek(0, SeekOrigin.Begin);
}

public static void Decompress(
Stream inputStream,
Stream outputStream)
{
var compressedBytes = inputStream.ReadToEnd();
var rawBytes = IronSnappy.Snappy.Decode(compressedBytes);
outputStream.Write(rawBytes, 0, rawBytes.Length);
inputStream.TrySeek(0, SeekOrigin.Begin);
outputStream.TrySeek(0, SeekOrigin.Begin);
}
}
15 changes: 15 additions & 0 deletions src/Zaabee.Snappy/Snappy.Helper.String.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Zaabee.Snappy;

public static partial class SnappyHelper
{
public static byte[] Compress(
string str,
Encoding? encoding = null) =>
Compress(str.GetBytes(encoding ?? Consts.DefaultEncoding));

public static string DecompressToString(
byte[] compressedBytes,
Encoding? encoding = null) =>
Decompress(compressedBytes)
.GetString(encoding ?? Consts.DefaultEncoding);
}
48 changes: 48 additions & 0 deletions src/Zaabee.Snappy/SnappyCompressor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace Zaabee.Snappy;

public class SnappyCompressor : ICompressor
{
public async Task<MemoryStream> CompressAsync(
Stream rawStream,
CancellationToken cancellationToken = default) =>
await rawStream.ToSnappyAsync(cancellationToken);

public async Task<MemoryStream> DecompressAsync(
Stream compressedStream,
CancellationToken cancellationToken = default) =>
await compressedStream.UnSnappyAsync(cancellationToken);

public async Task CompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default) =>
await inputStream.ToSnappyAsync(outputStream, cancellationToken);

public async Task DecompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default) =>
await inputStream.UnSnappyAsync(outputStream, cancellationToken);

public byte[] Compress(byte[] rawBytes) =>
rawBytes.ToSnappy();

public byte[] Decompress(byte[] compressedBytes) =>
compressedBytes.UnSnappy();

public MemoryStream Compress(Stream rawStream) =>
rawStream.ToSnappy();

public MemoryStream Decompress(Stream compressedStream) =>
compressedStream.UnSnappy();

public void Compress(
Stream inputStream,
Stream outputStream) =>
inputStream.ToSnappy(outputStream);

public void Decompress(
Stream inputStream,
Stream outputStream) =>
inputStream.UnSnappy(outputStream);
}
15 changes: 13 additions & 2 deletions src/Zaabee.Snappy/Zaabee.Snappy.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2023.8.0</PackageVersion>
<Version>2023.8.0</Version>
<Authors>Mutuduxf</Authors>
<Company>Mutuduxf</Company>
<PackageTags>Zaabee;IronSnappy;Snappy;Compression</PackageTags>
<Description>Helper and Extensions for IronSnappy.</Description>
<PackageProjectUrl>https://github.com/PicoHex/Zaabee.Compression</PackageProjectUrl>
<LicenseExpression>MIT</LicenseExpression>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="IronSnappy" Version="1.3.1" />
<PackageReference Include="IronSnappy" Version="1.3.1" />
<PackageReference Include="Zaabee.Compressor.Abstractions" Version="2023.12.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void ZstdCompressAndDecompressString() =>
public void XzCompressAndDecompressString() =>
CompressAndDecompressString(new XzCompressor());

[Fact]
public void SnappyCompressAndDecompressString() =>
CompressAndDecompressString(new SnappyCompressor());

private void CompressAndDecompressString(ICompressor compressor)
{
const string str = "Hello World!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void ZstdCompressToBytesAndDecompressToBytesTest() =>
public void XzCompressToBytesAndDecompressToBytesTest() =>
CompressToBytesAndDecompressToBytesTest(new XzCompressor());

[Fact]
public void SnappyCompressToBytesAndDecompressToBytesTest() =>
CompressToBytesAndDecompressToBytesTest(new SnappyCompressor());

private void CompressToBytesAndDecompressToBytesTest(ICompressor compressor)
{
var compressedBytes = compressor.Compress(TestConsts.Data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void ZstdCompressToBytesAndDecompressToStreamTest() =>
public void XzCompressToBytesAndDecompressToStreamTest() =>
CompressToBytesAndDecompressToStreamTest(new XzCompressor());

[Fact]
public void SnappyCompressToBytesAndDecompressToStreamTest() =>
CompressToBytesAndDecompressToStreamTest(new SnappyCompressor());

private void CompressToBytesAndDecompressToStreamTest(ICompressor compressor)
{
var compressedBytes = compressor.Compress(TestConsts.Data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public void ZstdCompressToStreamAndDecompressToBytesTest() =>
public void XzCompressToStreamAndDecompressToBytesTest() =>
CompressToStreamAndDecompressToBytesTest(new XzCompressor());

[Fact]
public void SnappyCompressToStreamAndDecompressToBytesTest() =>
CompressToStreamAndDecompressToBytesTest(new SnappyCompressor());

private void CompressToStreamAndDecompressToBytesTest(ICompressor compressor)
{
var compressedStream = new MemoryStream();
Expand Down
Loading

0 comments on commit 548a820

Please sign in to comment.