开发者

Need help using Read7BitEncodedInt

开发者 https://www.devze.com 2023-03-12 20:58 出处:网络
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 l

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


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


This is a case where you have a solution to a problem in mind before you ask the question, even if the solution is a bad idea. There is a blog entry on either Raymond Chen's blog, Eric Lippert's blog or probably both about this subject, but I couldn't find it (them).

In the last 20 years, needing to care about 7-bit characters has been mostly phased out. And, the notion of trying to save disk space by using them is patently ridiculous.

The only way this would even work would be if you overlapped characters so that you effectively combined 8 characters into 7 bytes. I can almost guarantee without looking that that is not what Read7BitEncodedInt does.

(Actually, I have no idea what it does, but fortunately, I don't care)

If you need to work with large files, use compression of some kind. Zip, GZip, 7-Zip, whatever.

EDIT: Since you are not actually asking about strings, then this may be of use. YMMV, however. Look for the ReadVLI and WriteVLI functions.

http://gilgamesh.hamsterrepublic.com/websvn/filedetails.php?repname=ohrrpgce&path=%2Fwip%2Freload.bas&rev=3568&peg=4164

0

精彩评论

暂无评论...
验证码 换一张
取 消