PushbackStream.cs 998 B

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