PushbackStream.cs 904 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.IO;
  4. using Org.BouncyCastle.Asn1.Utilities;
  5. namespace Org.BouncyCastle.Utilities.IO
  6. {
  7. public class PushbackStream
  8. : FilterStream
  9. {
  10. private int buf = -1;
  11. public PushbackStream(
  12. Stream s)
  13. : base(s)
  14. {
  15. }
  16. public override int ReadByte()
  17. {
  18. if (buf != -1)
  19. {
  20. int tmp = buf;
  21. buf = -1;
  22. return tmp;
  23. }
  24. return base.ReadByte();
  25. }
  26. public override int Read(byte[] buffer, int offset, int count)
  27. {
  28. if (buf != -1 && count > 0)
  29. {
  30. // TODO Can this case be made more efficient?
  31. buffer[offset] = (byte) buf;
  32. buf = -1;
  33. return 1;
  34. }
  35. return base.Read(buffer, offset, count);
  36. }
  37. public virtual void Unread(int b)
  38. {
  39. if (buf != -1)
  40. throw new InvalidOperationException("Can only push back one byte");
  41. buf = b & 0xFF;
  42. }
  43. }
  44. }
  45. #endif