HttpResponseStream.cs 3.7 KB

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