More documentation cleanup and filling in missing information, bringing
[platform/upstream/glib.git] / gio / goutputstream.c
index 931e764..f827c81 100644 (file)
 #include "gsimpleasyncresult.h"
 #include "glibintl.h"
 
+#include "gioalias.h"
+
 /**
  * SECTION:goutputstream
- * @short_description: base class for implementing streaming output
- * 
- * 
+ * @short_description: Base class for implementing streaming output
+ *
+ *
  *
  **/
 
@@ -170,11 +172,11 @@ g_output_stream_init (GOutputStream *stream)
  * Return value: Number of bytes written, or -1 on error
  **/
 gssize
-g_output_stream_write (GOutputStream *stream,
-                      const void    *buffer,
-                      gsize          count,
-                      GCancellable  *cancellable,
-                      GError       **error)
+g_output_stream_write (GOutputStream  *stream,
+                      const void     *buffer,
+                      gsize           count,
+                      GCancellable   *cancellable,
+                      GError        **error)
 {
   GOutputStreamClass *class;
   gssize res;
@@ -192,39 +194,28 @@ g_output_stream_write (GOutputStream *stream,
       return -1;
     }
 
-  if (stream->priv->closed)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
-                  _("Stream is already closed"));
-      return -1;
-    }
-  
-  if (stream->priv->pending)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
-                  _("Stream has outstanding operation"));
-      return -1;
-    }
-  
   class = G_OUTPUT_STREAM_GET_CLASS (stream);
 
-  if (class->write == NULL) 
+  if (class->write_fn == NULL) 
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
                   _("Output stream doesn't implement write"));
       return -1;
     }
   
+  if (!g_output_stream_set_pending (stream, error))
+    return -1;
+  
   if (cancellable)
     g_push_current_cancellable (cancellable);
   
-  stream->priv->pending = TRUE;
-  res = class->write (stream, buffer, count, cancellable, error);
-  stream->priv->pending = FALSE;
+  res = class->write_fn (stream, buffer, count, cancellable, error);
   
   if (cancellable)
     g_pop_current_cancellable (cancellable);
   
+  g_output_stream_clear_pending (stream);
+
   return res; 
 }
 
