From 4ca163fde3324e7763cc35a0441d17bf75237f8c Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 24 Feb 2023 19:26:41 +0700 Subject: PgpUtilities refactoring --- crypto/src/openpgp/PgpUtilities.cs | 53 +++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/crypto/src/openpgp/PgpUtilities.cs b/crypto/src/openpgp/PgpUtilities.cs index 8b8f7d9c5..03bc73a21 100644 --- a/crypto/src/openpgp/PgpUtilities.cs +++ b/crypto/src/openpgp/PgpUtilities.cs @@ -1,4 +1,7 @@ using System; +#if NETCOREAPP1_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER +using System.Buffers; +#endif using System.Collections.Generic; using System.IO; using System.Text; @@ -17,7 +20,7 @@ using Org.BouncyCastle.Utilities.Encoders; namespace Org.BouncyCastle.Bcpg.OpenPgp { - /// Basic utility class. + /// Basic utility class. public sealed class PgpUtilities { private static readonly IDictionary NameToHashID = CreateNameToHashID(); @@ -406,51 +409,55 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } /// Write out the passed in file as a literal data packet. - public static void WriteFileToLiteralData( - Stream output, - char fileType, - FileInfo file) + public static void WriteFileToLiteralData(Stream output, char fileType, FileInfo file) { PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator(); - Stream pOut = lData.Open(output, fileType, file.Name, file.Length, file.LastWriteTime); - PipeFileContents(file, pOut, 32768); + using (var pOut = lData.Open(output, fileType, file.Name, file.Length, file.LastWriteTime)) + { + PipeFileContents(file, pOut, 32768); + } } /// Write out the passed in file as a literal data packet in partial packet format. - public static void WriteFileToLiteralData( - Stream output, - char fileType, - FileInfo file, - byte[] buffer) + public static void WriteFileToLiteralData(Stream output, char fileType, FileInfo file, byte[] buffer) { PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator(); - Stream pOut = lData.Open(output, fileType, file.Name, file.LastWriteTime, buffer); - PipeFileContents(file, pOut, buffer.Length); + using (var pOut = lData.Open(output, fileType, file.Name, file.LastWriteTime, buffer)) + { + PipeFileContents(file, pOut, buffer.Length); + } } private static void PipeFileContents(FileInfo file, Stream pOut, int bufSize) { - FileStream inputStream = file.OpenRead(); - byte[] buf = new byte[bufSize]; +#if NETCOREAPP1_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER + byte[] buf = ArrayPool.Shared.Rent(bufSize); +#else + byte[] buf = new byte[bufSize]; +#endif try { - int len; - while ((len = inputStream.Read(buf, 0, buf.Length)) > 0) + using (var fileStream = file.OpenRead()) { - pOut.Write(buf, 0, len); + int len; + while ((len = fileStream.Read(buf, 0, buf.Length)) > 0) + { + pOut.Write(buf, 0, len); + } } } finally { +#if NETCOREAPP1_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER + ArrayPool.Shared.Return(buf, clearArray: true); +#else Array.Clear(buf, 0, buf.Length); - - pOut.Dispose(); - inputStream.Dispose(); +#endif } } - private const int ReadAhead = 60; + private const int ReadAhead = 60; private static bool IsPossiblyBase64(int ch) { -- cgit 1.4.1