GIO Reference Manual | ||||
---|---|---|---|---|
Top | Description | Object Hierarchy |
#include <gio/gio.h> enum GOutputStreamSpliceFlags; GOutputStream; gssize g_output_stream_write (GOutputStream *stream, const void *buffer, gsize count, GCancellable *cancellable, GError **error); gboolean g_output_stream_write_all (GOutputStream *stream, const void *buffer, gsize count, gsize *bytes_written, GCancellable *cancellable, GError **error); gssize g_output_stream_splice (GOutputStream *stream, GInputStream *source, GOutputStreamSpliceFlags flags, GCancellable *cancellable, GError **error); gboolean g_output_stream_flush (GOutputStream *stream, GCancellable *cancellable, GError **error); gboolean g_output_stream_close (GOutputStream *stream, GCancellable *cancellable, GError **error); void g_output_stream_write_async (GOutputStream *stream, const void *buffer, gsize count, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); gssize g_output_stream_write_finish (GOutputStream *stream, GAsyncResult *result, GError **error); void g_output_stream_splice_async (GOutputStream *stream, GInputStream *source, GOutputStreamSpliceFlags flags, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); gssize g_output_stream_splice_finish (GOutputStream *stream, GAsyncResult *result, GError **error); void g_output_stream_flush_async (GOutputStream *stream, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); gboolean g_output_stream_flush_finish (GOutputStream *stream, GAsyncResult *result, GError **error); void g_output_stream_close_async (GOutputStream *stream, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); gboolean g_output_stream_close_finish (GOutputStream *stream, GAsyncResult *result, GError **error); gboolean g_output_stream_is_closed (GOutputStream *stream); gboolean g_output_stream_has_pending (GOutputStream *stream); gboolean g_output_stream_set_pending (GOutputStream *stream, GError **error); void g_output_stream_clear_pending (GOutputStream *stream);
GObject +----GOutputStream +----GFilterOutputStream +----GFileOutputStream +----GMemoryOutputStream +----GUnixOutputStream
GOutputStream has functions to write to a stream (g_output_stream_write()
),
to close a stream (g_output_stream_close()
) and to flush pending writes
(g_output_stream_flush()
).
To copy the content of an input stream to an output stream without
manually handling the reads and writes, use g_output_stream_splice()
.
All of these functions have async variants too.
typedef enum { G_OUTPUT_STREAM_SPLICE_NONE = 0, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE = (1 << 0), G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET = (1 << 1) } GOutputStreamSpliceFlags;
GOutputStreamSpliceFlags determine how streams should be spliced.
typedef struct _GOutputStream GOutputStream;
Base class for writing output.
All classes derived from GOutputStream should implement synchronous writing, splicing, flushing and closing streams, but may implement asynchronous versions.
gssize g_output_stream_write (GOutputStream *stream, const void *buffer, gsize count, GCancellable *cancellable, GError **error);
Tries to write count
bytes from buffer
into the stream. Will block
during the operation.
If count is zero returns zero and does nothing. A value of count
larger than G_MAXSSIZE
will cause a G_IO_ERROR_INVALID_ARGUMENT
error.
On success, the number of bytes written to the stream is returned.
It is not an error if this is not the same as the requested size, as it
can happen e.g. on a partial i/o error, or if there is not enough
storage in the stream. All writes either block until at least one byte
is written, so zero is never returned (unless count
is zero).
If cancellable
is not NULL, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error G_IO_ERROR_CANCELLED will be returned. If an
operation was partially finished when the operation was cancelled the
partial result will be returned, without an error.
On error -1 is returned and error
is set accordingly.
|
a GOutputStream. |
|
the buffer containing the data to write. |
|
the number of bytes to write |
|
optional cancellable object |
|
location to store the error occuring, or NULL to ignore
|
Returns : |
Number of bytes written, or -1 on error |
gboolean g_output_stream_write_all (GOutputStream *stream, const void *buffer, gsize count, gsize *bytes_written, GCancellable *cancellable, GError **error);
Tries to write count
bytes from buffer
into the stream. Will block
during the operation.
This function is similar to g_output_stream_write()
, except it tries to
write as many bytes as requested, only stopping on an error.
On a successful write of count
bytes, TRUE
is returned, and bytes_written
is set to count
.
If there is an error during the operation FALSE is returned and error
is set to indicate the error status, bytes_written
is updated to contain
the number of bytes written into the stream before the error occurred.
|
a GOutputStream. |
|
the buffer containing the data to write. |
|
the number of bytes to write |
|
location to store the number of bytes that was written to the stream |
|
optional GCancellable object, NULL to ignore.
|
|
location to store the error occuring, or NULL to ignore
|
Returns : |
TRUE on success, FALSE if there was an error
|
gssize g_output_stream_splice (GOutputStream *stream, GInputStream *source, GOutputStreamSpliceFlags flags, GCancellable *cancellable, GError **error);
Splices an input stream into an output stream.
|
a GOutputStream. |
|
a GInputStream. |
|
a set of GOutputStreamSpliceFlags. |
|
optional GCancellable object, NULL to ignore.
|
|
a GError location to store the error occuring, or NULL to
ignore.
|
Returns : |
a gssize containing the size of the data spliced. |
gboolean g_output_stream_flush (GOutputStream *stream, GCancellable *cancellable, GError **error);
Flushed any outstanding buffers in the stream. Will block during the operation. Closing the stream will implicitly cause a flush.
This function is optional for inherited classes.
If cancellable
is not NULL
, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error G_IO_ERROR_CANCELLED
will be returned.
|
a GOutputStream. |
|
optional cancellable object |
|
location to store the error occuring, or NULL to ignore
|
Returns : |
TRUE on success, FALSE on error
|
gboolean g_output_stream_close (GOutputStream *stream, GCancellable *cancellable, GError **error);
Closes the stream, releasing resources related to it.
Once the stream is closed, all other operations will return G_IO_ERROR_CLOSED
.
Closing a stream multiple times will not return an error.
Closing a stream will automatically flush any outstanding buffers in the stream.
Streams will be automatically closed when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible.
Some streams might keep the backing store of the stream (e.g. a file descriptor) open after the stream is closed. See the documentation for the individual stream for details.
On failure the first error that happened will be reported, but the close
operation will finish as much as possible. A stream that failed to
close will still return G_IO_ERROR_CLOSED
for all operations. Still, it
is important to check and report the error to the user, otherwise
there might be a loss of data as all data might not be written.
If cancellable
is not NULL, then the operation can be cancelled by
triggering the cancellable object from another thread. If the operation
was cancelled, the error G_IO_ERROR_CANCELLED
will be returned.
Cancelling a close will still leave the stream closed, but there some streams
can use a faster close that doesn't block to e.g. check errors. On
cancellation (as with any error) there is no guarantee that all written
data will reach the target.
|
A GOutputStream. |
|
optional cancellable object |
|
location to store the error occuring, or NULL to ignore
|
Returns : |
TRUE on success, FALSE on failure
|
void g_output_stream_write_async (GOutputStream *stream, const void *buffer, gsize count, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data);
Request an asynchronous write of count
bytes from buffer
into
the stream. When the operation is finished callback
will be called.
You can then call g_output_stream_write_finish()
to get the result of the
operation.
During an async request no other sync and async calls are allowed,
and will result in G_IO_ERROR_PENDING
errors.
A value of count
larger than G_MAXSSIZE
will cause a
G_IO_ERROR_INVALID_ARGUMENT
error.
On success, the number of bytes written will be passed to the
callback
. It is not an error if this is not the same as the
requested size, as it can happen e.g. on a partial I/O error,
but generally we try to write as many bytes as requested.
Any outstanding I/O request with higher priority (lower numerical
value) will be executed before an outstanding request with lower
priority. Default priority is G_PRIORITY_DEFAULT
.
The asyncronous methods have a default fallback that uses threads to implement asynchronicity, so they are optional for inheriting classes. However, if you override one you must override all.
For the synchronous, blocking version of this function, see
g_output_stream_write()
.
|
A GOutputStream. |
|
the buffer containing the data to write. |
|
the number of bytes to write |
|
the io priority of the request. |
|
optional GCancellable object, NULL to ignore.
|
|
callback to call when the request is satisfied |
|
the data to pass to callback function |
gssize g_output_stream_write_finish (GOutputStream *stream, GAsyncResult *result, GError **error);
Finishes a stream write operation.
|
a GOutputStream. |
|
a GAsyncResult. |
|
a GError location to store the error occuring, or NULL to
ignore.
|
Returns : |
a gssize containing the number of bytes written to the stream. |
void g_output_stream_splice_async (GOutputStream *stream, GInputStream *source, GOutputStreamSpliceFlags flags, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data);
Splices a stream asynchronously.
When the operation is finished callback
will be called.
You can then call g_output_stream_splice_finish()
to get the
result of the operation.
For the synchronous, blocking version of this function, see
g_output_stream_splice()
.
|
a GOutputStream. |
|
a GInputStream. |
|
a set of GOutputStreamSpliceFlags. |
|
the io priority of the request. |
|
optional GCancellable object, NULL to ignore.
|
|
a GAsyncReadyCallback. |
|
user data passed to callback .
|
gssize g_output_stream_splice_finish (GOutputStream *stream, GAsyncResult *result, GError **error);
Finishes an asynchronous stream splice operation.
|
a GOutputStream. |
|
a GAsyncResult. |
|
a GError location to store the error occuring, or NULL to
ignore.
|
Returns : |
a gssize of the number of bytes spliced. |
void g_output_stream_flush_async (GOutputStream *stream, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data);
Flushes a stream asynchronously.
For behaviour details see g_output_stream_flush()
.
When the operation is finished callback
will be
called. You can then call g_output_stream_flush_finish()
to get the
result of the operation.
|
a GOutputStream. |
|
the io priority of the request. |
|
optional GCancellable object, NULL to ignore.
|
|
a GAsyncReadyCallback to call when the request is satisfied |
|
the data to pass to callback function |
gboolean g_output_stream_flush_finish (GOutputStream *stream, GAsyncResult *result, GError **error);
Finishes flushing an output stream.
|
a GOutputStream. |
|
a GAsyncResult. |
|
a GError location to store the error occuring, or NULL to
ignore.
|
Returns : |
TRUE if flush operation suceeded, FALSE otherwise.
|
void g_output_stream_close_async (GOutputStream *stream, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data);
Requests an asynchronous close of the stream, releasing resources
related to it. When the operation is finished callback
will be
called. You can then call g_output_stream_close_finish()
to get
the result of the operation.
For behaviour details see g_output_stream_close()
.
The asyncronous methods have a default fallback that uses threads to implement asynchronicity, so they are optional for inheriting classes. However, if you override one you must override all.
|
A GOutputStream. |
|
the io priority of the request. |
|
optional cancellable object |
|
callback to call when the request is satisfied |
|
the data to pass to callback function |
gboolean g_output_stream_close_finish (GOutputStream *stream, GAsyncResult *result, GError **error);
Closes an output stream.
|
a GOutputStream. |
|
a GAsyncResult. |
|
a GError location to store the error occuring, or NULL to
ignore.
|
Returns : |
TRUE if stream was successfully closed, FALSE otherwise.
|
gboolean g_output_stream_is_closed (GOutputStream *stream);
Checks if an output stream has already been closed.
|
a GOutputStream. |
Returns : |
TRUE if stream is closed. FALSE otherwise.
|
gboolean g_output_stream_has_pending (GOutputStream *stream);
Checks if an ouput stream has pending actions.
|
a GOutputStream. |
Returns : |
TRUE if stream has pending actions.
|
gboolean g_output_stream_set_pending (GOutputStream *stream, GError **error);
Sets stream
to have actions pending. If the pending flag is
already set or stream
is closed, it will return FALSE
and set
error
.
|
a GOutputStream. |
|
a GError location to store the error occuring, or NULL to
ignore.
|
Returns : |
TRUE if pending was previously unset and is now set.
|
void g_output_stream_clear_pending (GOutputStream *stream);
Clears the pending flag on stream
.
|
output stream |