@@ -233,7 +224,8 @@ g_output_stream_write (GOutputStream *stream,
  * @stream: a #GOutputStream.
  * @buffer: the buffer containing the data to write. 
  * @count: the number of bytes to write
- * @bytes_written: location to store the number of bytes that was written to the stream
+ * @bytes_written: location to store the number of bytes that was 
+ *     written to the stream
  * @cancellable: optional #GCancellable object, %NULL to ignore.
  * @error: location to store the error occuring, or %NULL to ignore
  *
@@ -253,12 +245,12 @@ g_output_stream_write (GOutputStream *stream,
  * Return value: %TRUE on success, %FALSE if there was an error
  **/
 gboolean
-g_output_stream_write_all (GOutputStream *stream,
-                          const void    *buffer,
-                          gsize          count,
-                          gsize         *bytes_written,
-                          GCancellable  *cancellable,
-                          GError       **error)
+g_output_stream_write_all (GOutputStream  *stream,
+                          const void     *buffer,
+                          gsize           count,
+                          gsize          *bytes_written,
+                          GCancellable   *cancellable,
+                          GError        **error)
 {
   gsize _bytes_written;
   gssize res;
@@ -286,6 +278,7 @@ g_output_stream_write_all (GOutputStream *stream,
   
   if (bytes_written)
     *bytes_written = _bytes_written;
+
   return TRUE;
 }
 
@@ -295,8 +288,8 @@ g_output_stream_write_all (GOutputStream *stream,
  * @cancellable: optional cancellable object
  * @error: location to store the error occuring, or %NULL to ignore
  *
- * Flushed any outstanding buffers in the stream. Will block during the operation.
- * Closing the stream will implicitly cause a flush.
+ * 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.
  * 
@@ -307,28 +300,17 @@ g_output_stream_write_all (GOutputStream *stream,
  * Return value: %TRUE on success, %FALSE on error
  **/
 gboolean
-g_output_stream_flush (GOutputStream    *stream,
-                      GCancellable  *cancellable,
-                      GError          **error)
+g_output_stream_flush (GOutputStream  *stream,
+                       GCancellable   *cancellable,
+                       GError        **error)
 {
   GOutputStreamClass *class;
   gboolean res;
 
   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
 
-  if (stream->priv->closed)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
-                  _("Stream is already closed"));
-      return FALSE;
-    }
-
-  if (stream->priv->pending)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
-                  _("Stream has outstanding operation"));
-      return FALSE;
-    }
+  if (!g_output_stream_set_pending (stream, error))
+    return FALSE;
   
   class = G_OUTPUT_STREAM_GET_CLASS (stream);
 
@@ -338,14 +320,14 @@ g_output_stream_flush (GOutputStream    *stream,
       if (cancellable)
        g_push_current_cancellable (cancellable);
       
-      stream->priv->pending = TRUE;
       res = class->flush (stream, cancellable, error);
-      stream->priv->pending = FALSE;
       
       if (cancellable)
        g_pop_current_cancellable (cancellable);
     }
   
+  g_output_stream_clear_pending (stream);
+
   return res;
 }
 
@@ -363,11 +345,11 @@ g_output_stream_flush (GOutputStream    *stream,
  * Returns: a #gssize containig the size of the data spliced.
  **/
 gssize
-g_output_stream_splice (GOutputStream *stream,
-                       GInputStream *source,
-                       GOutputStreamSpliceFlags flags,
-                       GCancellable  *cancellable,
-                       GError **error)
+g_output_stream_splice (GOutputStream             *stream,
+                       GInputStream              *source,
+                       GOutputStreamSpliceFlags   flags,
+                       GCancellable              *cancellable,
+                       GError                   **error)
 {
   GOutputStreamClass *class;
   gboolean res;
@@ -375,13 +357,6 @@ g_output_stream_splice (GOutputStream *stream,
   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
   g_return_val_if_fail (G_IS_INPUT_STREAM (source), -1);
 
-  if (stream->priv->closed)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
-                  _("Target stream is already closed"));
-      return -1;
-    }
-
   if (g_input_stream_is_closed (source))
     {
       g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
@@ -389,12 +364,8 @@ g_output_stream_splice (GOutputStream *stream,
       return -1;
     }
 
-  if (stream->priv->pending)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
-                  _("Stream has outstanding operation"));
-      return -1;
-    }
+  if (!g_output_stream_set_pending (stream, error))
+    return -1;
   
   class = G_OUTPUT_STREAM_GET_CLASS (stream);
 
@@ -402,29 +373,38 @@ g_output_stream_splice (GOutputStream *stream,
   if (cancellable)
     g_push_current_cancellable (cancellable);
       
-  stream->priv->pending = TRUE;
   res = class->splice (stream, source, flags, cancellable, error);
-  stream->priv->pending = FALSE;
       
   if (cancellable)
     g_pop_current_cancellable (cancellable);
   
+  g_output_stream_clear_pending (stream);
+
   return res;
 }
 
 static gssize
-g_output_stream_real_splice (GOutputStream *stream,
-                            GInputStream *source,
-                            GOutputStreamSpliceFlags flags,
-                            GCancellable  *cancellable,
-                            GError **error)
+g_output_stream_real_splice (GOutputStream             *stream,
+                             GInputStream              *source,
+                             GOutputStreamSpliceFlags   flags,
+                             GCancellable              *cancellable,
+                             GError                   **error)
 {
+  GOutputStreamClass *class = G_OUTPUT_STREAM_GET_CLASS (stream);
   gssize n_read, n_written;
   gssize bytes_copied;
   char buffer[8192], *p;
   gboolean res;
 
   bytes_copied = 0;
+  if (class->write_fn == NULL) 
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+                  _("Output stream doesn't implement write"));
+      res = FALSE;
+      goto notsupported;
+    }
+  
   res = TRUE;
   do 
     {
@@ -441,9 +421,7 @@ g_output_stream_real_splice (GOutputStream *stream,
       p = buffer;
       while (n_read > 0)
        {
-         stream->priv->pending = FALSE;
-         n_written = g_output_stream_write (stream, p, n_read, cancellable, error);
-         stream->priv->pending = TRUE;
+         n_written = class->write_fn (stream, p, n_read, cancellable, error);
          if (n_written == -1)
            {
              res = FALSE;
@@ -457,6 +435,7 @@ g_output_stream_real_splice (GOutputStream *stream,
     }
   while (res);
 
+ notsupported:
   if (!res)
     error = NULL; /* Ignore further errors */
 
@@ -469,10 +448,8 @@ g_output_stream_real_splice (GOutputStream *stream,
   if (flags & G_OUTPUT_STREAM_SPLICE_FLAGS_CLOSE_TARGET)
     {
       /* But write errors on close are bad! */
-      stream->priv->pending = FALSE;
-      if (!g_output_stream_close (stream, cancellable, error))
+      if (!class->close_fn (stream, cancellable, error))
        res = FALSE;
-      stream->priv->pending = TRUE;
     }
 
   if (res)
@@ -535,66 +512,63 @@ g_output_stream_close (GOutputStream  *stream,
   if (stream->priv->closed)
     return TRUE;
 
-  if (stream->priv->pending)
-    {
-      g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
-                  _("Stream has outstanding operation"));
-      return FALSE;
-    }
-
-  res = g_output_stream_flush (stream, cancellable, error);
+  if (!g_output_stream_set_pending (stream, error))
+    return FALSE;
 
-  stream->priv->pending = TRUE;
-  
   if (cancellable)
     g_push_current_cancellable (cancellable);
 
+  if (class->flush)
+    res = class->flush (stream, cancellable, error);
+  else
+    res = TRUE;
+  
   if (!res)
     {
       /* flushing caused the error that we want to return,
        * but we still want to close the underlying stream if possible
        */
-      if (class->close)
-       class->close (stream, cancellable, NULL);
+      if (class->close_fn)
+       class->close_fn (stream, cancellable, NULL);
     }
   else
     {
       res = TRUE;
-      if (class->close)
-       res = class->close (stream, cancellable, error);
+      if (class->close_fn)
+       res = class->close_fn (stream, cancellable, error);
     }
   
   if (cancellable)
     g_pop_current_cancellable (cancellable);
   
   stream->priv->closed = TRUE;
-  stream->priv->pending = FALSE;
+  g_output_stream_clear_pending (stream);
   
   return res;
 }
 
 static void
-async_ready_callback_wrapper (GObject *source_object,
-                             GAsyncResult *res,
-                             gpointer      user_data)
+async_ready_callback_wrapper (GObject      *source_object,
+                              GAsyncResult *res,
+                              gpointer      user_data)
 {
   GOutputStream *stream = G_OUTPUT_STREAM (source_object);
 
-  stream->priv->pending = FALSE;
+  g_output_stream_clear_pending (stream);
   if (stream->priv->outstanding_callback)
     (*stream->priv->outstanding_callback) (source_object, res, user_data);
   g_object_unref (stream);
 }
 
 static void
-async_ready_close_callback_wrapper (GObject *source_object,
-                                   GAsyncResult *res,
-                                   gpointer user_data)
+async_ready_close_callback_wrapper (GObject      *source_object,
+                                    GAsyncResult *res,
+                                    gpointer      user_data)
 {
   GOutputStream *stream = G_OUTPUT_STREAM (source_object);
 
-  stream->priv->pending = FALSE;
   stream->priv->closed = TRUE;
+  g_output_stream_clear_pending (stream);
   if (stream->priv->outstanding_callback)
     (*stream->priv->outstanding_callback) (source_object, res, user_data);
   g_object_unref (stream);
@@ -605,33 +579,36 @@ async_ready_close_callback_wrapper (GObject *source_object,
  * @stream: A #GOutputStream.
  * @buffer: the buffer containing the data to write. 
  * @count: the number of bytes to write
- * @io_priority: the io priority of the request. the io priority of the request
+ * @io_priority: the io priority of the request.
  * @cancellable: optional #GCancellable object, %NULL to ignore.
  * @callback: callback to call when the request is satisfied
  * @user_data: the data to pass to callback function
  *
- * Request an asynchronous write of @count bytes from @buffer into the stream.
- * When the operation is finished @callback will be called, giving the results.
+ * Request an asynchronous write of @count bytes from @buffer into 
+ * the stream. When the operation is finished @callback will be called, 
+ * giving the results.
  *
- * During an async request no other sync and async calls are allowed, and will
- * result in %G_IO_ERROR_PENDING errors. 
+ * 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.
+ * 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. 
+ * @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.
+ * 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.
+ * 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().
+ * For the synchronous, blocking version of this function, see 
+ * g_output_stream_write().
  **/
 void
 g_output_stream_write_async (GOutputStream       *stream,
@@ -644,6 +621,7 @@ g_output_stream_write_async (GOutputStream       *stream,
 {
   GOutputStreamClass *class;
   GSimpleAsyncResult *simple;
+  GError *error = NULL;
 
   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
   g_return_if_fail (buffer != NULL);
@@ -669,29 +647,18 @@ g_output_stream_write_async (GOutputStream       *stream,
       return;
     }
 
-  if (stream->priv->closed)
+  if (!g_output_stream_set_pending (stream, &error))
     {
-      g_simple_async_report_error_in_idle (G_OBJECT (stream),
-                                          callback,
-                                          user_data,
-                                          G_IO_ERROR, G_IO_ERROR_CLOSED,
-                                          _("Stream is already closed"));
+      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+                                           callback,
+                                           user_data,
+                                           error);
+      g_error_free (error);
       return;
     }
   
-  if (stream->priv->pending)
-    {
-      g_simple_async_report_error_in_idle (G_OBJECT (stream),
-                                          callback,
-                                          user_data,
-                                          G_IO_ERROR, G_IO_ERROR_PENDING,
-                                          _("Stream has outstanding operation"));
-      return;
-    }
-
   class = G_OUTPUT_STREAM_GET_CLASS (stream);
 
-  stream->priv->pending = TRUE;
   stream->priv->outstanding_callback = callback;
   g_object_ref (stream);
   class->write_async (stream, buffer, count, io_priority, cancellable,
@@ -710,9 +677,9 @@ g_output_stream_write_async (GOutputStream       *stream,
  * Returns: a #gssize containing the number of bytes written to the stream.
  **/
 gssize
-g_output_stream_write_finish (GOutputStream *stream,
-                             GAsyncResult *result,
-                             GError **error)
+g_output_stream_write_finish (GOutputStream  *stream,
+                              GAsyncResult   *result,
+                              GError        **error)
 {
   GSimpleAsyncResult *simple;
   GOutputStreamClass *class;
@@ -742,14 +709,14 @@ typedef struct {
 } SpliceUserData;
 
 static void
-async_ready_splice_callback_wrapper (GObject *source_object,
-                                    GAsyncResult *res,
-                                    gpointer _data)
+async_ready_splice_callback_wrapper (GObject      *source_object,
+                                     GAsyncResult *res,
+                                     gpointer     _data)
 {
   GOutputStream *stream = G_OUTPUT_STREAM (source_object);
   SpliceUserData *data = _data;
   
-  stream->priv->pending = FALSE;
+  g_output_stream_clear_pending (stream);
   
   if (data->callback)
     (*data->callback) (source_object, res, data->user_data);
@@ -773,30 +740,21 @@ async_ready_splice_callback_wrapper (GObject *source_object,
  * 
  **/
 void
-g_output_stream_splice_async (GOutputStream             *stream,
-                             GInputStream              *source,
-                             GOutputStreamSpliceFlags   flags,
-                             int                        io_priority,
-                             GCancellable              *cancellable,
-                             GAsyncReadyCallback        callback,
-                             gpointer                   user_data)
+g_output_stream_splice_async (GOutputStream            *stream,
+                             GInputStream             *source,
+                             GOutputStreamSpliceFlags  flags,
+                             int                       io_priority,
+                             GCancellable             *cancellable,
+                             GAsyncReadyCallback       callback,
+                             gpointer                  user_data)
 {
   GOutputStreamClass *class;
   SpliceUserData *data;
+  GError *error = NULL;
 
   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
   g_return_if_fail (G_IS_INPUT_STREAM (source));
 
-  if (stream->priv->closed)
-    {
-      g_simple_async_report_error_in_idle (G_OBJECT (stream),
-                                          callback,
-                                          user_data,
-                                          G_IO_ERROR, G_IO_ERROR_CLOSED,
-                                          _("Target stream is already closed"));
-      return;
-    }
-
   if (g_input_stream_is_closed (source))
     {
       g_simple_async_report_error_in_idle (G_OBJECT (stream),
@@ -807,20 +765,18 @@ g_output_stream_splice_async (GOutputStream             *stream,
       return;
     }
   
-  if (stream->priv->pending)
+  if (!g_output_stream_set_pending (stream, &error))
     {
-      g_simple_async_report_error_in_idle (G_OBJECT (stream),
-                                          callback,
-                                          user_data,
-                                          G_IO_ERROR, G_IO_ERROR_PENDING,
-                                          _("Stream has outstanding operation"));
+      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+                                           callback,
+                                           user_data,
+                                           error);
+      g_error_free (error);
       return;
     }
 
   class = G_OUTPUT_STREAM_GET_CLASS (stream);
 
-  stream->priv->pending = TRUE;
-
   data = g_new0 (SpliceUserData, 1);
   data->callback = callback;
   data->user_data = user_data;
@@ -843,9 +799,9 @@ g_output_stream_splice_async (GOutputStream             *stream,
  * Returns: a #gssize of the number of bytes spliced.
  **/
 gssize
-g_output_stream_splice_finish (GOutputStream             *stream,
-                              GAsyncResult              *result,
-                              GError                   **error)
+g_output_stream_splice_finish (GOutputStream  *stream,
+                              GAsyncResult   *result,
+                              GError        **error)
 {
   GSimpleAsyncResult *simple;
   GOutputStreamClass *class;
@@ -877,42 +833,36 @@ g_output_stream_splice_finish (GOutputStream             *stream,
  **/
 void
 g_output_stream_flush_async (GOutputStream       *stream,
-                            int                  io_priority,
-                            GCancellable        *cancellable,
-                            GAsyncReadyCallback  callback,
-                            gpointer             user_data)
+                             int                  io_priority,
+                             GCancellable        *cancellable,
+                             GAsyncReadyCallback  callback,
+                             gpointer             user_data)
 {
   GOutputStreamClass *class;
   GSimpleAsyncResult *simple;
+  GError *error = NULL;
 
   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
 
-  if (stream->priv->closed)
-    {
-      g_simple_async_report_error_in_idle (G_OBJECT (stream),
-                                          callback,
-                                          user_data,
-                                          G_IO_ERROR, G_IO_ERROR_CLOSED,
-                                          _("Stream is already closed"));
-      return;
-    }
-  
-  if (stream->priv->pending)
+  if (!g_output_stream_set_pending (stream, &error))
     {
-      g_simple_async_report_error_in_idle (G_OBJECT (stream),
-                                          callback,
-                                          user_data,
-                                          G_IO_ERROR, G_IO_ERROR_PENDING,
-                                          _("Stream has outstanding operation"));
+      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+                                           callback,
+                                           user_data,
+                                           error);
+      g_error_free (error);
       return;
     }
 
+  stream->priv->outstanding_callback = callback;
+  g_object_ref (stream);
+
   class = G_OUTPUT_STREAM_GET_CLASS (stream);
   
   if (class->flush_async == NULL)
     {
       simple = g_simple_async_result_new (G_OBJECT (stream),
-                                         callback,
+                                         async_ready_callback_wrapper,
                                          user_data,
                                          g_output_stream_flush_async);
       g_simple_async_result_complete_in_idle (simple);
@@ -920,9 +870,6 @@ g_output_stream_flush_async (GOutputStream       *stream,
       return;
     }
       
-  stream->priv->pending = TRUE;
-  stream->priv->outstanding_callback = callback;
-  g_object_ref (stream);
   class->flush_async (stream, io_priority, cancellable,
                      async_ready_callback_wrapper, user_data);
 }
@@ -939,9 +886,9 @@ g_output_stream_flush_async (GOutputStream       *stream,
  * Returns: %TRUE if flush operation suceeded, %FALSE otherwise.
  **/
 gboolean
-g_output_stream_flush_finish (GOutputStream *stream,
-                             GAsyncResult *result,
-                             GError **error)
+g_output_stream_flush_finish (GOutputStream  *stream,
+                              GAsyncResult   *result,
+                              GError        **error)
 {
   GSimpleAsyncResult *simple;
   GOutputStreamClass *klass;
@@ -968,28 +915,31 @@ g_output_stream_flush_finish (GOutputStream *stream,
 /**
  * g_output_stream_close_async:
  * @stream: A #GOutputStream.
+ * @io_priority: the io priority of the request.
  * @callback: callback to call when the request is satisfied
  * @user_data: the data to pass to callback function
  * @cancellable: optional cancellable object
  *
- * Requests an asynchronous closes of the stream, releasing resources related to it.
- * When the operation is finished @callback will be called, giving the results.
+ * Requests an asynchronous close of the stream, releasing resources 
+ * related to it. When the operation is finished @callback will be 
+ * called, giving the results.
  *
  * 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.
+ * 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.
  **/
 void
-g_output_stream_close_async (GOutputStream      *stream,
-                            int                 io_priority,
-                            GCancellable       *cancellable,
-                            GAsyncReadyCallback callback,
-                            gpointer            user_data)
+g_output_stream_close_async (GOutputStream       *stream,
+                             int                  io_priority,
+                             GCancellable        *cancellable,
+                             GAsyncReadyCallback  callback,
+                             gpointer             user_data)
 {
   GOutputStreamClass *class;
   GSimpleAsyncResult *simple;
+  GError *error = NULL;
 
   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
   
@@ -1004,18 +954,17 @@ g_output_stream_close_async (GOutputStream      *stream,
       return;
     }
 
-  if (stream->priv->pending)
+  if (!g_output_stream_set_pending (stream, &error))
     {
-      g_simple_async_report_error_in_idle (G_OBJECT (stream),
-                                          callback,
-                                          user_data,
-                                          G_IO_ERROR, G_IO_ERROR_PENDING,
-                                          _("Stream has outstanding operation"));
+      g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
+                                           callback,
+                                           user_data,
+                                           error);
+      g_error_free (error);
       return;
     }
   
   class = G_OUTPUT_STREAM_GET_CLASS (stream);
-  stream->priv->pending = TRUE;
   stream->priv->outstanding_callback = callback;
   g_object_ref (stream);
   class->close_async (stream, io_priority, cancellable,
@@ -1034,9 +983,9 @@ g_output_stream_close_async (GOutputStream      *stream,
  * Returns: %TRUE if stream was successfully closed, %FALSE otherwise.
  **/
 gboolean
-g_output_stream_close_finish (GOutputStream *stream,
-                             GAsyncResult *result,
-                             GError **error)
+g_output_stream_close_finish (GOutputStream  *stream,
+                              GAsyncResult   *result,
+                              GError        **error)
 {
   GSimpleAsyncResult *simple;
   GOutputStreamClass *class;
@@ -1094,17 +1043,51 @@ g_output_stream_has_pending (GOutputStream *stream)
 /**
  * g_output_stream_set_pending:
  * @stream: a #GOutputStream.
- * @pending: a #gboolean.
+ * @error: a #GError location to store the error occuring, or %NULL to 
+ * ignore.
  * 
- * Sets the @stream as having pending actions if @pending is %TRUE. 
+ * Sets @stream to have actions pending. If the pending flag is
+ * already set or @stream is closed, it will return %FALSE and set
+ * @error.
+ *
+ * Return value: %TRUE if pending was previously unset and is now set.
+ **/
+gboolean
+g_output_stream_set_pending (GOutputStream *stream,
+                            GError **error)
+{
+  g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
+  
+  if (stream->priv->closed)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED,
+                  _("Stream is already closed"));
+      return FALSE;
+    }
+  
+  if (stream->priv->pending)
+    {
+      g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING,
+                  _("Stream has outstanding operation"));
+      return FALSE;
+    }
+  
+  stream->priv->pending = TRUE;
+  return TRUE;
+}
+
+/**
+ * g_output_stream_clear_pending:
+ * @stream: output stream
+ * 
+ * Clears the pending flag on @stream.
  **/
 void
-g_output_stream_set_pending (GOutputStream              *stream,
-                           gboolean                   pending)
+g_output_stream_clear_pending (GOutputStream *stream)
 {
   g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
   
-  stream->priv->pending = pending;
+  stream->priv->pending = FALSE;
 }
 
 
@@ -1120,8 +1103,8 @@ typedef struct {
 
 static void
 write_async_thread (GSimpleAsyncResult *res,
-                  GObject *object,
-                  GCancellable *cancellable)
+                    GObject            *object,
+                    GCancellable       *cancellable)
 {
   WriteData *op;
   GOutputStreamClass *class;
@@ -1129,8 +1112,8 @@ write_async_thread (GSimpleAsyncResult *res,
 
   class = G_OUTPUT_STREAM_GET_CLASS (object);
   op = g_simple_async_result_get_op_res_gpointer (res);
-  op->count_written = class->write (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
-                                   cancellable, &error);
+  op->count_written = class->write_fn (G_OUTPUT_STREAM (object), op->buffer, op->count_requested,
+                                      cancellable, &error);
   if (op->count_written == -1)
     {
       g_simple_async_result_set_from_error (res, error);
@@ -1140,12 +1123,12 @@ write_async_thread (GSimpleAsyncResult *res,
 
 static void
 g_output_stream_real_write_async (GOutputStream       *stream,
-                                 const void          *buffer,
-                                 gsize                count,
-                                 int                  io_priority,
-                                 GCancellable        *cancellable,
-                                 GAsyncReadyCallback  callback,
-                                 gpointer             user_data)
+                                  const void          *buffer,
+                                  gsize                count,
+                                  int                  io_priority,
+                                  GCancellable        *cancellable,
+                                  GAsyncReadyCallback  callback,
+                                  gpointer             user_data)
 {
   GSimpleAsyncResult *res;
   WriteData *op;
@@ -1161,9 +1144,9 @@ g_output_stream_real_write_async (GOutputStream       *stream,
 }
 
 static gssize
-g_output_stream_real_write_finish (GOutputStream *stream,
-                                  GAsyncResult *result,
-                                  GError **error)
+g_output_stream_real_write_finish (GOutputStream  *stream,
+                                   GAsyncResult   *result,
+                                   GError        **error)
 {
   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
   WriteData *op;
@@ -1181,8 +1164,8 @@ typedef struct {
 
 static void
 splice_async_thread (GSimpleAsyncResult *result,
-                    GObject *object,
-                    GCancellable *cancellable)
+                     GObject            *object,
+                     GCancellable       *cancellable)
 {
   SpliceData *op;
   GOutputStreamClass *class;
@@ -1193,15 +1176,11 @@ splice_async_thread (GSimpleAsyncResult *result,
   class = G_OUTPUT_STREAM_GET_CLASS (object);
   op = g_simple_async_result_get_op_res_gpointer (result);
   
-  stream->priv->pending = FALSE;
-  op->bytes_copied =
-    g_output_stream_splice (stream,
-                           op->source,
-                           op->flags,
-                           cancellable,
-                           &error);
-  stream->priv->pending = TRUE;
-
+  op->bytes_copied = class->splice (stream,
+                                   op->source,
+                                   op->flags,
+                                   cancellable,
+                                   &error);
   if (op->bytes_copied == -1)
     {
       g_simple_async_result_set_from_error (result, error);
@@ -1210,13 +1189,13 @@ splice_async_thread (GSimpleAsyncResult *result,
 }
 
 static void
-g_output_stream_real_splice_async  (GOutputStream             *stream,
-                                   GInputStream              *source,
-                                   GOutputStreamSpliceFlags   flags,
-                                   int                        io_priority,
-                                   GCancellable              *cancellable,
-                                   GAsyncReadyCallback        callback,
-                                   gpointer                   user_data)
+g_output_stream_real_splice_async (GOutputStream             *stream,
+                                   GInputStream              *source,
+                                   GOutputStreamSpliceFlags   flags,
+                                   int                        io_priority,
+                                   GCancellable              *cancellable,
+                                   GAsyncReadyCallback        callback,
+                                   gpointer                   user_data)
 {
   GSimpleAsyncResult *res;
   SpliceData *op;
@@ -1235,9 +1214,9 @@ g_output_stream_real_splice_async  (GOutputStream             *stream,
 }
 
 static gssize
-g_output_stream_real_splice_finish (GOutputStream             *stream,
-                                   GAsyncResult              *result,
-                                   GError                   **error)
+g_output_stream_real_splice_finish (GOutputStream  *stream,
+                                    GAsyncResult   *result,
+                                    GError        **error)
 {
   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
   SpliceData *op;
@@ -1248,11 +1227,10 @@ g_output_stream_real_splice_finish (GOutputStream             *stream,
 }
 
 
-
 static void
 flush_async_thread (GSimpleAsyncResult *res,
-                   GObject *object,
-                   GCancellable *cancellable)
+                    GObject            *object,
+                    GCancellable       *cancellable)
 {
   GOutputStreamClass *class;
   gboolean result;
@@ -1272,10 +1250,10 @@ flush_async_thread (GSimpleAsyncResult *res,
 
 static void
 g_output_stream_real_flush_async (GOutputStream       *stream,
-                                 int                  io_priority,
-                                 GCancellable        *cancellable,
-                                 GAsyncReadyCallback  callback,
-                                 gpointer             user_data)
+                                  int                  io_priority,
+                                  GCancellable        *cancellable,
+                                  GAsyncReadyCallback  callback,
+                                  gpointer             user_data)
 {
   GSimpleAsyncResult *res;
 
@@ -1286,17 +1264,17 @@ g_output_stream_real_flush_async (GOutputStream       *stream,
 }
 
 static gboolean
-g_output_stream_real_flush_finish (GOutputStream *stream,
-                                  GAsyncResult *result,
-                                  GError **error)
+g_output_stream_real_flush_finish (GOutputStream  *stream,
+                                   GAsyncResult   *result,
+                                   GError        **error)
 {
   return TRUE;
 }
 
 static void
 close_async_thread (GSimpleAsyncResult *res,
-                   GObject *object,
-                   GCancellable *cancellable)
+                    GObject            *object,
+                    GCancellable       *cancellable)
 {
   GOutputStreamClass *class;
   GError *error = NULL;
@@ -1308,7 +1286,7 @@ close_async_thread (GSimpleAsyncResult *res,
      open handles */
   
   class = G_OUTPUT_STREAM_GET_CLASS (object);
-  result = class->close (G_OUTPUT_STREAM (object), cancellable, &error);
+  result = class->close_fn (G_OUTPUT_STREAM (object), cancellable, &error);
   if (!result)
     {
       g_simple_async_result_set_from_error (res, error);
@@ -1317,11 +1295,11 @@ close_async_thread (GSimpleAsyncResult *res,
 }
 
 static void
-g_output_stream_real_close_async (GOutputStream      *stream,
-                                 int                 io_priority,
-                                 GCancellable       *cancellable,
-                                 GAsyncReadyCallback callback,
-                                 gpointer            user_data)
+g_output_stream_real_close_async (GOutputStream       *stream,
+                                  int                  io_priority,
+                                  GCancellable        *cancellable,
+                                  GAsyncReadyCallback  callback,
+                                  gpointer             user_data)
 {
   GSimpleAsyncResult *res;
   
@@ -1334,11 +1312,14 @@ g_output_stream_real_close_async (GOutputStream      *stream,
 }
 
 static gboolean
-g_output_stream_real_close_finish (GOutputStream              *stream,
-                                  GAsyncResult              *result,
-                                  GError                   **error)
+g_output_stream_real_close_finish (GOutputStream  *stream,
+                                   GAsyncResult   *result,
+                                   GError        **error)
 {
   GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (result);
   g_assert (g_simple_async_result_get_source_tag (simple) == g_output_stream_real_close_async);
   return TRUE;
 }
+
+#define __G_OUTPUT_STREAM_C__
+#include "gioaliasdef.c"