TeeInputStream.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 BestHTTP.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. #if PORTABLE || NETFX_CORE
  20. protected override void Dispose(bool disposing)
  21. {
  22. if (disposing)
  23. {
  24. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(input);
  25. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(tee);
  26. }
  27. base.Dispose(disposing);
  28. }
  29. #else
  30. public override void Close()
  31. {
  32. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(input);
  33. BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(tee);
  34. base.Close();
  35. }
  36. #endif
  37. public override int Read(byte[] buf, int off, int len)
  38. {
  39. int i = input.Read(buf, off, len);
  40. if (i > 0)
  41. {
  42. tee.Write(buf, off, i);
  43. }
  44. return i;
  45. }
  46. public override int ReadByte()
  47. {
  48. int i = input.ReadByte();
  49. if (i >= 0)
  50. {
  51. tee.WriteByte((byte)i);
  52. }
  53. return i;
  54. }
  55. }
  56. }
  57. #pragma warning restore
  58. #endif