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); }
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); }
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); }
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); }
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(); }