0

I am wondering if there is any difference in getting bytes range content in ByteRangeStreamContent and getting bytes range directly from bytes array

So basically I want to ask if there is any difference in below two codes, in terms of the result that the receiver will receive as response

byte[] bytes = firmwareFile.Content;;
var stream = new MemoryStream(bytes);
var content = new ByteRangeStreamContent(stream, Request.Headers.Range, contentType);
var response = new HttpResponseMessage
                         {
                             Content = content
                         };
result.StatusCode = HttpStatusCode.PartialContent;

and

if (Request.Headers.Range != null)
{
    var range = Request.Headers.Range.Ranges.OfType<RangeItemHeaderValue>().First();

    if(range.From.HasValue && range.To.HasValue)
    {
        var from = (int)range.From.Value;
        var to = (int)range.To.Value;

        bytes = firmwareFile.Content.Skip(from).Take(to - from).ToArray();
    }
}

var stream = new MemoryStream(bytes);
var content = new ByteArrayContent(stream.ToArray());
var response = new HttpResponseMessage
                             {
                                 Content = content
                             };
response.Content.Headers.ContentType = contentType;
result.StatusCode = HttpStatusCode.PartialContent;

Also, is the second version correct? And can I use it

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • It doesn't look like the second example is adding the response headers. – ProgrammingLlama May 27 '20 at 06:12
  • @John - Ah! it was just mistake. I forgot to add that line. Updated the question. – Pawan Nogariya May 27 '20 at 06:15
  • It still appears to be missing `Content-Range`. – ProgrammingLlama May 27 '20 at 06:19
  • I updated the question to add `StatusCode`. So if I add `response.Content.Headers.ContentRange = new ContentRangeHeaderValue(from, to);` to the second code, it should become same as the first? – Pawan Nogariya May 27 '20 at 06:31
  • Maybe. Don't hold me to that since I've never needed to do it manually. – ProgrammingLlama May 27 '20 at 06:32
  • Hmm okay. Actually I want to add CRC32 to the data before sending it that is why I am trying to do it manually, so that I can add CRC32 to data bytes array before sending it and with the first approach I have no control over bytes array if I use `ByteRangeStreamContent`. Any idea of this particular case? – Pawan Nogariya May 27 '20 at 06:34
  • Is tha CRC as a header, or? – ProgrammingLlama May 27 '20 at 06:56
  • No, with the data. To be appended in the end of the data. – Pawan Nogariya May 27 '20 at 07:31
  • Problems with that approach might be: 1) you would need to make the actual response bytes shorter than requested, to accommodate the additional bytes taken up by the CRC result. 2) If a range smaller than the size of the CRC is requested, what will you do? – ProgrammingLlama May 27 '20 at 07:36
  • For 1st - I am thinking to send more bytes than requested, meaning including crc bytes, because with manual approach I can control the size of data that should be sent. For 2nd - This case will never happen, as per my business requirement. – Pawan Nogariya May 27 '20 at 07:39
  • And actually I am not saying the approach I am thinking is full proof, that is why I put question and looking of better approach to do this – Pawan Nogariya May 27 '20 at 07:40
  • For 1) It might result in undefined behaviour in some clients. Anyway, I'm afraid I don't have a complete answer for your actual question and would need to do research before I could answer it. I don't have time to do that right now so I wish you the best of luck :) – ProgrammingLlama May 27 '20 at 07:40
  • Okay. But thanks for your help to this level also :) I am searching for it from last two three days, have not found anything concrete yet. But let's see. Thanks! – Pawan Nogariya May 27 '20 at 07:43

0 Answers0