Splitting a byte[] into multiple byte[] arrays in C#

前端 未结 4 1976
天涯浪人
天涯浪人 2020-12-09 21:51

I am trying to \"chunk\" up the bytes of an image. This will allow me to upload a large image in portions. I have the image currently stored as one large byte[]. I would lik

4条回答
  •  既然无缘
    2020-12-09 22:19

    The most efficient method would be: not to. If you already have the image as a single byte[] then for local code, just specifying the offset and length (perhaps som ArraySegment-of-byte) is usually sufficient. If your upload API only takes byte[], then you still shouldn't chunk it completely; just use a single 512 buffer and use Buffer.BlockCopy to load it will successive pieces of the data. You may need to resize (Array.Resize) the final chunk, but at most 2 arrays should be needed.

    Even better; avoid needing a byte[] in the first place: consider loading the data via a streaming API (this will work well if the data is coming from a file); just use Read (in a loop, processing the returned value etc) to populate chunks of max 512. For example (untested, just of illustration):

    byte[] buffer = new byte[512];
    while(true) {
        int space = 512, read, offset = 0;
        while(space > 0 && (read = stream.Read(buffer, offset, space)) > 0) {
            space -= read;
            offset += read;
        }
        // either a full buffer, or EOF
        if(space != 0) { // EOF - final
           if(offset != 0) { // something to send
             Array.Resize(red buffer, offset);
             Upload(buffer);
           }
           break;
        } else { // full buffer
           Upload(buffer);
        }
    }
    

提交回复
热议问题