JsonBuffer.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. using System.Text;
  18. namespace MongoDB.Bson.IO
  19. {
  20. /// <summary>
  21. /// Represents a wrapper around a TextReader to provide some buffering functionality.
  22. /// </summary>
  23. internal class JsonBuffer
  24. {
  25. // private fields
  26. private readonly StringBuilder _buffer;
  27. private int _position;
  28. private readonly TextReader _reader;
  29. // constructors
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="JsonBuffer"/> class.
  32. /// </summary>
  33. /// <param name="json">The json.</param>
  34. public JsonBuffer(string json)
  35. {
  36. if (json == null)
  37. {
  38. throw new ArgumentNullException("json");
  39. }
  40. _buffer = new StringBuilder(json);
  41. }
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="JsonBuffer" /> class.
  44. /// </summary>
  45. /// <param name="reader">The reader.</param>
  46. public JsonBuffer(TextReader reader)
  47. {
  48. if (reader == null)
  49. {
  50. throw new ArgumentNullException("reader");
  51. }
  52. _buffer = new StringBuilder(256); // start out with a reasonable initial capacity
  53. _reader = reader;
  54. }
  55. // public properties
  56. /// <summary>
  57. /// Gets or sets the current position.
  58. /// </summary>
  59. public int Position
  60. {
  61. get { return _position; }
  62. set
  63. {
  64. if (value < 0 || value > _buffer.Length)
  65. {
  66. var message = string.Format("Invalid position: {0}.", value);
  67. throw new ArgumentOutOfRangeException("value", message);
  68. }
  69. _position = value;
  70. }
  71. }
  72. // public methods
  73. /// <summary>
  74. /// Gets a snippet of a maximum length from the buffer (usually to include in an error message).
  75. /// </summary>
  76. /// <param name="start">The start.</param>
  77. /// <param name="maxLength">The maximum length.</param>
  78. /// <returns>The snippet.</returns>
  79. public string GetSnippet(int start, int maxLength)
  80. {
  81. if (start < 0)
  82. {
  83. throw new ArgumentOutOfRangeException("start", "Start cannot be negative.");
  84. }
  85. if (maxLength < 0)
  86. {
  87. throw new ArgumentOutOfRangeException("maxLength", "MaxLength cannot be negative.");
  88. }
  89. if (start > _position)
  90. {
  91. throw new ArgumentOutOfRangeException("start", "Start is beyond current position.");
  92. }
  93. var availableCount = _position - start;
  94. var count = Math.Min(availableCount, maxLength);
  95. return _buffer.ToString(start, count);
  96. }
  97. /// <summary>
  98. /// Gets a substring from the buffer.
  99. /// </summary>
  100. /// <param name="start">The start.</param>
  101. /// <param name="count">The count.</param>
  102. /// <returns>The substring.</returns>
  103. public string GetSubstring(int start, int count)
  104. {
  105. if (start < 0)
  106. {
  107. throw new ArgumentOutOfRangeException("start", "Start cannot be negative.");
  108. }
  109. if (count < 0)
  110. {
  111. throw new ArgumentOutOfRangeException("count", "Count cannot be negative.");
  112. }
  113. if (start > _position)
  114. {
  115. throw new ArgumentOutOfRangeException("start", "Start is beyond current position.");
  116. }
  117. if (start + count > _position)
  118. {
  119. throw new ArgumentOutOfRangeException("start", "End of substring is beyond current position.");
  120. }
  121. return _buffer.ToString(start, count);
  122. }
  123. /// <summary>
  124. /// Reads the next character from the text reader and advances the character position by one character.
  125. /// </summary>
  126. /// <returns>
  127. /// The next character from the text reader, or -1 if no more characters are available. The default implementation returns -1.
  128. /// </returns>
  129. public int Read()
  130. {
  131. ReadMoreIfAtEndOfBuffer();
  132. return _position >= _buffer.Length ? -1 : _buffer[_position++];
  133. }
  134. /// <summary>
  135. /// Resets the buffer (clears everything up to the current position).
  136. /// </summary>
  137. public void ResetBuffer()
  138. {
  139. // only trim the buffer if enough space will be reclaimed to make it worthwhile
  140. var minimumTrimCount = 256; // TODO: make configurable?
  141. if (_position >= minimumTrimCount)
  142. {
  143. _buffer.Remove(0, _position);
  144. _position = 0;
  145. }
  146. }
  147. /// <summary>
  148. /// Unreads one character (moving the current Position back one position).
  149. /// </summary>
  150. /// <param name="c">The character.</param>
  151. public void UnRead(int c)
  152. {
  153. if (_position == 0)
  154. {
  155. throw new InvalidOperationException("Unread called when nothing has been read.");
  156. }
  157. if (c == -1)
  158. {
  159. if (_position != _buffer.Length)
  160. {
  161. throw new InvalidOperationException("Unread called with -1 when position is not at the end of the buffer.");
  162. }
  163. }
  164. else
  165. {
  166. if (_buffer[_position - 1] != c)
  167. {
  168. throw new InvalidOperationException("Unread called with a character that does not match what is in the buffer.");
  169. }
  170. _position -= 1;
  171. }
  172. }
  173. // private methods
  174. private void ReadMoreIfAtEndOfBuffer()
  175. {
  176. if (_position >= _buffer.Length)
  177. {
  178. if (_reader != null)
  179. {
  180. var blockSize = 1024; // TODO: make configurable?
  181. var block = new char[blockSize];
  182. var actualCount = _reader.ReadBlock(block, 0, blockSize);
  183. if (actualCount > 0)
  184. {
  185. _buffer.Append(block, 0, actualCount);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }