IByteBuffer.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright 2010-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. namespace MongoDB.Bson.IO
  18. {
  19. /// <summary>
  20. /// Represents a byte buffer (backed by various means depending on the implementation).
  21. /// </summary>
  22. public interface IByteBuffer : IDisposable
  23. {
  24. // properties
  25. /// <summary>
  26. /// Gets the capacity.
  27. /// </summary>
  28. /// <value>
  29. /// The capacity.
  30. /// </value>
  31. int Capacity { get; }
  32. /// <summary>
  33. /// Gets a value indicating whether this instance is read only.
  34. /// </summary>
  35. /// <value>
  36. /// <c>true</c> if this instance is read only; otherwise, <c>false</c>.
  37. /// </value>
  38. bool IsReadOnly { get; }
  39. /// <summary>
  40. /// Gets or sets the length.
  41. /// </summary>
  42. /// <value>
  43. /// The length.
  44. /// </value>
  45. int Length { get; set; }
  46. // methods
  47. /// <summary>
  48. /// Access the backing bytes directly. The returned ArraySegment will point to the desired position and contain
  49. /// as many bytes as possible up to the next chunk boundary (if any). If the returned ArraySegment does not
  50. /// contain enough bytes for your needs you will have to call ReadBytes instead.
  51. /// </summary>
  52. /// <param name="position">The position.</param>
  53. /// <returns>
  54. /// An ArraySegment pointing directly to the backing bytes for the position.
  55. /// </returns>
  56. ArraySegment<byte> AccessBackingBytes(int position);
  57. /// <summary>
  58. /// Clears the specified bytes.
  59. /// </summary>
  60. /// <param name="position">The position.</param>
  61. /// <param name="count">The count.</param>
  62. void Clear(int position, int count);
  63. /// <summary>
  64. /// Ensure that the buffer has a minimum capacity. Depending on the buffer allocation strategy
  65. /// calling this method may result in a higher capacity than the minimum (but never lower).
  66. /// </summary>
  67. /// <param name="minimumCapacity">The minimum capacity.</param>
  68. void EnsureCapacity(int minimumCapacity);
  69. /// <summary>
  70. /// Gets a slice of this buffer.
  71. /// </summary>
  72. /// <param name="position">The position of the start of the slice.</param>
  73. /// <param name="length">The length of the slice.</param>
  74. /// <returns>A slice of this buffer.</returns>
  75. IByteBuffer GetSlice(int position, int length);
  76. /// <summary>
  77. /// Makes this buffer read only.
  78. /// </summary>
  79. void MakeReadOnly();
  80. /// <summary>
  81. /// Gets a byte.
  82. /// </summary>
  83. /// <param name="position">The position.</param>
  84. /// <returns>A byte.</returns>
  85. byte GetByte(int position);
  86. /// <summary>
  87. /// Gets bytes.
  88. /// </summary>
  89. /// <param name="position">The position.</param>
  90. /// <param name="destination">The destination.</param>
  91. /// <param name="offset">The destination offset.</param>
  92. /// <param name="count">The count.</param>
  93. void GetBytes(int position, byte[] destination, int offset, int count);
  94. /// <summary>
  95. /// Sets a byte.
  96. /// </summary>
  97. /// <param name="position">The position.</param>
  98. /// <param name="value">The value.</param>
  99. void SetByte(int position, byte value);
  100. /// <summary>
  101. /// Sets bytes.
  102. /// </summary>
  103. /// <param name="position">The position.</param>
  104. /// <param name="source">The bytes.</param>
  105. /// <param name="offset">The offset.</param>
  106. /// <param name="count">The count.</param>
  107. void SetBytes(int position, byte[] source, int offset, int count);
  108. }
  109. }