ByteBufferSlice.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Copyright 2010-2015 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. namespace MongoDB.Bson.IO
  18. {
  19. /// <summary>
  20. /// Represents a slice of a byte buffer.
  21. /// </summary>
  22. public class ByteBufferSlice : IByteBuffer
  23. {
  24. private readonly IByteBuffer _buffer;
  25. private bool _disposed;
  26. private readonly int _length;
  27. private readonly int _offset;
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="ByteBufferSlice"/> class.
  30. /// </summary>
  31. /// <param name="buffer">The byte buffer.</param>
  32. /// <param name="offset">The offset of the slice.</param>
  33. /// <param name="length">The length of the slice.</param>
  34. public ByteBufferSlice(IByteBuffer buffer, int offset, int length)
  35. {
  36. if (buffer == null)
  37. {
  38. throw new ArgumentNullException("buffer");
  39. }
  40. if (!buffer.IsReadOnly)
  41. {
  42. throw new ArgumentException("The buffer is not read only.", "buffer");
  43. }
  44. if (offset < 0 || offset > buffer.Length)
  45. {
  46. throw new ArgumentOutOfRangeException("offset");
  47. }
  48. if (length < 0 || offset + length > buffer.Length)
  49. {
  50. throw new ArgumentOutOfRangeException("length");
  51. }
  52. _buffer = buffer;
  53. _offset = offset;
  54. _length = length;
  55. }
  56. /// <summary>
  57. /// Gets the buffer.
  58. /// </summary>
  59. /// <value>
  60. /// The buffer.
  61. /// </value>
  62. public IByteBuffer Buffer
  63. {
  64. get
  65. {
  66. ThrowIfDisposed();
  67. return _buffer;
  68. }
  69. }
  70. /// <inheritdoc/>
  71. public int Capacity
  72. {
  73. get
  74. {
  75. ThrowIfDisposed();
  76. return _length;
  77. }
  78. }
  79. /// <inheritdoc/>
  80. public bool IsReadOnly
  81. {
  82. get
  83. {
  84. ThrowIfDisposed();
  85. return true;
  86. }
  87. }
  88. /// <inheritdoc/>
  89. public int Length
  90. {
  91. get
  92. {
  93. ThrowIfDisposed();
  94. return _length;
  95. }
  96. set
  97. {
  98. throw new NotSupportedException();
  99. }
  100. }
  101. /// <inheritdoc/>
  102. public ArraySegment<byte> AccessBackingBytes(int position)
  103. {
  104. EnsureValidPosition(position);
  105. ThrowIfDisposed();
  106. var segment = _buffer.AccessBackingBytes(position + _offset);
  107. var count = Math.Min(segment.Count, _length - position);
  108. return new ArraySegment<byte>(segment.Array, segment.Offset, count);
  109. }
  110. /// <inheritdoc/>
  111. public void Clear(int position, int count)
  112. {
  113. EnsureValidPositionAndCount(position, count);
  114. ThrowIfDisposed();
  115. _buffer.Clear(position + _offset, count);
  116. }
  117. /// <inheritdoc/>
  118. public void Dispose()
  119. {
  120. Dispose(true);
  121. GC.SuppressFinalize(this);
  122. }
  123. /// <inheritdoc/>
  124. public void EnsureCapacity(int minimumCapacity)
  125. {
  126. throw new NotSupportedException();
  127. }
  128. /// <inheritdoc/>
  129. public byte GetByte(int position)
  130. {
  131. EnsureValidPosition(position);
  132. ThrowIfDisposed();
  133. return _buffer.GetByte(position + _offset);
  134. }
  135. /// <inheritdoc/>
  136. public void GetBytes(int position, byte[] destination, int offset, int count)
  137. {
  138. EnsureValidPositionAndCount(position, count);
  139. ThrowIfDisposed();
  140. _buffer.GetBytes(position + _offset, destination, offset, count);
  141. }
  142. /// <inheritdoc/>
  143. public IByteBuffer GetSlice(int position, int length)
  144. {
  145. EnsureValidPositionAndLength(position, length);
  146. ThrowIfDisposed();
  147. return _buffer.GetSlice(position + _offset, length);
  148. }
  149. /// <inheritdoc/>
  150. public void MakeReadOnly()
  151. {
  152. ThrowIfDisposed();
  153. }
  154. /// <inheritdoc/>
  155. public void SetByte(int position, byte value)
  156. {
  157. throw new NotSupportedException();
  158. }
  159. /// <inheritdoc/>
  160. public void SetBytes(int position, byte[] source, int offset, int count)
  161. {
  162. throw new NotSupportedException();
  163. }
  164. /// <summary>
  165. /// Releases unmanaged and - optionally - managed resources.
  166. /// </summary>
  167. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  168. protected virtual void Dispose(bool disposing)
  169. {
  170. if (disposing && !_disposed)
  171. {
  172. _buffer.Dispose();
  173. }
  174. _disposed = true;
  175. }
  176. private void EnsureValidPosition(int position)
  177. {
  178. if (position < 0 || position > _length)
  179. {
  180. throw new ArgumentOutOfRangeException("position");
  181. }
  182. }
  183. private void EnsureValidPositionAndCount(int position, int count)
  184. {
  185. EnsureValidPosition(position);
  186. if (count < 0 || position + count > _length)
  187. {
  188. throw new ArgumentOutOfRangeException("count");
  189. }
  190. }
  191. private void EnsureValidPositionAndLength(int position, int length)
  192. {
  193. EnsureValidPosition(position);
  194. if (length < 0 || position + length > _length)
  195. {
  196. throw new ArgumentOutOfRangeException("length");
  197. }
  198. }
  199. private void ThrowIfDisposed()
  200. {
  201. if (_disposed)
  202. {
  203. throw new ObjectDisposedException(GetType().Name);
  204. }
  205. }
  206. }
  207. }