/// <summary> /// Ctor /// </summary> /// <param name="subtype">binary type code</param> /// <param name="bytes">byte date</param> public BsonBinary(BsonBinarySubtype subtype, byte[] bytes) { if (bytes == null) { throw new ArgumentNullException("bytes"); } this.Subtype = subtype; this.Bytes = bytes; }
private static void ReadBinary(List <Token <ModelTokenType> > tokens, BinaryReader reader) { int size = reader.ReadInt32(); BsonBinarySubtype subtype = (BsonBinarySubtype)reader.ReadByte(); byte[] buffer = reader.ReadBytes(size); object value; switch (subtype) { case BsonBinarySubtype.MD5: { if (size != 16) { goto default; } value = new BsonMD5(buffer); break; } case BsonBinarySubtype.UUID: { if (size != 16) { goto default; } value = new Guid(buffer); break; } case BsonBinarySubtype.BinaryOld: { // Binary (Old): // "The structure of the binary data (this byte* array in the binary non-terminal) must be an int32 followed by a (byte*)." // http://bsonspec.org/#/specification size = BitConverter.ToInt32(buffer, 0); // trim Int32 size off front of array byte[] temp = new byte[size]; Buffer.BlockCopy(buffer, 4, temp, 0, size); // since obsolete, convert to generic value = new BsonBinary(BsonBinarySubtype.Generic, temp); break; } case BsonBinarySubtype.Function: case BsonBinarySubtype.Generic: case BsonBinarySubtype.UserDefined: default: { // TODO: convert Function accordingly value = new BsonBinary(subtype, buffer); break; } } tokens.Add(ModelGrammar.TokenPrimitive(value)); }