/// <summary> /// Writes a the bytes for a CS string in BSON format /// </summary> public static byte[] AsCString(string value) { var stream = new DynamicStream(); stream.Append(AsString(value)); stream.InsertAt(0, BitConverter.GetBytes(stream.Length)); return stream.ToArray(); }
/// <summary> /// Writes a binary object to the Mongo database /// </summary> public static byte[] AsBinary(byte[] value) { var stream = new DynamicStream(4); //write the kinds of stream this is //for now default to User-Defined stream.Append((byte)MongoBinaryTypes.UserDefined); //write the bytes and update the length stream.Append(value); stream.WriteAt(0, BitConverter.GetBytes(value.Length)); //and return the final bytes to use return stream.ToArray(); }
/// <summary> /// Renders the bytes required to create a document /// </summary> public override byte[] ToBsonByteArray() { //create the default size DynamicStream stream = new DynamicStream(5); //generate the bytes stream.InsertAt(4, base.ToBsonByteArray()); //update the length stream.WriteAt(0, BsonTranslator.AsInt32(stream.Length)); //and return the bytes to use return stream.ToArray(); }
/// <summary> /// Creates the bytes for a regular string in BSON format /// </summary> public static byte[] AsString(string value) { var stream = new DynamicStream(); stream.Append(Encoding.UTF8.GetBytes(value)); stream.Append(0); return stream.ToArray(); }
/// <summary> /// Writes a regex in BSON format /// </summary> /// <param name="value">The regex instance</param> /// <returns>regex as bson byte arrray</returns> public static byte[] AsRegex(Regex value) { var optionsStr = ""; var flags = value.Options; if ((flags & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase) optionsStr += "i"; if ((flags & RegexOptions.CultureInvariant) == RegexOptions.CultureInvariant) optionsStr += "l"; if ((flags & RegexOptions.Multiline) == RegexOptions.Multiline) optionsStr += "m"; if ((flags & RegexOptions.Singleline) == RegexOptions.Singleline) optionsStr += "s"; if ((flags & RegexOptions.IgnorePatternWhitespace) == RegexOptions.IgnorePatternWhitespace) optionsStr += "x"; var stream = new DynamicStream(); stream.Append(AsString(value.ToString())); stream.Append(AsString(optionsStr)); return stream.ToArray(); }