TeeInputStream.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.IO
  7. {
  8. public class TeeInputStream
  9. : BaseInputStream
  10. {
  11. private readonly Stream input, tee;
  12. public TeeInputStream(Stream input, Stream tee)
  13. {
  14. Debug.Assert(input.CanRead);
  15. Debug.Assert(tee.CanWrite);
  16. this.input = input;
  17. this.tee = tee;
  18. }
  19. protected override void Dispose(bool disposing)
  20. {
  21. if (disposing)
  22. {
  23. input.Dispose();
  24. tee.Dispose();
  25. }
  26. base.Dispose(disposing);
  27. }
  28. public override int Read(byte[] buffer, int offset, int count)
  29. {
  30. int i = input.Read(buffer, offset, count);
  31. if (i > 0)
  32. {
  33. tee.Write(buffer, offset, i);
  34. }
  35. return i;
  36. }
  37. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  38. public override int Read(Span<byte> buffer)
  39. {
  40. int i = input.Read(buffer);
  41. if (i > 0)
  42. {
  43. tee.Write(buffer[..i]);
  44. }
  45. return i;
  46. }
  47. #endif
  48. public override int ReadByte()
  49. {
  50. int i = input.ReadByte();
  51. if (i >= 0)
  52. {
  53. tee.WriteByte((byte)i);
  54. }
  55. return i;
  56. }
  57. }
  58. }
  59. #pragma warning restore
  60. #endif