SingleChunkBuffer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright 2010-2014 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 IBsonBuffer that only has a single chunk.
  20. /// </summary>
  21. public class SingleChunkBuffer : ByteArrayBuffer
  22. {
  23. // private fields
  24. private BsonChunk _chunk;
  25. // constructors
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="SingleChunkBuffer"/> class.
  28. /// </summary>
  29. /// <param name="chunk">The chunk.</param>
  30. /// <param name="sliceOffset">The slice offset.</param>
  31. /// <param name="length">The length.</param>
  32. /// <param name="isReadOnly">Whether the buffer is read only.</param>
  33. public SingleChunkBuffer(BsonChunk chunk, int sliceOffset, int length, bool isReadOnly)
  34. : base(GetChunkBytes(chunk), sliceOffset, length, isReadOnly)
  35. {
  36. _chunk = chunk;
  37. chunk.IncrementReferenceCount();
  38. }
  39. // public methods
  40. /// <summary>
  41. /// Gets a slice of this buffer.
  42. /// </summary>
  43. /// <param name="position">The position of the start of the slice.</param>
  44. /// <param name="length">The length of the slice.</param>
  45. /// <returns>
  46. /// A slice of this buffer.
  47. /// </returns>
  48. /// <exception cref="System.ObjectDisposedException">SingleChunkBuffer</exception>
  49. /// <exception cref="System.InvalidOperationException">GetSlice can only be called for read only buffers.</exception>
  50. /// <exception cref="System.ArgumentOutOfRangeException">
  51. /// position
  52. /// or
  53. /// length
  54. /// </exception>
  55. public override IByteBuffer GetSlice(int position, int length)
  56. {
  57. ThrowIfDisposed();
  58. EnsureIsReadOnly();
  59. if (position < 0 || position >= Length)
  60. {
  61. throw new ArgumentOutOfRangeException("position");
  62. }
  63. if (length <= 0 || length > Length - position)
  64. {
  65. throw new ArgumentOutOfRangeException("length");
  66. }
  67. return new SingleChunkBuffer(_chunk, SliceOffset + position, length, true);
  68. }
  69. // protected methods
  70. /// <summary>
  71. /// Clears this instance.
  72. /// </summary>
  73. /// <exception cref="System.ObjectDisposedException">SingleChunkBuffer</exception>
  74. /// <exception cref="System.InvalidOperationException">Write operations are not allowed for read only buffers.</exception>
  75. public override void Clear()
  76. {
  77. ThrowIfDisposed();
  78. EnsureIsWritable();
  79. Length = 0;
  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 override void Dispose(bool disposing)
  86. {
  87. if (!Disposed)
  88. {
  89. if (disposing)
  90. {
  91. if (_chunk != null)
  92. {
  93. _chunk.DecrementReferenceCount();
  94. _chunk = null;
  95. }
  96. }
  97. }
  98. base.Dispose(disposing);
  99. }
  100. // private static methods
  101. private static byte[] GetChunkBytes(BsonChunk chunk)
  102. {
  103. if (chunk == null)
  104. {
  105. throw new ArgumentNullException("chunk");
  106. }
  107. return chunk.Bytes;
  108. }
  109. }
  110. }