diff options
Diffstat (limited to 'crypto/src/util')
-rw-r--r-- | crypto/src/util/io/BaseInputStream.cs | 4 | ||||
-rw-r--r-- | crypto/src/util/io/BaseOutputStream.cs | 3 | ||||
-rw-r--r-- | crypto/src/util/io/FilterStream.cs | 2 | ||||
-rw-r--r-- | crypto/src/util/io/PushbackStream.cs | 2 | ||||
-rw-r--r-- | crypto/src/util/io/Streams.cs | 50 |
5 files changed, 50 insertions, 11 deletions
diff --git a/crypto/src/util/io/BaseInputStream.cs b/crypto/src/util/io/BaseInputStream.cs index 438583c75..4551f2ef7 100644 --- a/crypto/src/util/io/BaseInputStream.cs +++ b/crypto/src/util/io/BaseInputStream.cs @@ -1,7 +1,7 @@ using System; using System.IO; -using System.Threading; #if NETCOREAPP1_0_OR_GREATER || NET45_OR_GREATER || NETSTANDARD1_0_OR_GREATER +using System.Threading; using System.Threading.Tasks; #endif @@ -102,5 +102,7 @@ namespace Org.BouncyCastle.Utilities.IO throw new NotSupportedException(); } #endif + + // TODO[api] WriteByte } } diff --git a/crypto/src/util/io/BaseOutputStream.cs b/crypto/src/util/io/BaseOutputStream.cs index 72234b817..9c8ddc811 100644 --- a/crypto/src/util/io/BaseOutputStream.cs +++ b/crypto/src/util/io/BaseOutputStream.cs @@ -1,7 +1,7 @@ using System; using System.IO; -using System.Threading; #if NETCOREAPP1_0_OR_GREATER || NET45_OR_GREATER || NETSTANDARD1_0_OR_GREATER +using System.Threading; using System.Threading.Tasks; #endif @@ -43,6 +43,7 @@ namespace Org.BouncyCastle.Utilities.IO throw new NotSupportedException(); } #endif + // TODO[api] ReadByte public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); } public sealed override void SetLength(long value) { throw new NotSupportedException(); } diff --git a/crypto/src/util/io/FilterStream.cs b/crypto/src/util/io/FilterStream.cs index 8fb6d9df4..80518c6d1 100644 --- a/crypto/src/util/io/FilterStream.cs +++ b/crypto/src/util/io/FilterStream.cs @@ -1,7 +1,7 @@ using System; using System.IO; -using System.Threading; #if NETCOREAPP1_0_OR_GREATER || NET45_OR_GREATER || NETSTANDARD1_0_OR_GREATER +using System.Threading; using System.Threading.Tasks; #endif diff --git a/crypto/src/util/io/PushbackStream.cs b/crypto/src/util/io/PushbackStream.cs index 452019805..739972b4e 100644 --- a/crypto/src/util/io/PushbackStream.cs +++ b/crypto/src/util/io/PushbackStream.cs @@ -1,7 +1,7 @@ using System; using System.IO; -using System.Threading; #if NETCOREAPP1_0_OR_GREATER || NET45_OR_GREATER || NETSTANDARD1_0_OR_GREATER +using System.Threading; using System.Threading.Tasks; #endif 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> |