readChunked(fileHandle:fromOffset:byteCount:chunkSize:allocator:eventLoop:chunkHandler:)
Read byteCount
bytes from offset fileOffset
in chunks of chunkSize
bytes from fileHandle
in NonBlockingFileIO
’s private thread pool which is separate from any EventLoop
thread.
@preconcurrency func readChunked(fileHandle: NIOFileHandle, fromOffset fileOffset: Int64, byteCount: Int, chunkSize: Int = NonBlockingFileIO.defaultChunkSize, allocator: ByteBufferAllocator, eventLoop: any EventLoop, chunkHandler: @escaping (ByteBuffer) -> EventLoopFuture<Void>) -> EventLoopFuture<Void>
Parameters
- fileHandle
The
NIOFileHandle
to read from.- fileOffset
The offset into the file at which the read should begin.
- byteCount
The number of bytes to read from
fileHandle
.- 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 byteCount
is greater than zero and there are enough bytes available chunkHandler
will be called 1 + |_ byteCount / chunkSize _|
times, delivering chunkSize
bytes each time. If less than byteCount
bytes can be read from descriptor
, 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 NIOFileHandle
in multiple threads.