Search code examples
c#.netbinarybinarywriter7-bit

Need help using Read7BitEncodedInt


I've been searching for 2 hours or better for a way to use the Read7BitEncodedInt method for this. I need to use it somehow to reduce my file size (in this case likely by 100mb or more). I was also looking at using the ReadString method since it seems to do roughly the same thing. But that seems less appropriate and I'm not really sure that it would work. If there is some other alternative to this that I'm unaware of I'd be open to using that too.

In summation. How would I implement the Read7BitEncodedInt method into the following code ? Also, I'm not too certain that my method to Write7BitEncodedInt is correct either.

    public void SaveFile()
    {
        using (FileStream stream = new FileStream("C:\\A_random.txt", FileMode.Create))
        {
            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                for (int i = 0; i < typeCount.Count; i++)
                {
                    writer.Write((byte)typeCount[i]);
                    writer.Write(type[i]);
                }
                writer.Close();
            }
        }
        LoadFile();
    }

    public void LoadFile()
    {
        using (FileStream stream = new FileStream("C:\\A_random.txt", FileMode.Open))
        {
            using (BinaryReader reader = new BinaryReader(stream))
            {
                int i = 0;
                while (stream.Position != stream.Length)
                {
                    int count = reader.Read7BitEncodedInt();
                    byte val = reader.ReadByte();
                    for (int ii = 0; ii < count; ii++)
                    {
                        grid[i].val1 = i;
                        grid[i].val2 = val;
                        grid[i].val3 = vect;
                        i++;
                    }
                }
                reader.Close();
            }
        }
    }

Solution

  • Here is a way to do it:

    public class MyBinaryReader : BinaryReader {
        public MyBinaryReader(Stream stream) : base(stream) {}
        public new int Read7BitEncodedInt() {
            return base.Read7BitEncodedInt();
        }
    }
    
    public class MyBinaryWriter : BinaryWriter {
        public MyBinaryWriter(Stream stream) : base(stream) {}
        public new void Write7BitEncodedInt(int i) {
            base.Write7BitEncodedInt(i);
        }
    }
    

    And some test code:

    void Main() {
    var stream = new MemoryStream();
    
    var writer = new MyBinaryWriter(stream);    
    
    writer.Write7BitEncodedInt(100);
    writer.Write7BitEncodedInt(1000);
    writer.Write7BitEncodedInt(10000);
    writer.Write7BitEncodedInt(100000);
    writer.Write7BitEncodedInt(1000000);
    writer.Write7BitEncodedInt(-1000000);
    
    stream.Position = 0;
    
    var reader = new MyBinaryReader(stream);    
    
    Debug.Assert(reader.Read7BitEncodedInt() == 100);
    Debug.Assert(reader.Read7BitEncodedInt() == 1000);
    Debug.Assert(reader.Read7BitEncodedInt() == 10000);
    Debug.Assert(reader.Read7BitEncodedInt() == 100000);
    Debug.Assert(reader.Read7BitEncodedInt() == 1000000);
    Debug.Assert(reader.Read7BitEncodedInt() == -1000000);
    }