Stream that reads byte per byte?

Asked

Viewed 59 times

0

I created a simple byte-by-byte reading method in a stream, but it’s very slow and it’s taking minutes to process a ten-megabyte file, I don’t know why it’s so slow. It needs to be byte-by-byte and not byte[] because it’s an operation I do in one byte at a given position and write it at the same position as the stream. The gambiarra:

public void EncryptStream(Stream s) {

    // verificações
    if (s == null) throw new ArgumentNullException("s", "Stream cannot be nothing.");
    if (s.CanRead == false) throw new Exception("Stream is not readable.");
    if (s.CanWrite == false) throw new Exception("Stream is not writeable.");

    long len = s.Length;
    for(long i = 0; i <= len; i++) {
        s.Position = i;
        // "a" é o byte que está sendo operacionado no stream
        // "i" é a posição que o byte está no stream
        // o byte "a" será escrito novamente na mesma posição após a
        // operação abaixo.
        int a = s.ReadByte(); s.Position--;
        int pos = computePos(key, i);
        a += pos;
        s.WriteByte((byte)a);
    }
    byte checksun = performKeyHash(key);
    Console.WriteLine("CheckE = " + checksun.ToString());
    s.Position = len + 1;
    s.WriteByte(checksun);
    s.Close();
}

What is wrong to be so slow? There is something that can be done? Remembering that I can’t make Stream read a byte buffer, it needs to be byte-by-byte.

  • The answer lies in the question, if you want to create an artificial constraint coexists with the problem it causes.

  • There is no other "more correct" way to do this?

  • 1

    There are several things you would need to analyze. It needs to be Stream? really needs to be generic? Can’t make different versions for each type of Stream just for your own need? Do you have one that will probably be used more? There are some variables and functions that I don’t know what they are for. Do you know where the neck is? You have to touch it. You have to make adjustments and test. You can fix a lot of problems with this code, but performance depends on things I don’t know from what’s posted.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.