From be97c80fdecadac37413a6a6a8de417e0332f6bf Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 24 Oct 2022 15:49:27 +0700 Subject: Use platform compression where available - Move Bzip2 code into Utilities --- crypto/src/util/io/compression/Bzip2.cs | 21 +++++++++++++++ crypto/src/util/io/compression/ZLib.cs | 46 +++++++++++++++++++++++++++++++++ crypto/src/util/io/compression/Zip.cs | 33 +++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 crypto/src/util/io/compression/Bzip2.cs create mode 100644 crypto/src/util/io/compression/ZLib.cs create mode 100644 crypto/src/util/io/compression/Zip.cs (limited to 'crypto/src/util/io') diff --git a/crypto/src/util/io/compression/Bzip2.cs b/crypto/src/util/io/compression/Bzip2.cs new file mode 100644 index 000000000..72b006dc9 --- /dev/null +++ b/crypto/src/util/io/compression/Bzip2.cs @@ -0,0 +1,21 @@ +using System.IO; + +namespace Org.BouncyCastle.Utilities.IO.Compression +{ + using Impl = Utilities.Bzip2; + + internal static class Bzip2 + { + internal static Stream CompressOutput(Stream stream, bool leaveOpen = false) + { + return leaveOpen + ? new Impl.CBZip2OutputStreamLeaveOpen(stream) + : new Impl.CBZip2OutputStream(stream); + } + + internal static Stream DecompressInput(Stream stream) + { + return new Impl.CBZip2InputStream(stream); + } + } +} diff --git a/crypto/src/util/io/compression/ZLib.cs b/crypto/src/util/io/compression/ZLib.cs new file mode 100644 index 000000000..1254da012 --- /dev/null +++ b/crypto/src/util/io/compression/ZLib.cs @@ -0,0 +1,46 @@ +using System.IO; + +#if NET6_0_OR_GREATER +using System.IO.Compression; +#else +using Org.BouncyCastle.Utilities.Zlib; +#endif + +namespace Org.BouncyCastle.Utilities.IO.Compression +{ + internal static class ZLib + { + internal static Stream CompressOutput(Stream stream, int zlibCompressionLevel, bool leaveOpen = false) + { +#if NET6_0_OR_GREATER + return new ZLibStream(stream, GetCompressionLevel(zlibCompressionLevel), leaveOpen); +#else + return leaveOpen + ? new ZOutputStreamLeaveOpen(stream, zlibCompressionLevel, false) + : new ZOutputStream(stream, zlibCompressionLevel, false); +#endif + } + + internal static Stream DecompressInput(Stream stream) + { +#if NET6_0_OR_GREATER + return new ZLibStream(stream, CompressionMode.Decompress, leaveOpen: false); +#else + return new ZInputStream(stream); +#endif + } + +#if NET6_0_OR_GREATER + internal static CompressionLevel GetCompressionLevel(int zlibCompressionLevel) + { + return zlibCompressionLevel switch + { + 0 => CompressionLevel.NoCompression, + 1 or 2 or 3 => CompressionLevel.Fastest, + 7 or 8 or 9 => CompressionLevel.SmallestSize, + _ => CompressionLevel.Optimal, + }; + } +#endif + } +} diff --git a/crypto/src/util/io/compression/Zip.cs b/crypto/src/util/io/compression/Zip.cs new file mode 100644 index 000000000..f2773d63b --- /dev/null +++ b/crypto/src/util/io/compression/Zip.cs @@ -0,0 +1,33 @@ +using System.IO; + +#if NET6_0_OR_GREATER +using System.IO.Compression; +#else +using Org.BouncyCastle.Utilities.Zlib; +#endif + +namespace Org.BouncyCastle.Utilities.IO.Compression +{ + internal static class Zip + { + internal static Stream CompressOutput(Stream stream, int zlibCompressionLevel, bool leaveOpen = false) + { +#if NET6_0_OR_GREATER + return new DeflateStream(stream, ZLib.GetCompressionLevel(zlibCompressionLevel), leaveOpen); +#else + return leaveOpen + ? new ZOutputStreamLeaveOpen(stream, zlibCompressionLevel, true) + : new ZOutputStream(stream, zlibCompressionLevel, true); +#endif + } + + internal static Stream DecompressInput(Stream stream) + { +#if NET6_0_OR_GREATER + return new DeflateStream(stream, CompressionMode.Decompress, leaveOpen: false); +#else + return new ZInputStream(stream, true); +#endif + } + } +} -- cgit 1.4.1