Search code examples
glibvala

file input/output in Vala (IOChannel vs FileStream vs IOStream)


I have a few fds (obtained from Process.spawn_async_with_pipes) that I want to read from and write to. Now I saw that there are multiple ways to do so:

  • using IOChannel.unix_new
  • using FileStream.fdopen
  • using UnixInputStream and UnixOutputStream

What is the difference between these APIs and which one should I take?


Solution

  • IOChannel

    • Part of GLib
    • Portable support for using files, pipes and sockets

    FileStream

    • Not part of GLib / GIO
    • Binds to standard C library (libc) fopen, fclose, fwrite, etc.

    UnixInputStream / UnixOutputStream

    • Part of GIO (gio-unix to be precise, it's Unix only as far as I know)
    • Supports the InputStream and OutputStream interfaces
    • Good for abstraction of different types of streams
    • There are also Win32InputStream and Win32OutputStream for dealing with Windows file handles in GIO

    So it depends on several factors which one to use:

    • Do you need non Unix support?
    • Do you want to use the GIO abstract interfaces?
    • Do you need to work with FILE*
    • Do you need to work with Windows file handles