本文整理汇总了C#中BsonReader.ReadBytes方法的典型用法代码示例。如果您正苦于以下问题:C# BsonReader.ReadBytes方法的具体用法?C# BsonReader.ReadBytes怎么用?C# BsonReader.ReadBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BsonReader
的用法示例。
在下文中一共展示了BsonReader.ReadBytes方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Deserialize
// public methods
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(BitArray));
BsonType bsonType = bsonReader.GetCurrentBsonType();
BitArray bitArray;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
return new BitArray(bsonReader.ReadBytes());
case BsonType.Document:
bsonReader.ReadStartDocument();
var length = bsonReader.ReadInt32("Length");
var bytes = bsonReader.ReadBytes("Bytes");
bsonReader.ReadEndDocument();
bitArray = new BitArray(bytes);
bitArray.Length = length;
return bitArray;
case BsonType.String:
var s = bsonReader.ReadString();
bitArray = new BitArray(s.Length);
for (int i = 0; i < s.Length; i++)
{
var c = s[i];
switch (c)
{
case '0':
break;
case '1':
bitArray[i] = true;
break;
default:
throw new FileFormatException("String value is not a valid BitArray.");
}
}
return bitArray;
default:
var message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例2: Deserialize
// public methods
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(sbyte));
var bsonType = bsonReader.GetCurrentBsonType();
var lostData = false;
sbyte value;
switch (bsonType)
{
case BsonType.Binary:
var bytes = bsonReader.ReadBytes();
if (bytes.Length != 1)
{
throw new FileFormatException("Binary data for SByte must be exactly one byte long.");
}
value = (sbyte)bytes[0];
break;
case BsonType.Int32:
var int32Value = bsonReader.ReadInt32();
value = (sbyte)int32Value;
lostData = (int)value != int32Value;
break;
case BsonType.Int64:
var int64Value = bsonReader.ReadInt64();
value = (sbyte)int64Value;
lostData = (int)value != int64Value;
break;
case BsonType.String:
var s = bsonReader.ReadString();
if (s.Length == 1)
{
s = "0" + s;
}
value = (sbyte)byte.Parse(s, NumberStyles.HexNumber);
break;
default:
var message = string.Format("Cannot deserialize SByte from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
if (lostData)
{
var message = string.Format("Data loss occurred when trying to convert from {0} to SByte.", bsonType);
throw new FileFormatException(message);
}
return value;
}
示例3: Deserialize
// public methods
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
/// <summary>
/// Deserializes an object from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the object.</param>
/// <param name="actualType">The actual type of the object.</param>
/// <param name="options">The serialization options.</param>
/// <returns>An object.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
VerifyTypes(nominalType, actualType, typeof(byte[]));
BsonType bsonType = bsonReader.GetCurrentBsonType();
byte[] bytes;
string message;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
bytes = bsonReader.ReadBytes();
return bytes;
case BsonType.String:
var s = bsonReader.ReadString();
if ((s.Length % 2) != 0)
{
s = "0" + s; // prepend a zero to make length even
}
bytes = new byte[s.Length / 2];
for (int i = 0; i < s.Length; i += 2)
{
var hex = s.Substring(i, 2);
var b = byte.Parse(hex, NumberStyles.HexNumber);
bytes[i / 2] = b;
}
return bytes;
default:
message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType);
throw new FileFormatException(message);
}
}
示例4: Deserialize
// public methods
/// <summary>
/// Deserializes an Bitmap from a BsonReader.
/// </summary>
/// <param name="bsonReader">The BsonReader.</param>
/// <param name="nominalType">The nominal type of the Bitmap.</param>
/// <param name="actualType">The actual type of the Bitmap.</param>
/// <param name="options">The serialization options.</param>
/// <returns>A Bitmap.</returns>
public override object Deserialize(
BsonReader bsonReader,
Type nominalType,
Type actualType,
IBsonSerializationOptions options)
{
if (nominalType != typeof(Image) && nominalType != typeof(Bitmap))
{
var message = string.Format("Nominal type must be Image or Bitmap, not {0}.", nominalType.FullName);
throw new ArgumentException(message, "nominalType");
}
if (actualType != typeof(Bitmap))
{
var message = string.Format("Actual type must be Bitmap, not {0}.", actualType.FullName);
throw new ArgumentException(message, "actualType");
}
var bsonType = bsonReader.GetCurrentBsonType();
byte[] bytes;
switch (bsonType)
{
case BsonType.Null:
bsonReader.ReadNull();
return null;
case BsonType.Binary:
bytes = bsonReader.ReadBytes();
break;
case BsonType.Document:
bsonReader.ReadStartDocument();
bsonReader.ReadString("_t");
bytes = bsonReader.ReadBytes("bitmap");
bsonReader.ReadEndDocument();
break;
default:
var message = string.Format("BsonType must be Null, Binary or Document, not {0}.", bsonType);
throw new FileFormatException(message);
}
var stream = new MemoryStream(bytes);
return new Bitmap(stream);
}