diff options
Diffstat (limited to 'crypto/src/bcpg/BcpgInputStream.cs')
-rw-r--r-- | crypto/src/bcpg/BcpgInputStream.cs | 73 |
1 files changed, 51 insertions, 22 deletions
diff --git a/crypto/src/bcpg/BcpgInputStream.cs b/crypto/src/bcpg/BcpgInputStream.cs index 895b03260..3b6f61bbc 100644 --- a/crypto/src/bcpg/BcpgInputStream.cs +++ b/crypto/src/bcpg/BcpgInputStream.cs @@ -57,27 +57,46 @@ namespace Org.BouncyCastle.Bcpg return 1; } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public override int Read(Span<byte> buffer) + { + if (!next) + return m_in.Read(buffer); + + if (nextB < 0) + return 0; + + buffer[0] = (byte)nextB; + next = false; + return 1; + } +#endif + public byte[] ReadAll() { return Streams.ReadAll(this); } - public void ReadFully( - byte[] buffer, - int off, - int len) + public void ReadFully(byte[] buffer, int offset, int count) { - if (Streams.ReadFully(this, buffer, off, len) < len) + if (Streams.ReadFully(this, buffer, offset, count) < count) throw new EndOfStreamException(); } - public void ReadFully( - byte[] buffer) + public void ReadFully(byte[] buffer) { ReadFully(buffer, 0, buffer.Length); } - /// <summary>Returns the next packet tag in the stream.</summary> +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public void ReadFully(Span<byte> buffer) + { + if (Streams.ReadFully(this, buffer) < buffer.Length) + throw new EndOfStreamException(); + } +#endif + + /// <summary>Returns the next packet tag in the stream.</summary> public PacketTag NextPacketTag() { if (!next) @@ -184,11 +203,7 @@ namespace Org.BouncyCastle.Bcpg else { PartialInputStream pis = new PartialInputStream(this, partial, bodyLen); -#if PORTABLE - Stream buf = pis; -#else Stream buf = new BufferedStream(pis); -#endif objStream = new BcpgInputStream(buf); } @@ -251,7 +266,6 @@ namespace Org.BouncyCastle.Bcpg return tag; } -#if PORTABLE protected override void Dispose(bool disposing) { if (disposing) @@ -260,13 +274,6 @@ namespace Org.BouncyCastle.Bcpg } base.Dispose(disposing); } -#else - public override void Close() - { - Platform.Dispose(m_in); - base.Close(); - } -#endif /// <summary> /// A stream that overlays our input stream, allowing the user to only read a segment of it. @@ -320,9 +327,8 @@ namespace Org.BouncyCastle.Bcpg int readLen = (dataLength > count || dataLength < 0) ? count : dataLength; int len = m_in.Read(buffer, offset, readLen); if (len < 1) - { throw new EndOfStreamException("Premature end of stream in PartialInputStream"); - } + dataLength -= len; return len; } @@ -332,6 +338,29 @@ namespace Org.BouncyCastle.Bcpg return 0; } +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public override int Read(Span<byte> buffer) + { + do + { + if (dataLength != 0) + { + int count = buffer.Length; + int readLen = (dataLength > count || dataLength < 0) ? count : dataLength; + int len = m_in.Read(buffer[..readLen]); + if (len < 1) + throw new EndOfStreamException("Premature end of stream in PartialInputStream"); + + dataLength -= len; + return len; + } + } + while (partial && ReadPartialDataLength() >= 0); + + return 0; + } +#endif + private int ReadPartialDataLength() { int l = m_in.ReadByte(); |