diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-11-01 14:39:54 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-11-01 14:39:54 +0700 |
commit | 716a491e3ed312da6c80a74e327d62dd4388b11e (patch) | |
tree | 0adabea28431857f372256233ddd4b2e0982190b /crypto/src/openpgp | |
parent | Package with LICENSE.md file (diff) | |
download | BouncyCastle.NET-ed25519-716a491e3ed312da6c80a74e327d62dd4388b11e.tar.xz |
More Span-based Stream methods
Diffstat (limited to 'crypto/src/openpgp')
-rw-r--r-- | crypto/src/openpgp/PgpEncryptedData.cs | 29 | ||||
-rw-r--r-- | crypto/src/openpgp/WrappedGeneratorStream.cs | 5 |
2 files changed, 30 insertions, 4 deletions
diff --git a/crypto/src/openpgp/PgpEncryptedData.cs b/crypto/src/openpgp/PgpEncryptedData.cs index 5cdc0d533..bad4cb8cd 100644 --- a/crypto/src/openpgp/PgpEncryptedData.cs +++ b/crypto/src/openpgp/PgpEncryptedData.cs @@ -54,6 +54,9 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp { Streams.ValidateBufferArguments(buffer, offset, count); +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + return Read(buffer.AsSpan(offset, count)); +#else int avail = bufEnd - bufStart; int pos = offset; @@ -73,8 +76,34 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp bufStart += count; return pos + count - offset; +#endif } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public override int Read(Span<byte> buffer) + { + int avail = bufEnd - bufStart; + + int pos = 0, count = buffer.Length; + while (count > avail) + { + lookAhead.AsSpan(bufStart, avail).CopyTo(buffer[pos..]); + + bufStart += avail; + pos += avail; + count -= avail; + + if ((avail = FillBuffer()) < 1) + return pos; + } + + lookAhead.AsSpan(bufStart, count).CopyTo(buffer[pos..]); + bufStart += count; + + return pos + count; + } +#endif + public override int ReadByte() { if (bufStart < bufEnd) diff --git a/crypto/src/openpgp/WrappedGeneratorStream.cs b/crypto/src/openpgp/WrappedGeneratorStream.cs index c54ee0b3b..6f96dc9b8 100644 --- a/crypto/src/openpgp/WrappedGeneratorStream.cs +++ b/crypto/src/openpgp/WrappedGeneratorStream.cs @@ -13,10 +13,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp internal WrappedGeneratorStream(IStreamGenerator generator, Stream s) : base(s) { - if (generator == null) - throw new ArgumentNullException(nameof(generator)); - - m_generator = generator; + m_generator = generator ?? throw new ArgumentNullException(nameof(generator)); } protected override void Dispose(bool disposing) |