我正在try 对FileStream执行分页之类的操作,函数Get FileStream和number OfChunks以及CurrentChunk,它将FileStream切片为number OfChunks并只返回CurrentChunk的字节,但它只有在CurrentChunk为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;
}