Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mutuduxf committed Dec 12, 2023
1 parent af1e4a3 commit 2705f44
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 130 deletions.
2 changes: 1 addition & 1 deletion perf/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 10 additions & 16 deletions src/Zaabee.Brotli/BrotliCompressor.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
namespace Zaabee.Brotli;

public sealed class BrotliCompressor : ICompressor
public sealed class BrotliCompressor(
uint quality = BrotliHelper.Quality,
uint window = BrotliHelper.Window)
: ICompressor
{
private readonly uint _quality;
private readonly uint _window;

public BrotliCompressor(uint quality = BrotliHelper.Quality, uint window = BrotliHelper.Window)
{
_quality = quality;
_window = window;
}

public ValueTask<MemoryStream> CompressAsync(
Stream rawStream,
CancellationToken cancellationToken = default
) => rawStream.ToBrotliAsync(_quality, _window, cancellationToken);
) => rawStream.ToBrotliAsync(quality, window, cancellationToken);

public ValueTask<MemoryStream> DecompressAsync(
Stream compressedStream,
Expand All @@ -25,25 +19,25 @@ public ValueTask CompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.ToBrotliAsync(outputStream, _quality, _window, cancellationToken);
) => inputStream.ToBrotliAsync(outputStream, quality, window, cancellationToken);

public ValueTask DecompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.UnBrotliAsync(outputStream, cancellationToken);

public byte[] Compress(byte[] rawBytes) => rawBytes.ToBrotli(_quality, _window);
public byte[] Compress(byte[] rawBytes) => rawBytes.ToBrotli(quality, window);

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

public MemoryStream Compress(Stream rawStream) => rawStream.ToBrotli(_quality, _window);
public MemoryStream Compress(Stream rawStream) => rawStream.ToBrotli(quality, window);

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

public void Compress(Stream inputStream, Stream outputStream) =>
inputStream.ToBrotli(outputStream, _quality, _window);
inputStream.ToBrotli(outputStream, quality, window);

public void Decompress(Stream inputStream, Stream outputStream) =>
inputStream.UnBrotli(outputStream);
}
}
45 changes: 16 additions & 29 deletions src/Zaabee.LZ4/Lz4Compressor.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,47 @@
namespace Zaabee.LZ4;

public sealed class Lz4Compressor : ICompressor
public sealed class Lz4Compressor(
LZ4Level level = Lz4Helper.Level,
int extraMemory = Lz4Helper.ExtraMemory,
bool interactive = Lz4Helper.Interactive,
LZ4DecoderSettings? settings = Lz4Helper.Settings)
: ICompressor
{
private readonly LZ4Level _level;
private readonly int _extraMemory;
private readonly bool _interactive;
private readonly LZ4DecoderSettings? _settings;

public Lz4Compressor(
LZ4Level level = Lz4Helper.Level,
int extraMemory = Lz4Helper.ExtraMemory,
bool interactive = Lz4Helper.Interactive,
LZ4DecoderSettings? settings = Lz4Helper.Settings
)
{
_interactive = interactive;
_settings = settings;
_extraMemory = extraMemory;
_level = level;
}

public ValueTask<MemoryStream> CompressAsync(
Stream rawStream,
CancellationToken cancellationToken = default
) => rawStream.ToLz4Async(_level, _extraMemory, cancellationToken);
) => rawStream.ToLz4Async(level, extraMemory, cancellationToken);

public ValueTask<MemoryStream> DecompressAsync(
Stream compressedStream,
CancellationToken cancellationToken = default
) => compressedStream.UnLz4Async(_settings, _interactive, cancellationToken);
) => compressedStream.UnLz4Async(settings, interactive, cancellationToken);

public ValueTask CompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.ToLz4Async(outputStream, _level, _extraMemory, cancellationToken);
) => inputStream.ToLz4Async(outputStream, level, extraMemory, cancellationToken);

public ValueTask DecompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.UnLz4Async(outputStream, _settings, _interactive, cancellationToken);
) => inputStream.UnLz4Async(outputStream, settings, interactive, cancellationToken);

public byte[] Compress(byte[] rawBytes) => rawBytes.ToLz4(_level, _extraMemory);
public byte[] Compress(byte[] rawBytes) => rawBytes.ToLz4(level, extraMemory);

public byte[] Decompress(byte[] compressedBytes) =>
compressedBytes.UnLz4(_settings, _interactive);
compressedBytes.UnLz4(settings, interactive);

public MemoryStream Compress(Stream rawStream) => rawStream.ToLz4(_level, _extraMemory);
public MemoryStream Compress(Stream rawStream) => rawStream.ToLz4(level, extraMemory);

