예제 #1
0
 /// <summary>
 /// Writes this document using the specified EasyWriter.
 /// </summary>
 /// <param name="writer">The writer that will be used to write this document.</param>
 internal void Write(EasyWriter writer)
 {
     _stringTableIndex = 0;
     _stringTable.Clear();
     WriteItem(writer, Top, null, true);
     writer.Close();
 }
예제 #2
0
        /// <summary>
        /// Writes this BSON document to the specified path.
        /// </summary>
        /// <param name="path">The path to write to.</param>
        public void Write(string path, bool includeStringTable = false)
        {
            var stream = File.Open(path, FileMode.Create);
            var writer = new EasyWriter(stream);

            Write(writer);
            writer.Close();
            stream.Close();
        }
예제 #3
0
        public byte[] GenerateStringTable()
        {
            var stream = new MemoryStream();
            var writer = new EasyWriter(stream);

            writer.Write(_stringTable.Count);
            foreach (string key in _stringTable.Keys)
            {
                writer.Write(_stringTable[key]);
                writer.Write(key, Encoding.UTF8);
            }
            writer.Close();
            stream.Close();
            return(stream.ToArray());
        }
예제 #4
0
 /// <summary>
 /// Writes this BSON document to the specified path.
 /// </summary>
 /// <param name="path">The path to write to.</param>
 public void Write(string path, bool includeStringTable = false)
 {
     var stream = File.Open(path, FileMode.Create);
     var writer = new EasyWriter(stream);
     Write(writer);
     writer.Close();
     stream.Close();
 }
예제 #5
0
 public byte[] GenerateStringTable()
 {
     var stream = new MemoryStream();
     var writer = new EasyWriter(stream);
     writer.Write(_stringTable.Count);
     foreach(string key in _stringTable.Keys)
     {
         writer.Write(_stringTable[key]);
         writer.Write(key, Encoding.UTF8);
     }
     writer.Close();
     stream.Close();
     return stream.ToArray();
 }
예제 #6
0
        private void WriteItem(EasyWriter writer, BsonItem item, string name, bool isTop = false, bool isArray = false)
        {
            if(!isTop)
            {
                writer.Write(item.Type);
                writer.Write(isArray ? name : GetKeyName(name), Encoding.UTF8, true);
            }

            if (item.HasValues) // object or array - we need to write a document
            {
                var stream = new MemoryStream();
                var vWriter = new EasyWriter(stream);
                foreach(string key in item.Keys)
                    WriteItem(vWriter, item[key], key, false, item.IsArray);
                vWriter.Close();
                var data = stream.ToArray();
                // length of data + length of length + null terminator
                writer.Write(data.Length + 4 + 1);
                writer.Write(data);
                writer.Write((byte)0x00);
                return;
            }

            switch(item.Type)
            {
                case 0x01: // double
                    writer.Write((double)item.Value);
                    break;
                case 0x02: // string
                    var bytes = Encoding.UTF8.GetBytes(GetKeyName((string)item.Value, true));
                    writer.Write(bytes.Length + 1); // includes null terminator
                    writer.WriteBytes(bytes);
                    writer.Write((byte)0x00);
                    break;
                case 0x05: // binary
                    var byteArray = (byte[])item.Value;
                    writer.Write(byteArray.Length);
                    writer.Write((byte)0x00);
                    writer.Write(byteArray);
                    break;
                case 0x07: // objectid
                    writer.Write((byte[])item.Value);
                    break;
                case 0x08: // bool
                    writer.Write((bool)item.Value);
                    break;
                case 0x09: // UTC datetime
                case 0x11: // timestamp
                case 0x12: // 64 bit int
                    writer.Write((long)item.Value);
                    break;
                case 0x10: // 32 bit int
                    writer.Write((int)item.Value);
                    break;
                default:
                    throw new NotSupportedException($"Item type {item.Type} not supported.");
            }
        }
예제 #7
0
 /// <summary>
 /// Writes this document using the specified EasyWriter.
 /// </summary>
 /// <param name="writer">The writer that will be used to write this document.</param>
 internal void Write(EasyWriter writer)
 {
     _stringTableIndex = 0;
     _stringTable.Clear();
     WriteItem(writer, Top, null, true);
     writer.Close();
 }
예제 #8
0
        private void WriteItem(EasyWriter writer, BsonItem item, string name, bool isTop = false, bool isArray = false)
        {
            if (!isTop)
            {
                writer.Write(item.Type);
                writer.Write(isArray ? name : GetKeyName(name), Encoding.UTF8, true);
            }

            if (item.HasValues) // object or array - we need to write a document
            {
                var stream  = new MemoryStream();
                var vWriter = new EasyWriter(stream);
                foreach (string key in item.Keys)
                {
                    WriteItem(vWriter, item[key], key, false, item.IsArray);
                }
                vWriter.Close();
                var data = stream.ToArray();
                // length of data + length of length + null terminator
                writer.Write(data.Length + 4 + 1);
                writer.Write(data);
                writer.Write((byte)0x00);
                return;
            }

            switch (item.Type)
            {
            case 0x01:     // double
                writer.Write((double)item.Value);
                break;

            case 0x02:                          // string
                var bytes = Encoding.UTF8.GetBytes(GetKeyName((string)item.Value, true));
                writer.Write(bytes.Length + 1); // includes null terminator
                writer.WriteBytes(bytes);
                writer.Write((byte)0x00);
                break;

            case 0x05:     // binary
                var byteArray = (byte[])item.Value;
                writer.Write(byteArray.Length);
                writer.Write((byte)0x00);
                writer.Write(byteArray);
                break;

            case 0x07:     // objectid
                writer.Write((byte[])item.Value);
                break;

            case 0x08:     // bool
                writer.Write((bool)item.Value);
                break;

            case 0x09:     // UTC datetime
            case 0x11:     // timestamp
            case 0x12:     // 64 bit int
                writer.Write((long)item.Value);
                break;

            case 0x10:     // 32 bit int
                writer.Write((int)item.Value);
                break;

            default:
                throw new NotSupportedException($"Item type {item.Type} not supported.");
            }
        }