HttpResponseStream.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. namespace SocketHttpListener.Net
  6. {
  7. internal sealed partial class HttpResponseStream : Stream
  8. {
  9. private bool _closed;
  10. internal bool Closed => _closed;
  11. public override bool CanRead => false;
  12. public override bool CanSeek => false;
  13. public override bool CanWrite => true;
  14. public override void Flush() { }
  15. public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask;
  16. public override long Length => throw new NotImplementedException();
  17. public override long Position
  18. {
  19. get => throw new NotImplementedException();
  20. set => throw new NotImplementedException();
  21. }
  22. public override long Seek(long offset, SeekOrigin origin)
  23. {
  24. throw new NotImplementedException();
  25. }
  26. public override void SetLength(long value)
  27. {
  28. throw new NotImplementedException();
  29. }
  30. public override int Read(byte[] buffer, int offset, int count)
  31. {
  32. throw new NotImplementedException();
  33. }
  34. public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
  35. {
  36. return base.BeginRead(buffer, offset, count, callback, state);
  37. }
  38. public override int EndRead(IAsyncResult asyncResult)
  39. {
  40. return base.EndRead(asyncResult);
  41. }
  42. public override void Write(byte[] buffer, int offset, int size)
  43. {
  44. if (buffer == null)
  45. {
  46. throw new ArgumentNullException(nameof(buffer));
  47. }
  48. if (offset < 0 || offset > buffer.Length)
  49. {
  50. throw new ArgumentOutOfRangeException(nameof(offset));
  51. }
  52. if (size < 0 || size > buffer.Length - offset)
  53. {
  54. throw new ArgumentOutOfRangeException(nameof(size));
  55. }
  56. if (_closed)
  57. {
  58. return;
  59. }
  60. WriteCore(buffer, offset, size);
  61. }
  62. public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
  63. {
  64. if (buffer == null)
  65. {
  66. throw new ArgumentNullException(nameof(buffer));
  67. }
  68. if (offset < 0 || offset > buffer.Length)
  69. {
  70. throw new ArgumentOutOfRangeException(nameof(offset));
  71. }
  72. if (size < 0 || size > buffer.Length - offset)
  73. {
  74. throw new ArgumentOutOfRangeException(nameof(size));
  75. }
  76. return BeginWriteCore(buffer, offset, size, callback, state);
  77. }
  78. public override void EndWrite(IAsyncResult asyncResult)
  79. {
  80. if (asyncResult == null)
  81. {
  82. throw new ArgumentNullException(nameof(asyncResult));
  83. }
  84. EndWriteCore(asyncResult);
  85. }
  86. protected override void Dispose(bool disposing)
  87. {
  88. try
  89. {
  90. if (disposing)
  91. {
  92. if (_closed)
  93. {
  94. return;
  95. }
  96. _closed = true;
  97. DisposeCore();
  98. }
  99. }
  100. finally
  101. {
  102. base.Dispose(disposing);
  103. }
  104. }
  105. }
  106. }