I'm trying to do something like pagination on a FileStream, function get FileStream and numberOfChunks and currentChunk, it slices the FileStream to numberOfChunks and return only byte of currentChunk but it only works when the currentChunk is 0
public async Task<byte[]> GetStreamChunkAsync(Stream inputStream, int numberOfChunks, int currentChunk)
{
if (numberOfChunks <= 0 || currentChunk < 0 || currentChunk >= numberOfChunks)
{
throw new ArgumentOutOfRangeException("Invalid numberOfChunks or currentChunk values");
}
int bufferSize = (int)Math.Ceiling((double)inputStream.Length / numberOfChunks);
int startPosition = currentChunk * bufferSize;
int remainingBytes = (int)Math.Min(bufferSize, inputStream.Length - startPosition);
byte[] buffer =new byte[remainingBytes];
int bytesRead = await inputStream.ReadAsync(buffer, startPosition, buffer.Length);
if (bytesRead > 0)
{
return buffer;
}
// Return null if no bytes were read (unexpected end of stream)
return null;
}
You shouldn't set the position manually. The stream will advance anyway.
You do have another issue: you need a read loop, as the buffer may not be filled fully. A read loop would look like this
Or you can use
ReadAtLeastAsyncHaving said that, I would recommend you instead use the new Async Iterator feature, which means you can do a
await foreachover this.You can also return
Memory<byte>which will be more efficient in the case when you return the last partial chunk, which you would otherwise have to copy to a new array.Then use it like this