summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-02-24 19:26:41 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-02-24 19:26:41 +0700
commit4ca163fde3324e7763cc35a0441d17bf75237f8c (patch)
tree0010b63a6ecc3ac43c4ea936bf390f7d0cd25701
parentExtra constructor (diff)
downloadBouncyCastle.NET-ed25519-4ca163fde3324e7763cc35a0441d17bf75237f8c.tar.xz
PgpUtilities refactoring
-rw-r--r--crypto/src/openpgp/PgpUtilities.cs53
1 files 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
 {
-	/// <remarks>Basic utility class.</remarks>
+    /// <remarks>Basic utility class.</remarks>
     public sealed class PgpUtilities
     {
         private static readonly IDictionary<string, HashAlgorithmTag> NameToHashID = CreateNameToHashID();
@@ -406,51 +409,55 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp
         }
 
         /// <summary>Write out the passed in file as a literal data packet.</summary>
-        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);
+            }
         }
 
 		/// <summary>Write out the passed in file as a literal data packet in partial packet format.</summary>
-        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<byte>.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<byte>.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)
         {