RequestStream.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // System.Net.RequestStream
  3. //
  4. // Author:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System.IO;
  30. using System.Net.Sockets;
  31. using System.Runtime.InteropServices;
  32. namespace System.Net {
  33. class RequestStream : NetworkStream
  34. {
  35. byte [] buffer;
  36. int offset;
  37. int length;
  38. bool disposed;
  39. internal RequestStream (Socket sock, byte [] buffer, int offset, int length) :
  40. base (sock, false)
  41. {
  42. this.buffer = buffer;
  43. this.offset = offset;
  44. this.length = length;
  45. }
  46. public override bool CanRead {
  47. get { return true; }
  48. }
  49. public override bool CanSeek {
  50. get { return false; }
  51. }
  52. public override bool CanWrite {
  53. get { return false; }
  54. }
  55. public override long Length {
  56. get { throw new NotSupportedException (); }
  57. }
  58. public override long Position {
  59. get { throw new NotSupportedException (); }
  60. set { throw new NotSupportedException (); }
  61. }
  62. public override void Close ()
  63. {
  64. // TODO: What do we close?
  65. }
  66. public override void Flush ()
  67. {
  68. }
  69. // true if the we have >= count bytes in this.buffer
  70. int FillFromBuffer (byte [] buffer, int off, int count)
  71. {
  72. int size = this.length - this.offset;
  73. if (size <= 0)
  74. return 0;
  75. if (buffer == null)
  76. throw new ArgumentNullException ("buffer");
  77. if (off < 0)
  78. throw new ArgumentOutOfRangeException ("offset", "< 0");
  79. if (count < 0)
  80. throw new ArgumentOutOfRangeException ("count", "< 0");
  81. int len = buffer.Length;
  82. if (off > len)
  83. throw new ArgumentException ("destination offset is beyond array size");
  84. if (off > len - count)
  85. throw new ArgumentException ("Reading would overrun buffer");
  86. size = Math.Min (size, count);
  87. Buffer.BlockCopy (this.buffer, this.offset, buffer, off, size);
  88. this.offset += size;
  89. return size;
  90. }
  91. public override int Read ([In,Out] byte[] buffer, int offset, int count)
  92. {
  93. if (disposed)
  94. throw new ObjectDisposedException (typeof (RequestStream).ToString ());
  95. int nread = FillFromBuffer (buffer, offset, count);
  96. if (nread > 0)
  97. return nread;
  98. return base.Read (buffer, offset, count);
  99. }
  100. public override IAsyncResult BeginRead (byte [] buffer, int offset, int count,
  101. AsyncCallback cback, object state)
  102. {
  103. if (disposed)
  104. throw new ObjectDisposedException (typeof (RequestStream).ToString ());
  105. int nread = FillFromBuffer (buffer, offset, count);
  106. if (nread > 0) {
  107. HttpStreamAsyncResult ares = new HttpStreamAsyncResult ();
  108. ares.Buffer = buffer;
  109. ares.Offset = offset;
  110. ares.Count = count;
  111. ares.Callback = cback;
  112. ares.State = state;
  113. ares.SynchRead = nread;
  114. ares.Complete ();
  115. return ares;
  116. }
  117. return base.BeginRead (buffer, offset, count, cback, state);
  118. }
  119. public override int EndRead (IAsyncResult ares)
  120. {
  121. if (disposed)
  122. throw new ObjectDisposedException (typeof (RequestStream).ToString ());
  123. if (ares == null)
  124. throw new ArgumentNullException ("async_result");
  125. if (ares is HttpStreamAsyncResult) {
  126. HttpStreamAsyncResult r = (HttpStreamAsyncResult) ares;
  127. if (!ares.IsCompleted)
  128. ares.AsyncWaitHandle.WaitOne ();
  129. return r.SynchRead;
  130. }
  131. // Close on exception?
  132. return base.EndRead (ares);
  133. }
  134. public override long Seek (long offset, SeekOrigin origin)
  135. {
  136. throw new NotSupportedException ();
  137. }
  138. public override void SetLength (long value)
  139. {
  140. throw new NotSupportedException ();
  141. }
  142. public override void Write (byte[] buffer, int offset, int count)
  143. {
  144. throw new NotSupportedException ();
  145. }
  146. public override IAsyncResult BeginWrite (byte [] buffer, int offset, int count,
  147. AsyncCallback cback, object state)
  148. {
  149. throw new NotSupportedException ();
  150. }
  151. public override void EndWrite (IAsyncResult async_result)
  152. {
  153. throw new NotSupportedException ();
  154. }
  155. }
  156. }
  157. #endif