HttpResponseStream.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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
  17. {
  18. get
  19. {
  20. throw new NotImplementedException();
  21. }
  22. }
  23. public override long Position
  24. {
  25. get
  26. {
  27. throw new NotImplementedException();
  28. }
  29. set
  30. {
  31. throw new NotImplementedException();
  32. }
  33. }
  34. public override long Seek(long offset, SeekOrigin origin)
  35. {
  36. throw new NotImplementedException();
  37. }
  38. public override void SetLength(long value)
  39. {
  40. throw new NotImplementedException();
  41. }
  42. public override int Read(byte[] buffer, int offset, int count)
  43. {
  44. throw new NotImplementedException();
  45. }
  46. public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
  47. {
  48. return base.BeginRead(buffer, offset, count, callback, state);
  49. }
  50. public override int EndRead(IAsyncResult asyncResult)
  51. {
  52. return base.EndRead(asyncResult);
  53. }
  54. public override void Write(byte[] buffer, int offset, int size)
  55. {
  56. if (buffer == null)
  57. {
  58. throw new ArgumentNullException(nameof(buffer));
  59. }
  60. if (offset < 0 || offset > buffer.Length)
  61. {
  62. throw new ArgumentOutOfRangeException(nameof(offset));
  63. }
  64. if (size < 0 || size > buffer.Length - offset)
  65. {
  66. throw new ArgumentOutOfRangeException(nameof(size));
  67. }
  68. if (_closed)
  69. {
  70. return;
  71. }
  72. WriteCore(buffer, offset, size);
  73. }
  74. public override IAsyncResult BeginWrite(byte[] buffer, int offset, int size, AsyncCallback callback, object state)
  75. {
  76. if (buffer == null)
  77. {
  78. throw new ArgumentNullException(nameof(buffer));
  79. }
  80. if (offset < 0 || offset > buffer.Length)
  81. {
  82. throw new ArgumentOutOfRangeException(nameof(offset));
  83. }
  84. if (size < 0 || size > buffer.Length - offset)
  85. {
  86. throw new ArgumentOutOfRangeException(nameof(size));
  87. }
  88. return BeginWriteCore(buffer, offset, size, callback, state);
  89. }
  90. public override void EndWrite(IAsyncResult asyncResult)
  91. {
  92. if (asyncResult == null)
  93. {
  94. throw new ArgumentNullException(nameof(asyncResult));
  95. }
  96. EndWriteCore(asyncResult);
  97. }
  98. protected override void Dispose(bool disposing)
  99. {
  100. try
  101. {
  102. if (disposing)
  103. {
  104. if (_closed)
  105. {
  106. return;
  107. }
  108. _closed = true;
  109. DisposeCore();
  110. }
  111. }
  112. finally
  113. {
  114. base.Dispose(disposing);
  115. }
  116. }
  117. }
  118. }