public MemoryStream Decompress(Stream compressedStream) =>
compressedStream.UnLz4(_settings, _interactive);
compressedStream.UnLz4(settings, interactive);

public void Compress(Stream inputStream, Stream outputStream) =>
inputStream.ToLz4(outputStream, _level, _extraMemory);
inputStream.ToLz4(outputStream, level, extraMemory);

public void Decompress(Stream inputStream, Stream outputStream) =>
inputStream.UnLz4(outputStream, _settings, _interactive);
inputStream.UnLz4(outputStream, settings, interactive);
}
6 changes: 6 additions & 0 deletions src/Zaabee.SharpZipLib/BZip2.Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Zaabee.SharpZipLib;

public static partial class Bzip2Helper
{
internal const int BlockSize = 9;
}
7 changes: 7 additions & 0 deletions src/Zaabee.SharpZipLib/Deflate.Helper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Zaabee.SharpZipLib;

public static partial class DeflateHelper
{
internal const Deflater? Deflater = null;
internal const int BufferSize = 512;
}
1 change: 1 addition & 0 deletions src/Zaabee.SharpZipLib/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
global using System.Threading.Tasks;
global using ICSharpCode.SharpZipLib.BZip2;
global using ICSharpCode.SharpZipLib.GZip;
global using ICSharpCode.SharpZipLib.Zip.Compression;
global using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
global using Zaabee.Compressor.Abstractions;
global using Zaabee.Extensions;
19 changes: 6 additions & 13 deletions src/Zaabee.SystemIoCompression/BrotliCompressor.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
#if !NETSTANDARD2_0
namespace Zaabee.SystemIoCompression;

public sealed class BrotliCompressor : ICompressor
public sealed class BrotliCompressor(CompressionLevel compressionLevel = CompressionLevel.Optimal) : ICompressor
{
private readonly CompressionLevel _compressionLevel;

public BrotliCompressor(CompressionLevel compressionLevel = CompressionLevel.Optimal)
{
_compressionLevel = compressionLevel;
}

public ValueTask<MemoryStream> CompressAsync(
Stream rawStream,
CancellationToken cancellationToken = default
) => rawStream.ToBrotliAsync(_compressionLevel, cancellationToken);
) => rawStream.ToBrotliAsync(compressionLevel, cancellationToken);

public ValueTask<MemoryStream> DecompressAsync(
Stream compressedStream,
Expand All @@ -24,24 +17,24 @@ public ValueTask CompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.ToBrotliAsync(outputStream, _compressionLevel, cancellationToken);
) => inputStream.ToBrotliAsync(outputStream, compressionLevel, cancellationToken);

public ValueTask DecompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.UnBrotliAsync(outputStream, cancellationToken);

public byte[] Compress(byte[] rawBytes) => rawBytes.ToBrotli(_compressionLevel);
public byte[] Compress(byte[] rawBytes) => rawBytes.ToBrotli(compressionLevel);

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

public MemoryStream Compress(Stream rawStream) => rawStream.ToBrotli(_compressionLevel);
public MemoryStream Compress(Stream rawStream) => rawStream.ToBrotli(compressionLevel);

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

public void Compress(Stream inputStream, Stream outputStream) =>
inputStream.ToBrotli(outputStream, _compressionLevel);
inputStream.ToBrotli(outputStream, compressionLevel);

public void Decompress(Stream inputStream, Stream outputStream) =>
inputStream.UnBrotli(outputStream);
Expand Down
19 changes: 6 additions & 13 deletions src/Zaabee.SystemIoCompression/DeflateCompressor.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
namespace Zaabee.SystemIoCompression;

public sealed class DeflateCompressor : ICompressor
public sealed class DeflateCompressor(CompressionLevel compressionLevel = CompressionLevel.Optimal) : ICompressor
{
private readonly CompressionLevel _compressionLevel;

public DeflateCompressor(CompressionLevel compressionLevel = CompressionLevel.Optimal)
{
_compressionLevel = compressionLevel;
}

public ValueTask<MemoryStream> CompressAsync(
Stream rawStream,
CancellationToken cancellationToken = default
) => rawStream.ToDeflateAsync(_compressionLevel, cancellationToken);
) => rawStream.ToDeflateAsync(compressionLevel, cancellationToken);

public ValueTask<MemoryStream> DecompressAsync(
Stream compressedStream,
Expand All @@ -23,24 +16,24 @@ public ValueTask CompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.ToDeflateAsync(outputStream, _compressionLevel, cancellationToken);
) => inputStream.ToDeflateAsync(outputStream, compressionLevel, cancellationToken);

