TeeInputStream.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. namespace Org.BouncyCastle.Utilities.IO
  6. {
  7. public class TeeInputStream
  8. : BaseInputStream
  9. {
  10. private readonly Stream input, tee;
  11. public TeeInputStream(Stream input, Stream tee)
  12. {
  13. Debug.Assert(input.CanRead);
  14. Debug.Assert(tee.CanWrite);
  15. this.input = input;
  16. this.tee = tee;
  17. }
  18. #if PORTABLE || NETFX_CORE
  19. protected override void Dispose(bool disposing)
  20. {
  21. if (disposing)
  22. {
  23. Org.BouncyCastle.Utilities.Platform.Dispose(input);
  24. Org.BouncyCastle.Utilities.Platform.Dispose(tee);
  25. }
  26. base.Dispose(disposing);
  27. }
  28. #else
  29. public override void Close()
  30. {
  31. Org.BouncyCastle.Utilities.Platform.Dispose(input);
  32. Org.BouncyCastle.Utilities.Platform.Dispose(tee);
  33. base.Close();
  34. }
  35. #endif
  36. public override int Read(byte[] buf, int off, int len)
  37. {
  38. int i = input.Read(buf, off, len);
  39. if (i > 0)
  40. {
  41. tee.Write(buf, off, i);
  42. }
  43. return i;
  44. }
  45. public override int ReadByte()
  46. {
  47. int i = input.ReadByte();
  48. if (i >= 0)
  49. {
  50. tee.WriteByte((byte)i);
  51. }
  52. return i;
  53. }
  54. }
  55. }
  56. #endif