Instance Methodswift-nio 2.72.0NIOPosix
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: 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.
Other members in extension
Types
enum Error
NonBlockingFileIO
errors.
Type members
init(threadPool: NIOThreadPool
) Initialize a
NonBlockingFileIO
which uses theNIOThreadPool
.static let defaultChunkSize: Int
The default and recommended chunk size.
static let defaultThreadPoolSize: Int
The default and recommended size for
NonBlockingFileIO
’s thread pool.
Instance members
func changeFileSize(fileHandle: NIOFileHandle, size: Int64
) async throws Changes the file size of
fileHandle
tosize
.func changeFileSize(fileHandle: NIOFileHandle, size: Int64, eventLoop: EventLoop
) -> EventLoopFuture<()> Changes the file size of
fileHandle
tosize
.func createDirectory(path: String, withIntermediateDirectories: Bool, mode: NIOPOSIXFileMode
) async throws Creates directory at
path
on a private thread pool.func createDirectory(path: String, withIntermediateDirectories: Bool, mode: NIOPOSIXFileMode, eventLoop: EventLoop
) -> EventLoopFuture<Void> Creates directory at
path
on a private thread pool which is separate from anyEventLoop
thread.func listDirectory(path: String
) async throws -> [NIODirectoryEntry] List contents of the directory at
path
on a private thread pool.func listDirectory(path: String, eventLoop: EventLoop
) -> EventLoopFuture<[NIODirectoryEntry]> List contents of the directory at
path
on a private thread pool which is separate from anyEventLoop
thread.func lstat(path: String
) async throws -> stat Returns information about a file at
path
on a private thread pool.func lstat(path: String, eventLoop: EventLoop
) -> EventLoopFuture<stat> Returns information about a file at
path
on a private thread pool which is separate from anyEventLoop
thread.func openFile(path: String, eventLoop: EventLoop
) -> EventLoopFuture<(NIOFileHandle, FileRegion)> Open the file at
path
for reading on a private thread pool which is separate from anyEventLoop
thread.func openFile(path: String, mode: NIOFileHandle.Mode, flags: NIOFileHandle.Flags, eventLoop: EventLoop
) -> EventLoopFuture<NIOFileHandle> Open the file at
path
with specified access mode and POSIX flags on a private thread pool which is separate from anyEventLoop
thread.func read(fileHandle: NIOFileHandle, byteCount: Int, allocator: ByteBufferAllocator
) async throws -> ByteBuffer Read
byteCount
bytes fromfileHandle
inNonBlockingFileIO
’s private thread pool.func read(fileHandle: NIOFileHandle, byteCount: Int, allocator: ByteBufferAllocator, eventLoop: EventLoop
) -> EventLoopFuture<ByteBuffer> Read
byteCount
bytes fromfileHandle
inNonBlockingFileIO
’s private thread pool which is separate from anyEventLoop
thread.func read(fileHandle: NIOFileHandle, fromOffset: Int64, byteCount: Int, allocator: ByteBufferAllocator
) async throws -> ByteBuffer Read
byteCount
bytes starting atfileOffset
fromfileHandle
inNonBlockingFileIO
’s private thread pool .func read(fileHandle: NIOFileHandle, fromOffset: Int64, byteCount: Int, allocator: ByteBufferAllocator, eventLoop: EventLoop
) -> EventLoopFuture<ByteBuffer> Read
byteCount
bytes starting atfileOffset
fromfileHandle
inNonBlockingFileIO
’s private thread pool which is separate from anyEventLoop
thread.func read(fileRegion: FileRegion, allocator: ByteBufferAllocator
) async throws -> ByteBuffer Read a
FileRegion
inNonBlockingFileIO
’s private thread pool.func read(fileRegion: FileRegion, allocator: ByteBufferAllocator, eventLoop: EventLoop
) -> EventLoopFuture<ByteBuffer> Read a
FileRegion
inNonBlockingFileIO
’s private thread pool which is separate from anyEventLoop
thread.func readChunked(fileHandle: NIOFileHandle, byteCount: Int, chunkSize: Int, allocator: ByteBufferAllocator, eventLoop: EventLoop, chunkHandler: @escaping (ByteBuffer) -> EventLoopFuture<Void>
) -> EventLoopFuture<Void> Read
byteCount
bytes in chunks ofchunkSize
bytes fromfileHandle
inNonBlockingFileIO
’s private thread pool which is separate from anyEventLoop
thread.func readChunked(fileHandle: NIOFileHandle, fromOffset: Int64, byteCount: Int, chunkSize: Int, allocator: ByteBufferAllocator, eventLoop: EventLoop, chunkHandler: @escaping (ByteBuffer) -> EventLoopFuture<Void>
) -> EventLoopFuture<Void> Read
byteCount
bytes from offsetfileOffset
in chunks ofchunkSize
bytes fromfileHandle
inNonBlockingFileIO
’s private thread pool which is separate from anyEventLoop
thread.func readFileSize(fileHandle: NIOFileHandle
) async throws -> Int64 Returns the length of the file associated with
fileHandle
.func readFileSize(fileHandle: NIOFileHandle, eventLoop: EventLoop
) -> EventLoopFuture<Int64> Returns the length of the file in bytes associated with
fileHandle
.func readlink(path: String
) async throws -> String Returns target of the symbolic link at
path
on a private thread pool.func readlink(path: String, eventLoop: EventLoop
) -> EventLoopFuture<String> Returns target of the symbolic link at
path
on a private thread pool which is separate from anyEventLoop
thread.func remove(path: String
) async throws Removes the file at
path
on a private thread pool.func remove(path: String, eventLoop: EventLoop
) -> EventLoopFuture<Void> Removes the file at
path
on a private thread pool which is separate from anyEventLoop
thread.func rename(path: String, newName: String
) async throws Renames the file at
path
tonewName
on a private thread pool.func rename(path: String, newName: String, eventLoop: EventLoop
) -> EventLoopFuture<Void> Renames the file at
path
tonewName
on a private thread pool which is separate from anyEventLoop
thread.func symlink(path: String, to: String
) async throws Creates a symbolic link to a
destination
file atpath
on a private thread pool.func symlink(path: String, to: String, eventLoop: EventLoop
) -> EventLoopFuture<Void> Creates a symbolic link to a
destination
file atpath
on a private thread pool which is separate from anyEventLoop
thread.func unlink(path: String
) async throws Removes symbolic link at
path
on a private thread pool which is separate from anyEventLoop
thread.func unlink(path: String, eventLoop: EventLoop
) -> EventLoopFuture<Void> Removes symbolic link at
path
on a private thread pool which is separate from anyEventLoop
thread.func withFileHandle<Result>(path: String, mode: NIOFileHandle.Mode, flags: NIOFileHandle.Flags, (NIOFileHandle) async throws -> Result
) async throws -> Result Open file at
path
on a private thread pool, run an operation given the file handle and then close the file handle.func withFileRegion<Result>(path: String, (_ fileRegion: FileRegion) async throws -> Result
) async throws -> Result Open file at
path
and query its size on a private thread pool, run an operation given the resulting file region and then close the file handle.func write(fileHandle: NIOFileHandle, buffer: ByteBuffer
) async throws Write
buffer
tofileHandle
inNonBlockingFileIO
’s private thread pool.func write(fileHandle: NIOFileHandle, buffer: ByteBuffer, eventLoop: EventLoop
) -> EventLoopFuture<()> Write
buffer
tofileHandle
inNonBlockingFileIO
’s private thread pool which is separate from anyEventLoop
thread.func write(fileHandle: NIOFileHandle, toOffset: Int64, buffer: ByteBuffer
) async throws Write
buffer
starting fromtoOffset
tofileHandle
inNonBlockingFileIO
’s private thread pool.func write(fileHandle: NIOFileHandle, toOffset: Int64, buffer: ByteBuffer, eventLoop: EventLoop
) -> EventLoopFuture<()> Write
buffer
starting fromtoOffset
tofileHandle
inNonBlockingFileIO
’s private thread pool which is separate from anyEventLoop
thread.