`GOutputStream` is a base class for implementing streaming output.
It has functions to write to a stream ([method@Gio.OutputStream.write]), to close a stream ([method@Gio.OutputStream.close]) and to flush
pending writes ([method@Gio.OutputStream.flush]).
To copy the content of an input stream to an output stream without manually handling the reads and writes, use [method@Gio.OutputStream.splice]
.
See the documentation for [class@Gio.IOStream] for details of thread safety of streaming APIs.
All of these functions have async variants too.
All classes derived from `GOutputStream` *should* implement synchronous writing, splicing, flushing and closing streams, but *may* implement
asynchronous versions.
Example: IOStream:
public static int main (string[] args) {
try {
// Create a file that can only be accessed by the current user:
File file = File.new_for_path ("my-test.bin");
IOStream ios = file.create_readwrite (FileCreateFlags.PRIVATE);
//
// Write data:
//
size_t bytes_written;
OutputStream os = ios.output_stream;
os.write_all ("My 1. line\n".data, out bytes_written);
os.write_all ("My 2. line\n".data, out bytes_written);
DataOutputStream dos = new DataOutputStream (os);
dos.put_string ("My 3. line\n");
dos.put_int16 (10);
ios = null;
// Open the file for reading:
InputStream @is = file.read ();
//
// Read n bytes:
//
// Output: ``M``
uint8 buffer[1];
size_t size = @is.read (buffer);
stdout.write (buffer, size);
// Output: ``y 1. line``
DataInputStream dis = new DataInputStream (@is);
string str = dis.read_line ();
print ("%s\n", str);
// Output: ``My 2. line``
str = dis.read_line ();
print ("%s\n", str);
// Output: ``My 3. line``
str = dis.read_line ();
print ("%s\n", str);
// Output: ``10``
int16 i = dis.read_int16 ();
print ("%d\n", i);
} catch (Error e) {
print ("Error: %s\n", e.message);
}
return 0;
}
valac --pkg gio-2.0 GLib.IOStream.vala
- public void clear_pending ()
Clears the pending flag on this.
- public abstract bool close (Cancellable? cancellable = null) throws IOError
Closes the stream, releasing resources related to it.
- public virtual async bool close_async (int io_priority = DEFAULT, Cancellable? cancellable = null) throws IOError
Requests an asynchronous close of the stream, releasing resources related to
it.
- public virtual bool flush (Cancellable? cancellable = null) throws Error
Forces a write of all user-space buffered data for the given
this.
- public virtual async bool flush_async (int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
Forces an asynchronous write of all user-space buffered data for the given
this.
- public bool has_pending ()
Checks if an output stream has pending actions.
- public bool is_closed ()
Checks if an output stream has already been closed.
- public bool is_closing ()
Checks if an output stream is being closed.
- public bool printf (out size_t bytes_written, Cancellable? cancellable, string format, ...) throws Error
- public bool set_pending () throws Error
Sets this to have actions pending.
- public virtual ssize_t splice (InputStream source, OutputStreamSpliceFlags flags, Cancellable? cancellable = null) throws IOError
Splices an input stream into an output stream.
- public virtual async ssize_t splice_async (InputStream source, OutputStreamSpliceFlags flags, int io_priority = DEFAULT, Cancellable? cancellable = null) throws IOError
Splices a stream asynchronously.
- public bool vprintf (out size_t bytes_written, Cancellable? cancellable, string format, va_list args) throws Error
- public abstract ssize_t write (uint8[] buffer, Cancellable? cancellable = null) throws IOError
Tries to write buffer.length
bytes from buffer
into the stream.
- public bool write_all (uint8[] buffer, out size_t bytes_written, Cancellable? cancellable = null) throws IOError
Tries to write buffer.length
bytes from buffer
into the stream.
- public async bool write_all_async (uint8[] buffer, int io_priority, Cancellable? cancellable, out size_t bytes_written) throws Error
Request an asynchronous write of buffer.length
bytes from
buffer
into the stream.
- public virtual async ssize_t write_async (uint8[]? buffer, int io_priority = DEFAULT, Cancellable? cancellable = null) throws IOError
Request an asynchronous write of buffer.length
bytes from
buffer
into the stream.
- public ssize_t write_bytes (Bytes bytes, Cancellable? cancellable = null) throws Error
A wrapper function for
write which takes a
Bytes as input.
- public async ssize_t write_bytes_async (Bytes bytes, int io_priority = DEFAULT, Cancellable? cancellable = null) throws Error
- public virtual bool writev (OutputVector[] vectors, out size_t bytes_written, Cancellable? cancellable = null) throws Error
Tries to write the bytes contained in the vectors.length
vectors
into the stream.
- public bool writev_all (OutputVector[] vectors, out size_t bytes_written, Cancellable? cancellable = null) throws Error
Tries to write the bytes contained in the vectors.length
vectors
into the stream.
- public async bool writev_all_async (OutputVector[] vectors, int io_priority = DEFAULT, Cancellable? cancellable = null, out size_t bytes_written) throws Error
Request an asynchronous write of the bytes contained in the
vectors.length
vectors
into the stream.
- public virtual async bool writev_async (OutputVector[] vectors, int io_priority = DEFAULT, Cancellable? cancellable = null, out size_t bytes_written) throws Error
Request an asynchronous write of the bytes contained in vectors.length
vectors
into the stream.