本文整理汇总了C#中MongoDB.Bson.IO.BsonBinaryReader类的典型用法代码示例。如果您正苦于以下问题:C# BsonBinaryReader类的具体用法?C# BsonBinaryReader怎么用?C# BsonBinaryReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BsonBinaryReader类属于MongoDB.Bson.IO命名空间,在下文中一共展示了BsonBinaryReader类的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: TestIsAtEndOfFileWithTwoDocuments
public void TestIsAtEndOfFileWithTwoDocuments()
{
var expected = new BsonDocument("x", 1);
byte[] bson;
using (var stream = new MemoryStream())
using (var writer = new BsonBinaryWriter(stream))
{
BsonSerializer.Serialize(writer, expected);
BsonSerializer.Serialize(writer, expected);
bson = stream.ToArray();
}
using (var stream = new MemoryStream(bson))
using (var reader = new BsonBinaryReader(stream))
{
var count = 0;
while (!reader.IsAtEndOfFile())
{
var document = BsonSerializer.Deserialize<BsonDocument>(reader);
Assert.AreEqual(expected, document);
count++;
}
Assert.AreEqual(2, count);
}
}
示例3: 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
}
}
}
示例4: Constructor_should_not_throw_if_only_binaryReader_is_provided
public void Constructor_should_not_throw_if_only_binaryReader_is_provided()
{
using (var stream = new MemoryStream())
using (var binaryReader = new BsonBinaryReader(stream))
{
Action action = () => new DeleteMessageBinaryEncoder(binaryReader, null);
action.ShouldNotThrow();
}
}
示例5: Constructor_should_not_throw_if_only_binaryReader_is_provided
public void Constructor_should_not_throw_if_only_binaryReader_is_provided()
{
using (var stream = new MemoryStream())
using (var binaryReader = new BsonBinaryReader(stream))
{
Action action = () => new InsertMessageBinaryEncoder<BsonDocument>(binaryReader, null, __serializer);
action.ShouldNotThrow();
}
}
示例6: Constructor_with_two_parameters_should_not_throw_if_only_binaryReader_is_provided
public void Constructor_with_two_parameters_should_not_throw_if_only_binaryReader_is_provided()
{
using (var stream = new MemoryStream())
using (var binaryReader = new BsonBinaryReader(stream))
{
Action action = () => new BinaryMessageEncoderFactory(binaryReader, null);
action.ShouldNotThrow();
}
}
示例7: Constructor_should_not_throw_if_binaryReader_and_binaryWriter_are_both_provided
public void Constructor_should_not_throw_if_binaryReader_and_binaryWriter_are_both_provided()
{
using (var stream = new MemoryStream())
using (var binaryReader = new BsonBinaryReader(stream))
using (var binaryWriter = new BsonBinaryWriter(stream))
{
Action action = () => new GetMoreMessageBinaryEncoder(binaryReader, binaryWriter);
action.ShouldNotThrow();
}
}
示例8: 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();
}
}
示例9: 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);
var stream = new MemoryStream(bytes);
using (var 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();
}
}
示例10: Contains
/// <summary>
/// Tests whether the array contains a value.
/// </summary>
/// <param name="value">The value to test for.</param>
/// <returns>True if the array contains the value.</returns>
public override bool Contains(BsonValue value)
{
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();
if (DeserializeBsonValue(context).Equals(value))
{
return true;
}
}
bsonReader.ReadEndDocument();
return false;
}
}
示例11: 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();
}
}
示例12: Deserialize
// private methods
private BsonDocument Deserialize(byte[] bson, PartiallyRawBsonDocumentSerializer serializer)
{
using (var stream = new MemoryStream(bson))
using (var reader = new BsonBinaryReader(stream))
{
var context = BsonDeserializationContext.CreateRoot(reader);
return serializer.Deserialize(context);
}
}
示例13: 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.");
}
}
示例14: WriteMessage_should_throw_if_binaryWriter_was_not_provided
public void WriteMessage_should_throw_if_binaryWriter_was_not_provided()
{
using (var stream = new MemoryStream())
using (var binaryReader = new BsonBinaryReader(stream))
{
var subject = new DeleteMessageBinaryEncoder(binaryReader, null);
Action action = () => subject.WriteMessage(__testMessage);
action.ShouldThrow<InvalidOperationException>();
}
}
示例15: CopyTo
public override void CopyTo(object[] array, int arrayIndex)
{
ThrowIfDisposed();
using (var bsonReader = new BsonBinaryReader(new BsonBuffer(CloneSlice(), false), true, _readerSettings))
{
bsonReader.ReadStartDocument();
while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
{
bsonReader.SkipName();
array[arrayIndex++] = DeserializeBsonValue(bsonReader).RawValue;
}
bsonReader.ReadEndDocument();
}
}