public ValueTask DecompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.UnDeflateAsync(outputStream, cancellationToken);

public byte[] Compress(byte[] rawBytes) => rawBytes.ToDeflate(_compressionLevel);
public byte[] Compress(byte[] rawBytes) => rawBytes.ToDeflate(compressionLevel);

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

public MemoryStream Compress(Stream rawStream) => rawStream.ToDeflate(_compressionLevel);
public MemoryStream Compress(Stream rawStream) => rawStream.ToDeflate(compressionLevel);

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

public void Compress(Stream inputStream, Stream outputStream) =>
inputStream.ToDeflate(outputStream, _compressionLevel);
inputStream.ToDeflate(outputStream, compressionLevel);

public void Decompress(Stream inputStream, Stream outputStream) =>
inputStream.UnDeflate(outputStream);
Expand Down
19 changes: 6 additions & 13 deletions src/Zaabee.SystemIoCompression/GzipCompressor.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
namespace Zaabee.SystemIoCompression;

public sealed class GzipCompressor : ICompressor
public sealed class GzipCompressor(CompressionLevel compressionLevel = CompressionLevel.Optimal) : ICompressor
{
private readonly CompressionLevel _compressionLevel;

public GzipCompressor(CompressionLevel compressionLevel = CompressionLevel.Optimal)
{
_compressionLevel = compressionLevel;
}

public ValueTask<MemoryStream> CompressAsync(
Stream rawStream,
CancellationToken cancellationToken = default
) => rawStream.ToGZipAsync(_compressionLevel, cancellationToken);
) => rawStream.ToGZipAsync(compressionLevel, cancellationToken);

public ValueTask<MemoryStream> DecompressAsync(
Stream compressedStream,
Expand All @@ -23,24 +16,24 @@ public ValueTask CompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.ToGZipAsync(outputStream, _compressionLevel, cancellationToken);
) => inputStream.ToGZipAsync(outputStream, compressionLevel, cancellationToken);

public ValueTask DecompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.UnGZipAsync(outputStream, cancellationToken);

public byte[] Compress(byte[] rawBytes) => rawBytes.ToGZip(_compressionLevel);
public byte[] Compress(byte[] rawBytes) => rawBytes.ToGZip(compressionLevel);

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

public MemoryStream Compress(Stream rawStream) => rawStream.ToGZip(_compressionLevel);
public MemoryStream Compress(Stream rawStream) => rawStream.ToGZip(compressionLevel);

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

public void Compress(Stream inputStream, Stream outputStream) =>
inputStream.ToGZip(outputStream, _compressionLevel);
inputStream.ToGZip(outputStream, compressionLevel);

public void Decompress(Stream inputStream, Stream outputStream) =>
inputStream.UnGZip(outputStream);
Expand Down
22 changes: 8 additions & 14 deletions src/Zaabee.XZ/XzCompressor.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
namespace Zaabee.XZ;

public sealed class XzCompressor : ICompressor
public sealed class XzCompressor(
int threads = XzHelper.Threads,
uint preset = XzHelper.Preset)
: ICompressor
{
private readonly int _threads;
private readonly uint _preset;

public XzCompressor(int threads = XzHelper.Threads, uint preset = XzHelper.Preset)
{
_threads = threads;
_preset = preset;
}

public ValueTask<MemoryStream> CompressAsync(
Stream rawStream,
CancellationToken cancellationToken = default
) => rawStream.ToXzAsync(_threads, _preset, cancellationToken);
) => rawStream.ToXzAsync(threads, preset, cancellationToken);

public ValueTask<MemoryStream> DecompressAsync(
Stream compressedStream,
Expand All @@ -25,7 +19,7 @@ public ValueTask CompressAsync(
Stream inputStream,
Stream outputStream,
CancellationToken cancellationToken = default
) => inputStream.ToXzAsync(outputStream, _threads, _preset, cancellationToken);
) => inputStream.ToXzAsync(outputStream, threads, preset, cancellationToken);

public ValueTask DecompressAsync(
Stream inputStream,
Expand All @@ -37,12 +31,12 @@ public ValueTask DecompressAsync(

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

public MemoryStream Compress(Stream rawStream) => rawStream.ToXz(_threads, _preset);
public MemoryStream Compress(Stream rawStream) => rawStream.ToXz(threads, preset);

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

public void Compress(Stream inputStream, Stream outputStream) =>
inputStream.ToXz(outputStream, _threads, _preset);
inputStream.ToXz(outputStream, threads, preset);

public void Decompress(Stream inputStream, Stream outputStream) =>
inputStream.UnXz(outputStream);
Expand Down
Loading

0 comments on commit 2705f44

Please sign in to comment.