ByteArrayChunk.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright 2013-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. /// Represents a chunk backed by a byte array.
  20. /// </summary>
  21. public class ByteArrayChunk : IBsonChunk
  22. {
  23. #region static
  24. private static byte[] CreateByteArray(int size)
  25. {
  26. if (size < 0)
  27. {
  28. throw new ArgumentOutOfRangeException("size");
  29. }
  30. return new byte[size];
  31. }
  32. #endregion
  33. // fields
  34. private byte[] _bytes;
  35. private bool _disposed;
  36. // constructors
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="ByteArrayChunk"/> class.
  39. /// </summary>
  40. /// <param name="size">The size.</param>
  41. public ByteArrayChunk(int size)
  42. : this(CreateByteArray(size))
  43. {
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="ByteArrayChunk"/> class.
  47. /// </summary>
  48. /// <param name="bytes">The bytes.</param>
  49. /// <exception cref="System.ArgumentNullException">bytes</exception>
  50. public ByteArrayChunk(byte[] bytes)
  51. {
  52. if (bytes == null)
  53. {
  54. throw new ArgumentNullException("bytes");
  55. }
  56. _bytes = bytes;
  57. }
  58. // properties
  59. /// <inheritdoc/>
  60. public ArraySegment<byte> Bytes
  61. {
  62. get
  63. {
  64. ThrowIfDisposed();
  65. return new ArraySegment<byte>(_bytes);
  66. }
  67. }
  68. // methods
  69. /// <inheritdoc/>
  70. public void Dispose()
  71. {
  72. Dispose(true);
  73. GC.SuppressFinalize(this);
  74. }
  75. /// <inheritdoc/>
  76. public IBsonChunk Fork()
  77. {
  78. ThrowIfDisposed();
  79. return new ByteArrayChunk(_bytes);
  80. }
  81. /// <summary>
  82. /// Releases unmanaged and - optionally - managed resources.
  83. /// </summary>
  84. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  85. protected virtual void Dispose(bool disposing)
  86. {
  87. if (disposing)
  88. {
  89. _bytes = null;
  90. }
  91. _disposed = true;
  92. }
  93. private void ThrowIfDisposed()
  94. {
  95. if (_disposed)
  96. {
  97. throw new ObjectDisposedException(GetType().Name);
  98. }
  99. }
  100. }
  101. }