本文整理汇总了C#中MongoDB.Bson.IO.BsonBinaryReader.ReadEndDocument方法的典型用法代码示例。如果您正苦于以下问题:C# BsonBinaryReader.ReadEndDocument方法的具体用法?C# BsonBinaryReader.ReadEndDocument怎么用?C# BsonBinaryReader.ReadEndDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MongoDB.Bson.IO.BsonBinaryReader
的用法示例。
在下文中一共展示了BsonBinaryReader.ReadEndDocument方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BsonBinaryReader_should_support_reading_multiple_documents
public void BsonBinaryReader_should_support_reading_multiple_documents(
[Range(0, 3)]
int numberOfDocuments)
{
var document = new BsonDocument("x", 1);
var bson = document.ToBson();
var input = Enumerable.Repeat(bson, numberOfDocuments).Aggregate(Enumerable.Empty<byte>(), (a, b) => a.Concat(b)).ToArray();
var expectedResult = Enumerable.Repeat(document, numberOfDocuments);
using (var stream = new MemoryStream(input))
using (var binaryReader = new BsonBinaryReader(stream))
{
var result = new List<BsonDocument>();
while (!binaryReader.IsAtEndOfFile())
{
binaryReader.ReadStartDocument();
var name = binaryReader.ReadName();
var value = binaryReader.ReadInt32();
binaryReader.ReadEndDocument();
var resultDocument = new BsonDocument(name, value);
result.Add(resultDocument);
}
result.Should().Equal(expectedResult);
}
}
示例2: BsonBinaryReader_should_support_reading_more_than_2GB
public void BsonBinaryReader_should_support_reading_more_than_2GB()
{
RequireEnvironmentVariable.IsDefined("EXPLICIT");
var binaryData = new BsonBinaryData(new byte[1024 * 1024]);
var tempFileName = Path.GetTempFileName();
try
{
using (var stream = new FileStream(tempFileName, FileMode.Open))
{
using (var binaryWriter = new BsonBinaryWriter(stream))
{
while (stream.Position < (long)int.MaxValue * 4)
{
binaryWriter.WriteStartDocument();
binaryWriter.WriteName("x");
binaryWriter.WriteBinaryData(binaryData);
binaryWriter.WriteEndDocument();
}
}
var endOfFilePosition = stream.Position;
stream.Position = 0;
using (var binaryReader = new BsonBinaryReader(stream))
{
while (!binaryReader.IsAtEndOfFile())
{
binaryReader.ReadStartDocument();
var bookmark = binaryReader.GetBookmark();
binaryReader.ReadName("x");
binaryReader.ReturnToBookmark(bookmark);
binaryReader.ReadName("x");
var readBinaryData = binaryReader.ReadBinaryData();
Assert.Equal(binaryData.Bytes.Length, readBinaryData.Bytes.Length);
binaryReader.ReadEndDocument();
}
}
Assert.Equal(endOfFilePosition, stream.Position);
}
}
finally
{
try
{
File.Delete(tempFileName);
}
catch
{
// ignore exceptions
}
}
}
示例3: TestHelloWorld
public void TestHelloWorld()
{
string byteString = @"\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00";
byte[] bytes = DecodeByteString(byteString);
var stream = new MemoryStream(bytes);
using (var bsonReader = new BsonBinaryReader(stream))
{
bsonReader.ReadStartDocument();
Assert.AreEqual(BsonType.String, bsonReader.ReadBsonType());
Assert.AreEqual("hello", bsonReader.ReadName());
Assert.AreEqual("world", bsonReader.ReadString());
bsonReader.ReadEndDocument();
}
}
示例4: TestBsonAwesome
public void TestBsonAwesome()
{
string byteString = @"1\x00\x00\x00\x04BSON\x00&\x00\x00\x00\x020\x00\x08\x00\x00\x00awesome\x00\x011\x00333333\[email protected]\x102\x00\xc2\x07\x00\x00\x00\x00";
byte[] bytes = DecodeByteString(byteString);
MemoryStream stream = new MemoryStream(bytes);
using (BsonReader bsonReader = new BsonBinaryReader(stream))
{
bsonReader.ReadStartDocument();
Assert.AreEqual(BsonType.Array, bsonReader.ReadBsonType());
Assert.AreEqual("BSON", bsonReader.ReadName());
bsonReader.ReadStartArray();
Assert.AreEqual(BsonType.String, bsonReader.ReadBsonType());
Assert.AreEqual("awesome", bsonReader.ReadString());
Assert.AreEqual(BsonType.Double, bsonReader.ReadBsonType());
Assert.AreEqual(5.05, bsonReader.ReadDouble());
Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
Assert.AreEqual(1986, bsonReader.ReadInt32());
bsonReader.ReadEndArray();
bsonReader.ReadEndDocument();
}
}
示例5: GetEnumerator
/// <summary>
/// Gets an enumerator that can enumerate the elements of the array.
/// </summary>
/// <returns>An enumerator.</returns>
public override IEnumerator<BsonValue> GetEnumerator()
{
ThrowIfDisposed();
using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
{
bsonReader.ReadStartDocument();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
bsonReader.SkipName();
yield return DeserializeBsonValue(bsonReader);
}
bsonReader.ReadEndDocument();
}
}
示例6: IndexOf
/// <summary>
/// Gets the index of a value in the array.
/// </summary>
/// <param name="value">The value to search for.</param>
/// <param name="index">The zero based index at which to start the search.</param>
/// <param name="count">The number of elements to search.</param>
/// <returns>The zero based index of the value (or -1 if not found).</returns>
public override int IndexOf(BsonValue value, int index, int count)
{
ThrowIfDisposed();
using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
{
var context = BsonDeserializationContext.CreateRoot(bsonReader);
bsonReader.ReadStartDocument();
var i = 0;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
bsonReader.SkipName();
if (i >= index)
{
if (count == 0)
{
return -1;
}
if (DeserializeBsonValue(context).Equals(value))
{
return i;
}
count--;
}
else
{
bsonReader.SkipValue();
}
i++;
}
bsonReader.ReadEndDocument();
return -1;
}
}
示例7: CopyTo
public override void CopyTo(object[] array, int arrayIndex)
{
ThrowIfDisposed();
using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
{
var context = BsonDeserializationContext.CreateRoot(bsonReader);
bsonReader.ReadStartDocument();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
bsonReader.SkipName();
array[arrayIndex++] = DeserializeBsonValue(context).RawValue;
}
bsonReader.ReadEndDocument();
}
}
示例8: ArgumentOutOfRangeException
// public indexers
/// <summary>
/// Gets or sets a value by position.
/// </summary>
/// <param name="index">The position.</param>
/// <returns>The value.</returns>
public override BsonValue this[int index]
{
get
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
ThrowIfDisposed();
using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
{
bsonReader.ReadStartDocument();
var i = 0;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
bsonReader.SkipName();
if (i == index)
{
var context = BsonDeserializationContext.CreateRoot(bsonReader);
return DeserializeBsonValue(context);
}
bsonReader.SkipValue();
i++;
}
bsonReader.ReadEndDocument();
throw new ArgumentOutOfRangeException("index");
}
}
set
{
throw new NotSupportedException("RawBsonArray instances are immutable.");
}
}
示例9: GetElement
/// <summary>
/// Gets an element of this document.
/// </summary>
/// <param name="index">The zero based index of the element.</param>
/// <returns>
/// The element.
/// </returns>
public override BsonElement GetElement(int index)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
ThrowIfDisposed();
using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
{
var context = BsonDeserializationContext.CreateRoot(bsonReader);
bsonReader.ReadStartDocument();
var i = 0;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
if (i == index)
{
var name = bsonReader.ReadName();
var value = DeserializeBsonValue(context);
return new BsonElement(name, value);
}
bsonReader.SkipName();
bsonReader.SkipValue();
i++;
}
bsonReader.ReadEndDocument();
throw new ArgumentOutOfRangeException("index");
}
}
示例10: GetValue
/// <summary>
/// Gets the value of an element.
/// </summary>
/// <param name="index">The zero based index of the element.</param>
/// <returns>
/// The value of the element.
/// </returns>
public override BsonValue GetValue(int index)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException("index");
}
ThrowIfDisposed();
using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
{
bsonReader.ReadStartDocument();
var i = 0;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
bsonReader.SkipName();
if (i == index)
{
return DeserializeBsonValue(bsonReader);
}
bsonReader.SkipValue();
i++;
}
bsonReader.ReadEndDocument();
throw new ArgumentOutOfRangeException("index");
}
}
示例11: MaterializeThisLevel
private IEnumerable<BsonElement> MaterializeThisLevel()
{
var elements = new List<BsonElement>();
using (var stream = new ByteBufferStream(_slice, ownsBuffer: false))
using (var bsonReader = new BsonBinaryReader(stream, _readerSettings))
{
var context = BsonDeserializationContext.CreateRoot(bsonReader);
bsonReader.ReadStartDocument();
BsonType bsonType;
while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
BsonValue value;
switch (bsonType)
{
case BsonType.Array: value = DeserializeLazyBsonArray(bsonReader); break;
case BsonType.Document: value = DeserializeLazyBsonDocument(bsonReader); break;
default: value = BsonValueSerializer.Instance.Deserialize(context); break;
}
elements.Add(new BsonElement(name, value));
}
bsonReader.ReadEndDocument();
}
return elements;
}
示例12: WriteRawBsonArray
/// <summary>
/// Writes a raw BSON array.
/// </summary>
/// <param name="slice">The byte buffer containing the raw BSON array.</param>
public virtual void WriteRawBsonArray(IByteBuffer slice)
{
// overridden in BsonBinaryWriter to write the raw bytes to the stream
// for all other streams, deserialize the raw bytes and serialize the resulting array instead
var documentLength = slice.Length + 8;
using (var memoryStream = new MemoryStream(documentLength))
{
// wrap the array in a fake document so we can deserialize it
var streamWriter = new BsonStreamWriter(memoryStream, Utf8Helper.StrictUtf8Encoding);
streamWriter.WriteInt32(documentLength);
streamWriter.WriteBsonType(BsonType.Array);
streamWriter.WriteByte((byte)'x');
streamWriter.WriteByte(0);
slice.WriteTo(streamWriter.BaseStream);
streamWriter.WriteByte(0);
memoryStream.Position = 0;
using (var bsonReader = new BsonBinaryReader(memoryStream, BsonBinaryReaderSettings.Defaults))
{
var deserializationContext = BsonDeserializationContext.CreateRoot<BsonDocument>(bsonReader);
bsonReader.ReadStartDocument();
bsonReader.ReadName("x");
var array = deserializationContext.DeserializeWithChildContext(BsonArraySerializer.Instance);
bsonReader.ReadEndDocument();
var serializationContext = BsonSerializationContext.CreateRoot<BsonArray>(this);
BsonArraySerializer.Instance.Serialize(serializationContext, array);
}
}
}
示例13: MaterializeThisLevel
private IEnumerable<BsonValue> MaterializeThisLevel()
{
var values = new List<BsonValue>();
var readerSettings = _readerSettings.Clone();
readerSettings.MaxDocumentSize = _slice.Length;
using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, readerSettings))
{
bsonReader.ReadStartDocument();
BsonType bsonType;
while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
{
bsonReader.SkipName();
BsonValue value;
switch (bsonType)
{
case BsonType.Array: value = DeserializeLazyBsonArray(bsonReader); break;
case BsonType.Document: value = DeserializeLazyBsonDocument(bsonReader); break;
default: value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); break;
}
values.Add(value);
}
bsonReader.ReadEndDocument();
}
return values;
}
示例14: MaterializeThisLevel
private IEnumerable<BsonElement> MaterializeThisLevel()
{
var elements = new List<BsonElement>();
using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), true), true, _readerSettings))
{
bsonReader.ReadStartDocument();
BsonType bsonType;
while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument)
{
var name = bsonReader.ReadName();
BsonValue value;
switch (bsonType)
{
case BsonType.Array: value = DeserializeLazyBsonArray(bsonReader); break;
case BsonType.Document: value = DeserializeLazyBsonDocument(bsonReader); break;
default: value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); break;
}
elements.Add(new BsonElement(name, value));
}
bsonReader.ReadEndDocument();
}
return elements;
}
示例15: IndexOf
/// <summary>
/// Gets the index of a value in the array.
/// </summary>
/// <param name="value">The value to search for.</param>
/// <param name="index">The zero based index at which to start the search.</param>
/// <param name="count">The number of elements to search.</param>
/// <returns>The zero based index of the value (or -1 if not found).</returns>
public override int IndexOf(BsonValue value, int index, int count)
{
ThrowIfDisposed();
using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
{
bsonReader.ReadStartDocument();
var i = 0;
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
bsonReader.SkipName();
if (i >= index)
{
if (count == 0)
{
return -1;
}
if (DeserializeBsonValue(bsonReader).Equals(value))
{
return i;
}
count--;
}
else
{
bsonReader.SkipValue();
}
i++;
}
bsonReader.ReadEndDocument();
return -1;
}
}