コード例 #1
0
        public static OpenBinaryFormat FromFile(string file)
        {
            OpenBinaryFormat format = new OpenBinaryFormat();

            System.IO.FileStream f = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            if (file.EndsWith(".gz"))
            {
                var mem = new System.IO.MemoryStream();
                using (GZipStream gzip = new GZipStream(f, CompressionMode.Decompress))
                {
                    // Use 4 KiB as buffer when decompressing.
                    var bufferSize = 1024 << 2;
                    var buffer     = new byte[bufferSize];
                    var readBytes  = bufferSize;
                    while (readBytes == bufferSize)
                    {
                        readBytes = gzip.Read(buffer, 0, buffer.Length);
                        mem.Write(buffer, 0, readBytes);
                    }
                }
                var bytes = mem.ToArray();
                f.Close();
                format.r = new System.IO.BinaryReader(new System.IO.MemoryStream(bytes), System.Text.Encoding.UTF8);
            }
            else
            {
                format.r = new System.IO.BinaryReader(f);
            }

            return(format);
        }
コード例 #2
0
        public static OpenBinaryFormat ToMemory(System.IO.MemoryStream mem)
        {
            OpenBinaryFormat format = new OpenBinaryFormat();

            format.w = new System.IO.BinaryWriter(mem, System.Text.Encoding.UTF8);

            return(format);
        }
コード例 #3
0
        public static OpenBinaryFormat FromStream(System.IO.Stream stream)
        {
            OpenBinaryFormat format = new OpenBinaryFormat();

            format.r = new System.IO.BinaryReader(stream, System.Text.Encoding.UTF8);

            return(format);
        }
コード例 #4
0
        public static OpenBinaryFormat ToFile(string file)
        {
            OpenBinaryFormat format = new OpenBinaryFormat();

            if (file.EndsWith(".gz"))
            {
                var mem = new System.IO.MemoryStream();
                format.w        = new System.IO.BinaryWriter(mem, System.Text.Encoding.UTF8);
                format.m_gzFile = file;
            }
            else
            {
                System.IO.FileStream f = new System.IO.FileStream(file, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                format.w = new System.IO.BinaryWriter(f, System.Text.Encoding.UTF8);
            }

            return(format);
        }
コード例 #5
0
        public void TestSkipBlock()
        {
            var mem = new System.IO.MemoryStream();
            var f   = OpenBinaryFormat.ToMemory(mem);

            var person = f.StartBlock("person");

            f.WriteString("name", "Luke Skywalker");

            // Write a block that will be skipped.
            var optional = f.StartBlock("skip this");

            f.WriteDouble("age", 20);
            f.EndBlock(optional);

            f.EndBlock(person);

            var bytes = mem.ToArray();

            f.Close();

            f = OpenBinaryFormat.FromBytes(bytes);

            person = f.StartBlock("person");
            Assert.True(person != -1);

            var name     = f.Seek <string>("name", null, person);
            var age      = f.Seek <int>("age", 0, person);
            var notThere = f.Seek <string>("notThere", "not found", person);

            f.EndBlock(person);

            Assert.True(name == "Luke Skywalker");
            Assert.False(age == 20);
            Assert.True(notThere == "not found");

            f.Close();
        }