ByteArrayBuffer.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. namespace MongoDB.Bson.IO
  17. {
  18. /// <summary>
  19. /// An IByteBuffer that is backed by a contiguous byte array.
  20. /// </summary>
  21. public sealed class ByteArrayBuffer : IByteBuffer
  22. {
  23. // private fields
  24. private byte[] _bytes;
  25. private bool _disposed;
  26. private bool _isReadOnly;
  27. private int _length;
  28. // constructors
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="ByteArrayBuffer"/> class.
  31. /// </summary>
  32. /// <param name="bytes">The bytes.</param>
  33. /// <param name="isReadOnly">Whether the buffer is read only.</param>
  34. public ByteArrayBuffer(byte[] bytes, bool isReadOnly = false)
  35. : this(bytes, bytes == null ? 0 : bytes.Length, isReadOnly)
  36. {
  37. }
  38. /// <summary>
  39. /// Initializes a new instance of the <see cref="ByteArrayBuffer"/> class.
  40. /// </summary>
  41. /// <param name="bytes">The bytes.</param>
  42. /// <param name="length">The length.</param>
  43. /// <param name="isReadOnly">Whether the buffer is read only.</param>
  44. public ByteArrayBuffer(byte[] bytes, int length, bool isReadOnly = false)
  45. {
  46. if (bytes == null)
  47. {
  48. throw new ArgumentNullException("bytes");
  49. }
  50. if (length < 0 || length > bytes.Length)
  51. {
  52. throw new ArgumentOutOfRangeException("length");
  53. }
  54. _length = length;
  55. _bytes = bytes;
  56. _isReadOnly = isReadOnly;
  57. }
  58. // public properties
  59. /// <inheritdoc/>
  60. public int Capacity
  61. {
  62. get
  63. {
  64. ThrowIfDisposed();
  65. return _isReadOnly ? _length : _bytes.Length;
  66. }
  67. }
  68. /// <inheritdoc/>
  69. public bool IsReadOnly
  70. {
  71. get
  72. {
  73. ThrowIfDisposed();
  74. return _isReadOnly;
  75. }
  76. }
  77. /// <inheritdoc/>
  78. public int Length
  79. {
  80. get
  81. {
  82. ThrowIfDisposed();
  83. return _length;
  84. }
  85. set
  86. {
  87. ThrowIfDisposed();
  88. if (value < 0 || value > _bytes.Length)
  89. {
  90. throw new ArgumentOutOfRangeException("value");
  91. }
  92. EnsureIsWritable();
  93. _length = value;
  94. }
  95. }
  96. // public methods
  97. /// <inheritdoc/>
  98. public ArraySegment<byte> AccessBackingBytes(int position)
  99. {
  100. ThrowIfDisposed();
  101. if (position < 0 || position > _length)
  102. {
  103. throw new ArgumentOutOfRangeException("position");
  104. }
  105. return new ArraySegment<byte>(_bytes, position, _length - position);
  106. }
  107. /// <inheritdoc/>
  108. public void Clear(int position, int count)
  109. {
  110. ThrowIfDisposed();
  111. if (position < 0 || position > _length)
  112. {
  113. throw new ArgumentOutOfRangeException("position");
  114. }
  115. if (count < 0 || position + count > _length)
  116. {
  117. throw new ArgumentOutOfRangeException("count");
  118. }
  119. EnsureIsWritable();
  120. Array.Clear(_bytes, position, count);
  121. }
  122. /// <inheritdoc/>
  123. public void Dispose()
  124. {
  125. _disposed = true;
  126. GC.SuppressFinalize(this);
  127. }
  128. /// <inheritdoc/>
  129. public void EnsureCapacity(int minimumCapacity)
  130. {
  131. if (minimumCapacity < 0)
  132. {
  133. throw new ArgumentOutOfRangeException("minimumCapacity");
  134. }
  135. ThrowIfDisposed();
  136. EnsureIsWritable();
  137. if (minimumCapacity > _bytes.Length)
  138. {
  139. var powerOf2 = Math.Max(32, PowerOf2.RoundUpToPowerOf2(minimumCapacity));
  140. SetCapacity(powerOf2);
  141. }
  142. }
  143. /// <inheritdoc/>
  144. public byte GetByte(int position)
  145. {
  146. ThrowIfDisposed();
  147. if (position < 0 || position > _length)
  148. {
  149. throw new ArgumentOutOfRangeException("position");
  150. }
  151. return _bytes[position];
  152. }
  153. /// <inheritdoc/>
  154. public void GetBytes(int position, byte[] destination, int offset, int count)
  155. {
  156. ThrowIfDisposed();
  157. if (position < 0 || position > _length)
  158. {
  159. throw new ArgumentOutOfRangeException("position");
  160. }
  161. if (destination == null)
  162. {
  163. throw new ArgumentNullException("destination");
  164. }
  165. if (offset < 0 || offset > destination.Length)
  166. {
  167. throw new ArgumentOutOfRangeException("offset");
  168. }
  169. if (count < 0 || position + count > _length || offset + count > destination.Length)
  170. {
  171. throw new ArgumentOutOfRangeException("count");
  172. }
  173. Buffer.BlockCopy(_bytes, position, destination, offset, count);
  174. }
  175. /// <inheritdoc/>
  176. public IByteBuffer GetSlice(int position, int length)
  177. {
  178. ThrowIfDisposed();
  179. if (position < 0 || position > _length)
  180. {
  181. throw new ArgumentOutOfRangeException("position");
  182. }
  183. if (length < 0 || position + length > _length)
  184. {
  185. throw new ArgumentOutOfRangeException("length");
  186. }
  187. EnsureIsReadOnly();
  188. var forkedBuffer = new ByteArrayBuffer(_bytes, _length, isReadOnly: true);
  189. return new ByteBufferSlice(forkedBuffer, position, length);
  190. }
  191. /// <inheritdoc/>
  192. public void MakeReadOnly()
  193. {
  194. ThrowIfDisposed();
  195. _isReadOnly = true;
  196. }
  197. /// <inheritdoc/>
  198. public void SetByte(int position, byte value)
  199. {
  200. ThrowIfDisposed();
  201. if (position < 0 || position > _length)
  202. {
  203. throw new ArgumentOutOfRangeException("position");
  204. }
  205. EnsureIsWritable();
  206. _bytes[position] = value;
  207. }
  208. /// <inheritdoc/>
  209. public void SetBytes(int position, byte[] source, int offset, int count)
  210. {
  211. ThrowIfDisposed();
  212. if (position < 0 || position > _length)
  213. {
  214. throw new ArgumentOutOfRangeException("position");
  215. }
  216. if (source == null)
  217. {
  218. throw new ArgumentNullException("source");
  219. }
  220. if (offset < 0 || offset > source.Length)
  221. {
  222. throw new ArgumentOutOfRangeException("offset");
  223. }
  224. if (count < 0 || position + count > _length || offset + count > source.Length)
  225. {
  226. throw new ArgumentOutOfRangeException("count");
  227. }
  228. EnsureIsWritable();
  229. Buffer.BlockCopy(source, offset, _bytes, position, count);
  230. }
  231. // private methods
  232. private void EnsureIsReadOnly()
  233. {
  234. if (!_isReadOnly)
  235. {
  236. var message = string.Format("{0} is not read only.", GetType().Name);
  237. throw new InvalidOperationException(message);
  238. }
  239. }
  240. private void EnsureIsWritable()
  241. {
  242. if (_isReadOnly)
  243. {
  244. var message = string.Format("{0} is not writable.", GetType().Name);
  245. throw new InvalidOperationException(message);
  246. }
  247. }
  248. private void SetCapacity(int capacity)
  249. {
  250. var oldBytes = _bytes;
  251. _bytes = new byte[capacity];
  252. var bytesToCopy = capacity < oldBytes.Length ? capacity : oldBytes.Length;
  253. Buffer.BlockCopy(oldBytes, 0, _bytes, 0, bytesToCopy);
  254. }
  255. private void ThrowIfDisposed()
  256. {
  257. if (_disposed)
  258. {
  259. throw new ObjectDisposedException(GetType().Name);
  260. }
  261. }
  262. }
  263. }