summary refs log tree commit diff
path: root/crypto/src/util/io/Streams.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-05-18 00:10:19 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-05-18 00:10:19 +0700
commita7d8b6406c3cb61a2aaa6f1e5bffd357561a7a41 (patch)
tree1f51b52d19800a3b0d8cbbf5e8e8e655dde88d99 /crypto/src/util/io/Streams.cs
parentMicrosoft.NET.Test.Sdk 17.6.0 (diff)
downloadBouncyCastle.NET-ed25519-a7d8b6406c3cb61a2aaa6f1e5bffd357561a7a41.tar.xz
Refactoring in Stream classes
Diffstat (limited to 'crypto/src/util/io/Streams.cs')
-rw-r--r--crypto/src/util/io/Streams.cs50
1 files changed, 43 insertions, 7 deletions
diff --git a/crypto/src/util/io/Streams.cs b/crypto/src/util/io/Streams.cs
index a1a2ea5d1..1c5210907 100644
--- a/crypto/src/util/io/Streams.cs
+++ b/crypto/src/util/io/Streams.cs
@@ -1,8 +1,10 @@
 using System;
 using System.IO;
+#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
 using System.Runtime.InteropServices;
-using System.Threading;
+#endif
 #if NETCOREAPP1_0_OR_GREATER || NET45_OR_GREATER || NETSTANDARD1_0_OR_GREATER
+using System.Threading;
 using System.Threading.Tasks;
 #endif
 
@@ -156,10 +158,10 @@ namespace Org.BouncyCastle.Utilities.IO
 
             byte[] sharedBuffer = new byte[buffer.Length];
 			var readTask = source.ReadAsync(sharedBuffer, 0, buffer.Length, cancellationToken);
-            return FinishReadAsync(readTask, sharedBuffer, buffer);
+            return ReadAsyncCompletion(readTask, sharedBuffer, buffer);
         }
 
-        private static async ValueTask<int> FinishReadAsync(Task<int> readTask, byte[] localBuffer,
+        internal static async ValueTask<int> ReadAsyncCompletion(Task<int> readTask, byte[] localBuffer,
 			Memory<byte> localDestination)
         {
             try
@@ -170,7 +172,7 @@ namespace Org.BouncyCastle.Utilities.IO
             }
             finally
             {
-                Array.Fill<byte>(localBuffer, 0x00);
+                Array.Clear(localBuffer, 0, localBuffer.Length);
             }
         }
 #endif
@@ -220,6 +222,30 @@ namespace Org.BouncyCastle.Utilities.IO
 				throw new ArgumentOutOfRangeException("count");
 		}
 
+#if NETCOREAPP1_0_OR_GREATER || NET45_OR_GREATER || NETSTANDARD1_0_OR_GREATER
+        internal static async Task WriteAsyncCompletion(Task writeTask, byte[] localBuffer)
+        {
+            try
+            {
+                await writeTask.ConfigureAwait(false);
+            }
+            finally
+            {
+                Array.Clear(localBuffer, 0, localBuffer.Length);
+            }
+        }
+
+        internal static Task WriteAsyncDirect(Stream destination, byte[] buffer, int offset, int count,
+            CancellationToken cancellationToken)
+        {
+            if (cancellationToken.IsCancellationRequested)
+                return Task.FromCanceled(cancellationToken);
+
+            destination.Write(buffer, offset, count);
+            return Task.CompletedTask;
+        }
+#endif
+
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
         public static ValueTask WriteAsync(Stream destination, ReadOnlyMemory<byte> buffer,
             CancellationToken cancellationToken = default)
@@ -232,10 +258,10 @@ namespace Org.BouncyCastle.Utilities.IO
 
             byte[] sharedBuffer = buffer.ToArray();
             var writeTask = destination.WriteAsync(sharedBuffer, 0, buffer.Length, cancellationToken);
-            return new ValueTask(FinishWriteAsync(writeTask, sharedBuffer));
+            return new ValueTask(WriteAsyncCompletion(writeTask, sharedBuffer));
         }
 
-        private static async Task FinishWriteAsync(Task writeTask, byte[] localBuffer)
+        internal static async ValueTask WriteAsyncCompletion(ValueTask writeTask, byte[] localBuffer)
         {
             try
             {
@@ -243,9 +269,19 @@ namespace Org.BouncyCastle.Utilities.IO
             }
             finally
             {
-                Array.Fill<byte>(localBuffer, 0x00);
+                Array.Clear(localBuffer, 0, localBuffer.Length);
             }
         }
+
+        internal static ValueTask WriteAsyncDirect(Stream destination, ReadOnlyMemory<byte> buffer,
+            CancellationToken cancellationToken = default)
+        {
+            if (cancellationToken.IsCancellationRequested)
+                return ValueTask.FromCanceled(cancellationToken);
+
+            destination.Write(buffer.Span);
+            return ValueTask.CompletedTask;
+        }
 #endif
 
         /// <exception cref="IOException"></exception>