IByteBuffer.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. 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 or sets the capacity.
  27. /// </summary>
  28. /// <value>
  29. /// The capacity.
  30. /// </value>
  31. int Capacity { get; set; }
  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. /// <summary>
  47. /// Gets or sets the position.
  48. /// </summary>
  49. /// <value>
  50. /// The position.
  51. /// </value>
  52. int Position { get; set; }
  53. // methods
  54. /// <summary>
  55. /// Clears this instance.
  56. /// </summary>
  57. void Clear();
  58. /// <summary>
  59. /// Finds the next null byte.
  60. /// </summary>
  61. /// <returns>The position of the next null byte.</returns>
  62. int FindNullByte();
  63. /// <summary>
  64. /// Gets a slice of this buffer.
  65. /// </summary>
  66. /// <param name="position">The position of the start of the slice.</param>
  67. /// <param name="length">The length of the slice.</param>
  68. /// <returns>A slice of this buffer.</returns>
  69. IByteBuffer GetSlice(int position, int length);
  70. /// <summary>
  71. /// Loads the buffer from a stream.
  72. /// </summary>
  73. /// <param name="stream">The stream.</param>
  74. /// <param name="count">The count.</param>
  75. void LoadFrom(Stream stream, int count);
  76. /// <summary>
  77. /// Makes this buffer read only.
  78. /// </summary>
  79. void MakeReadOnly();
  80. /// <summary>
  81. /// Read directly from the backing bytes. The returned ArraySegment points directly to the backing bytes for
  82. /// the current position and you can read the bytes directly from there. If the backing bytes happen to span
  83. /// a chunk boundary shortly after the current position there might not be enough bytes left in the current
  84. /// chunk in which case the returned ArraySegment will have a Count of zero and you should call ReadBytes instead.
  85. ///
  86. /// When ReadBackingBytes returns the position will have been advanced by count bytes *if and only if* there
  87. /// were count bytes left in the current chunk.
  88. /// </summary>
  89. /// <param name="count">The number of bytes you need to read.</param>
  90. /// <returns>An ArraySegment pointing directly to the backing bytes for the current position.</returns>
  91. ArraySegment<byte> ReadBackingBytes(int count);
  92. /// <summary>
  93. /// Reads a byte.
  94. /// </summary>
  95. /// <returns>A byte.</returns>
  96. byte ReadByte();
  97. /// <summary>
  98. /// Reads bytes.
  99. /// </summary>
  100. /// <param name="destination">The destination.</param>
  101. /// <param name="destinationOffset">The destination offset.</param>
  102. /// <param name="count">The count.</param>
  103. void ReadBytes(byte[] destination, int destinationOffset, int count);
  104. /// <summary>
  105. /// Reads bytes.
  106. /// </summary>
  107. /// <param name="count">The count.</param>
  108. /// <returns>The bytes.</returns>
  109. byte[] ReadBytes(int count);
  110. /// <summary>
  111. /// Write directly to the backing bytes. The returned ArraySegment points directly to the backing bytes for
  112. /// the current position and you can write the bytes directly to there. If the backing bytes happen to span
  113. /// a chunk boundary shortly after the current position there might not be enough bytes left in the current
  114. /// chunk in which case the returned ArraySegment will have a Count of zero and you should call WriteBytes instead.
  115. ///
  116. /// When WriteBackingBytes returns the position has not been advanced. After you have written up to count
  117. /// bytes directly to the backing bytes advance the position by the number of bytes actually written.
  118. /// </summary>
  119. /// <param name="count">The count.</param>
  120. /// <returns>An ArraySegment pointing directly to the backing bytes for the current position.</returns>
  121. ArraySegment<byte> WriteBackingBytes(int count);
  122. /// <summary>
  123. /// Writes a byte.
  124. /// </summary>
  125. /// <param name="source">The byte.</param>
  126. void WriteByte(byte source);
  127. /// <summary>
  128. /// Writes bytes.
  129. /// </summary>
  130. /// <param name="source">The bytes (in the form of a byte array).</param>
  131. void WriteBytes(byte[] source);
  132. /// <summary>
  133. /// Writes bytes.
  134. /// </summary>
  135. /// <param name="source">The bytes (in the form of an IByteBuffer).</param>
  136. void WriteBytes(IByteBuffer source);
  137. /// <summary>
  138. /// Writes Length bytes from this buffer starting at Position 0 to a stream.
  139. /// </summary>
  140. /// <param name="stream">The stream.</param>
  141. void WriteTo(Stream stream);
  142. }
  143. }