readChunked(fileRegion:chunkSize:allocator:eventLoop:chunkHandler:)
Read a FileRegion
in chunks of chunkSize
bytes on NonBlockingFileIO
’s private thread pool which is separate from any EventLoop
thread.
@preconcurrency func readChunked(fileRegion: FileRegion, chunkSize: Int = NonBlockingFileIO.defaultChunkSize, allocator: ByteBufferAllocator, eventLoop: any EventLoop, chunkHandler: @escaping (ByteBuffer) -> EventLoopFuture<Void>) -> EventLoopFuture<Void>
Parameters
- fileRegion
The file region to read.
- chunkSize
The size of the individual chunks to deliver.
- allocator
A
ByteBufferAllocator
used to allocate space for the chunks.- eventLoop
The
EventLoop
to callchunkHandler
on.- chunkHandler
Called for every chunk read. The next chunk will be read upon successful completion of the returned
EventLoopFuture
. If the returnedEventLoopFuture
fails, the overall operation is aborted.
Returns
An EventLoopFuture
which is the result of the overall operation. If either the reading of fileHandle
or chunkHandler
fails, the EventLoopFuture
will fail too. If the reading of fileHandle
as well as chunkHandler
always succeeded, the EventLoopFuture
will succeed too.
chunkHandler
will be called on eventLoop
for every chunk that was read. Assuming fileRegion.readableBytes
is greater than zero and there are enough bytes available chunkHandler
will be called 1 + |_ fileRegion.readableBytes / chunkSize _|
times, delivering chunkSize
bytes each time. If less than fileRegion.readableBytes
bytes can be read from the file, chunkHandler
will be called less often with the last invocation possibly being of less than chunkSize
bytes.
The allocation and reading of a subsequent chunk will only be attempted when chunkHandler
succeeds.
This method will not use the file descriptor’s seek pointer which means there is no danger of reading from the same FileRegion
in multiple threads.