DelegatingStream.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Copyright 2016-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.IO;
  17. #if NET452
  18. using System.Runtime.Remoting;
  19. #endif
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. namespace MongoDB.Driver.GridFS
  23. {
  24. /// <summary>
  25. /// Represents a Stream that delegates all of its operations to a wrapped Stream.
  26. /// </summary>
  27. /// <seealso cref="System.IO.Stream" />
  28. public class DelegatingStream : Stream
  29. {
  30. // private fields
  31. private readonly Stream _wrappedStream;
  32. // constructors
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="DelegatingStream"/> class.
  35. /// </summary>
  36. /// <param name="wrappedStream">The wrapped stream.</param>
  37. internal DelegatingStream(Stream wrappedStream)
  38. {
  39. _wrappedStream = wrappedStream;
  40. }
  41. // properties
  42. /// <inheritdoc/>
  43. public override bool CanRead
  44. {
  45. get { return _wrappedStream.CanRead; }
  46. }
  47. /// <inheritdoc/>
  48. public override bool CanSeek
  49. {
  50. get { return _wrappedStream.CanSeek; }
  51. }
  52. /// <inheritdoc/>
  53. public override bool CanTimeout
  54. {
  55. get { return _wrappedStream.CanTimeout; }
  56. }
  57. /// <inheritdoc/>
  58. public override bool CanWrite
  59. {
  60. get { return _wrappedStream.CanWrite; }
  61. }
  62. /// <inheritdoc/>
  63. public override long Length
  64. {
  65. get { return _wrappedStream.Length; }
  66. }
  67. /// <inheritdoc/>
  68. public override long Position
  69. {
  70. get { return _wrappedStream.Position; }
  71. set { _wrappedStream.Position = value; }
  72. }
  73. /// <inheritdoc/>
  74. public override int ReadTimeout
  75. {
  76. get { return _wrappedStream.ReadTimeout; }
  77. set { _wrappedStream.ReadTimeout = value; }
  78. }
  79. /// <inheritdoc/>
  80. public override int WriteTimeout
  81. {
  82. get { return _wrappedStream.WriteTimeout; }
  83. set { _wrappedStream.WriteTimeout = value; }
  84. }
  85. // methods
  86. #if NET452
  87. /// <inheritdoc/>
  88. public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
  89. {
  90. return _wrappedStream.BeginRead(buffer, offset, count, callback, state);
  91. }
  92. #endif
  93. #if NET452
  94. /// <inheritdoc/>
  95. public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
  96. {
  97. return _wrappedStream.BeginWrite(buffer, offset, count, callback, state);
  98. }
  99. #endif
  100. #if NET452
  101. /// <inheritdoc/>
  102. public override void Close()
  103. {
  104. _wrappedStream.Close();
  105. }
  106. #endif
  107. /// <inheritdoc/>
  108. public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
  109. {
  110. return _wrappedStream.CopyToAsync(destination, bufferSize, cancellationToken);
  111. }
  112. #if NET452
  113. /// <inheritdoc/>
  114. public override ObjRef CreateObjRef(Type requestedType)
  115. {
  116. return _wrappedStream.CreateObjRef(requestedType);
  117. }
  118. #endif
  119. #if NET452
  120. /// <inheritdoc/>
  121. [Obsolete("Not supported by DelegatingStream.")]
  122. protected override WaitHandle CreateWaitHandle()
  123. {
  124. throw new NotSupportedException();
  125. }
  126. #endif
  127. /// <inheritdoc/>
  128. protected override void Dispose(bool disposing)
  129. {
  130. if (disposing)
  131. {
  132. _wrappedStream.Dispose();
  133. }
  134. }
  135. #if NET452
  136. /// <inheritdoc/>
  137. public override int EndRead(IAsyncResult asyncResult)
  138. {
  139. return _wrappedStream.EndRead(asyncResult);
  140. }
  141. #endif
  142. #if NET452
  143. /// <inheritdoc/>
  144. public override void EndWrite(IAsyncResult asyncResult)
  145. {
  146. _wrappedStream.EndWrite(asyncResult);
  147. }
  148. #endif
  149. /// <inheritdoc/>
  150. public override bool Equals(object obj)
  151. {
  152. return _wrappedStream.Equals(obj);
  153. }
  154. /// <inheritdoc/>
  155. public override void Flush()
  156. {
  157. _wrappedStream.Flush();
  158. }
  159. /// <inheritdoc/>
  160. public override Task FlushAsync(CancellationToken cancellationToken)
  161. {
  162. return _wrappedStream.FlushAsync(cancellationToken);
  163. }
  164. /// <inheritdoc/>
  165. public override int GetHashCode()
  166. {
  167. return _wrappedStream.GetHashCode();
  168. }
  169. #if NET452
  170. /// <inheritdoc/>
  171. public override object InitializeLifetimeService()
  172. {
  173. return _wrappedStream.InitializeLifetimeService();
  174. }
  175. #endif
  176. #if NET452
  177. /// <inheritdoc/>
  178. [Obsolete("Not supported by DelegatingStream.")]
  179. protected override void ObjectInvariant()
  180. {
  181. throw new NotSupportedException();
  182. }
  183. #endif
  184. /// <inheritdoc/>
  185. public override int Read(byte[] buffer, int offset, int count)
  186. {
  187. return _wrappedStream.Read(buffer, offset, count);
  188. }
  189. /// <inheritdoc/>
  190. public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  191. {
  192. return _wrappedStream.ReadAsync(buffer, offset, count, cancellationToken);
  193. }
  194. /// <inheritdoc/>
  195. public override int ReadByte()
  196. {
  197. return _wrappedStream.ReadByte();
  198. }
  199. /// <inheritdoc/>
  200. public override long Seek(long offset, SeekOrigin origin)
  201. {
  202. return _wrappedStream.Seek(offset, origin);
  203. }
  204. /// <inheritdoc/>
  205. public override void SetLength(long value)
  206. {
  207. _wrappedStream.SetLength(value);
  208. }
  209. /// <inheritdoc/>
  210. public override string ToString()
  211. {
  212. return _wrappedStream.ToString();
  213. }
  214. /// <inheritdoc/>
  215. public override void Write(byte[] buffer, int offset, int count)
  216. {
  217. _wrappedStream.Write(buffer, offset, count);
  218. }
  219. /// <inheritdoc/>
  220. public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  221. {
  222. return _wrappedStream.WriteAsync(buffer, offset, count, cancellationToken);
  223. }
  224. /// <inheritdoc/>
  225. public override void WriteByte(byte value)
  226. {
  227. _wrappedStream.WriteByte(value);
  228. }
  229. }
  230. }