HttpResponseStream.cs 3.7 KB

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