<PackageReference Include="BouncyCastle.Cryptography" Version="2.5.1" />

PushbackStream

public class PushbackStream : FilterStream
using System; using System.IO; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; namespace Org.BouncyCastle.Utilities.IO { public class PushbackStream : FilterStream { private int m_buf = -1; public PushbackStream(Stream s) : base(s) { } [AsyncStateMachine(typeof(<CopyToAsync>d__2))] public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken) { <CopyToAsync>d__2 stateMachine = default(<CopyToAsync>d__2); stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create(); stateMachine.<>4__this = this; stateMachine.destination = destination; stateMachine.bufferSize = bufferSize; stateMachine.cancellationToken = cancellationToken; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } public override int Read(byte[] buffer, int offset, int count) { Streams.ValidateBufferArguments(buffer, offset, count); if (m_buf != -1) { if (count < 1) return 0; buffer[offset] = (byte)m_buf; m_buf = -1; return 1; } return s.Read(buffer, offset, count); } public override int ReadByte() { if (m_buf != -1) { int buf = m_buf; m_buf = -1; return buf; } return s.ReadByte(); } public virtual void Unread(int b) { if (m_buf != -1) throw new InvalidOperationException("Can only push back one byte"); m_buf = (b & 255); } } }