Is there an overlap between Core Libraries for file handling?

I'm building a library to parse binary data and I started using the SystemPackage.FileDescriptor to read from and write to file. Since additionally, I'm now considering of using, since I just found out it existed, SwiftNIO.ByteBuffer to do the reading and writing of different types.

I wish ByteBuffer would be standalone but I don't know if there's a plan for that ATM.

The FileDescriptor name became ambiguous since both libraries provide the same name and they seem to do, not same, but similar tasks in this regard. Is there a reason for this apparent "overlap", if there's any?

Among other options would be Data's init(contentsOf:options:), FileManager's contents(atPath:), FileHandle's readToEnd(), DispatchIO's read, unix's mmap and URLSession's data(from:delegate:). Some methods are more convenient than others (e.g. methods returning Data straight away are better if that's the Data type that you want to get).

For a beginner like me, can you suggest me the simplest approach?

SwiftNIO's FileDescriptor should go away over time: it exists only because it predated Swift System. In the next major release of NIO we should be aiming to remove our custom type and replace it with Swift System's.

6 Likes

Reading from local files into Data? If you don't care about asynchronity aspects (e.g. not a file on a network volume) then Data(contentsOf:) is the simplest. And this for async await version:

try await URLSession.shared.data(from: localOrRemoteUrl, delegate: nil)

In some projects I'm using these conveniences:

let file = URL.temporaryFolder + "file.xyz"
file.data = Data(...) // writes
...
let data = file.data // reads
....
file.data = nil // deletes
1 Like

@tera Thank you so much. You have made my made. I will let you know about the results soon.