diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-09 16:17:26 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2023-03-09 16:17:26 +0700 |
commit | 95db89f0bcf07e49ed86b235f3953718a50b6f54 (patch) | |
tree | 3354d8f3687f37f14800eb75627bf34630d90fdf /crypto/src/openpgp/PgpUtilities.cs | |
parent | Fix some warnings (diff) | |
download | BouncyCastle.NET-ed25519-95db89f0bcf07e49ed86b235f3953718a50b6f54.tar.xz |
Refactoring around Stream usage
Diffstat (limited to 'crypto/src/openpgp/PgpUtilities.cs')
-rw-r--r-- | crypto/src/openpgp/PgpUtilities.cs | 38 |
1 files changed, 10 insertions, 28 deletions
diff --git a/crypto/src/openpgp/PgpUtilities.cs b/crypto/src/openpgp/PgpUtilities.cs index 03bc73a21..17e100bff 100644 --- a/crypto/src/openpgp/PgpUtilities.cs +++ b/crypto/src/openpgp/PgpUtilities.cs @@ -1,7 +1,4 @@ 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,6 +14,7 @@ using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; using Org.BouncyCastle.Utilities.Encoders; +using Org.BouncyCastle.Utilities.IO; namespace Org.BouncyCastle.Bcpg.OpenPgp { @@ -414,7 +412,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp PgpLiteralDataGenerator lData = new PgpLiteralDataGenerator(); using (var pOut = lData.Open(output, fileType, file.Name, file.Length, file.LastWriteTime)) { - PipeFileContents(file, pOut, 32768); + PipeFileContents(file, pOut); } } @@ -428,32 +426,16 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp } } - private static void PipeFileContents(FileInfo file, Stream pOut, int bufSize) - { -#if NETCOREAPP1_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER - byte[] buf = ArrayPool<byte>.Shared.Rent(bufSize); -#else - byte[] buf = new byte[bufSize]; -#endif + private static void PipeFileContents(FileInfo file, Stream pOut) + { + PipeFileContents(file, pOut, Streams.DefaultBufferSize); + } - try - { - using (var fileStream = file.OpenRead()) - { - int len; - while ((len = fileStream.Read(buf, 0, buf.Length)) > 0) - { - pOut.Write(buf, 0, len); - } - } - } - finally + private static void PipeFileContents(FileInfo file, Stream pOut, int bufferSize) + { + using (var fileStream = file.OpenRead()) { -#if NETCOREAPP1_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER - ArrayPool<byte>.Shared.Return(buf, clearArray: true); -#else - Array.Clear(buf, 0, buf.Length); -#endif + Streams.CopyTo(fileStream, pOut, bufferSize); } } |