本文整理汇总了C#中MongoDB.Bson.IO.BsonBinaryReader类的典型用法代码示例。如果您正苦于以下问题:C# BsonBinaryReader类的具体用法?C# BsonBinaryReader怎么用?C# BsonBinaryReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BsonBinaryReader类属于MongoDB.Bson.IO命名空间,在下文中一共展示了BsonBinaryReader类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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);
}
}
开发者ID:narutoswj,项目名称:mongo-csharp-driver,代码行数:28,代码来源:BsonBinaryReaderTests.cs
示例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);
}
}
开发者ID:Bogdan0x400,项目名称:mongo-csharp-driver,代码行数:26,代码来源:BsonBinaryReaderTests.cs
示例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
}
}
}
开发者ID:RavenZZ,项目名称:MDRelation,代码行数:58,代码来源:BsonBinaryReaderTests.cs
示例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();
}
}
开发者ID:Nakro,项目名称:mongo-csharp-driver,代码行数:9,代码来源:DeleteMessageBinaryEncoderTests.cs
示例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();
}
}
开发者ID:Nakro,项目名称:mongo-csharp-driver,代码行数:9,代码来源:InsertMessageBinaryEncoderTests.cs
示例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();
}
}
开发者ID:Nakro,项目名称:mongo-csharp-driver,代码行数:9,代码来源:BinaryMessageEncoderFactoryTests.cs
示例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();
}
}
开发者ID:Nakro,项目名称:mongo-csharp-driver,代码行数:10,代码来源:GetMoreMessageBinaryEncoderTests.cs
示例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();
}
}
开发者ID:narutoswj,项目名称:mongo-csharp-driver,代码行数:14,代码来源:BsonBinaryReaderTests.cs
示例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();
}
}
开发者ID:narutoswj,项目名称:mongo-csharp-driver,代码行数:21,代码来源:BsonBinaryReaderTests.cs
示例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;
}
}
开发者ID:fir3pho3nixx,项目名称:mongo-csharp-driver,代码行数:27,代码来源:RawBsonArray.cs
示例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();
}
}
开发者ID:fir3pho3nixx,项目名称:mongo-csharp-driver,代码行数:17,代码来源:RawBsonArray.cs
示例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);
}
}
开发者ID:narutoswj,项目名称:mongo-csharp-driver,代码行数:10,代码来源:PartiallyRawBsonDocumentSerializerTests.cs
示例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.");
}
}
开发者ID:fir3pho3nixx,项目名称:mongo-csharp-driver,代码行数:43,代码来源:RawBsonArray.cs
示例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>();
}
}
开发者ID:Nakro,项目名称:mongo-csharp-driver,代码行数:10,代码来源:DeleteMessageBinaryEncoderTests.cs
示例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();
}
}
开发者ID:RoninWest,项目名称:mongo-csharp-driver,代码行数:14,代码来源:RawBsonArray.cs
示例16: TryGetValue
/// <summary>
/// Tries to get the value of an element of this document.
/// </summary>
/// <param name="name">The name of the element.</param>
/// <param name="value">The value of the element.</param>
/// <returns>
/// True if an element with that name was found.
/// </returns>
public override bool TryGetValue(string name, out 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)
{
if (bsonReader.ReadName() == name)
{
value = DeserializeBsonValue(context);
return true;
}
bsonReader.SkipValue();
}
bsonReader.ReadEndDocument();
value = null;
return false;
}
}
开发者ID:fir3pho3nixx,项目名称:mongo-csharp-driver,代码行数:33,代码来源:RawBsonDocument.cs
示例17: DeserializeBsonValue
private BsonValue DeserializeBsonValue(BsonBinaryReader bsonReader)
{
switch (bsonReader.GetCurrentBsonType())
{
case BsonType.Array: return DeserializeRawBsonArray(bsonReader);
case BsonType.Document: return DeserializeRawBsonDocument(bsonReader);
default: return (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null);
}
}
开发者ID:RoninWest,项目名称:mongo-csharp-driver,代码行数:9,代码来源:RawBsonArray.cs
示例18: 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
using (var bsonBuffer = new BsonBuffer())
{
BsonArray array;
// wrap the array in a fake document so we can deserialize it
var arrayLength = slice.Length;
var documentLength = arrayLength + 8;
bsonBuffer.WriteInt32(documentLength);
bsonBuffer.WriteByte((byte)BsonType.Array);
bsonBuffer.WriteByte((byte)'x');
bsonBuffer.WriteByte((byte)0);
bsonBuffer.ByteBuffer.WriteBytes(slice);
bsonBuffer.WriteByte((byte)0);
bsonBuffer.Position = 0;
using (var bsonReader = new BsonBinaryReader(bsonBuffer, true, BsonBinaryReaderSettings.Defaults))
{
bsonReader.ReadStartDocument();
bsonReader.ReadName("x");
array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null);
bsonReader.ReadEndDocument();
}
BsonArraySerializer.Instance.Serialize(this, typeof(BsonArray), array, null);
}
}
开发者ID:einaregilsson,项目名称:mongo-csharp-driver,代码行数:34,代码来源:BsonWriter.cs
示例19: 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();
}
}
开发者ID:RoninWest,项目名称:mongo-csharp-driver,代码行数:18,代码来源:RawBsonArray.cs
示例20: ReadMessage_should_decode_flags_correctly
public void ReadMessage_should_decode_flags_correctly(int flags, bool isUpsert, bool isMulti)
{
var bytes = (byte[])__testMessageBytes.Clone();
bytes[__flagsOffset] = (byte)flags;
using (var stream = new MemoryStream(bytes))
using (var binaryReader = new BsonBinaryReader(stream))
{
var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings);
var message = subject.ReadMessage();
message.IsMulti.Should().Be(isMulti);
message.IsUpsert.Should().Be(isUpsert);
}
}
开发者ID:fir3pho3nixx,项目名称:mongo-csharp-driver,代码行数:14,代码来源:UpdateMessageBinaryEncoderTests.cs
注:本文中的MongoDB.Bson.IO.BsonBinaryReader类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论