summaryrefslogtreecommitdiff
path: root/gvfs
diff options
context:
space:
mode:
authorAlexander Larsson <alexl@src.gnome.org>2007-09-13 09:38:47 +0000
committerAlexander Larsson <alexl@src.gnome.org>2007-09-13 09:38:47 +0000
commitc24e38314d483b2241448e3db70d36091030d55a (patch)
treed1091e248f2abc58e04a063e467626cc6aa16aab /gvfs
parent89ef7ed95edd065c8316990f28c3f77d1da8c928 (diff)
downloadgvfs-c24e38314d483b2241448e3db70d36091030d55a.tar.gz
Added read/write_all() methods
Original git commit by Alexander Larsson <alex@localhost.localdomain> at 1161874752 +0200 svn path=/trunk/; revision=139
Diffstat (limited to 'gvfs')
-rw-r--r--gvfs/ginputstream.c57
-rw-r--r--gvfs/ginputstream.h4
-rw-r--r--gvfs/goutputstream.c57
-rw-r--r--gvfs/goutputstream.h4
4 files changed, 122 insertions, 0 deletions
diff --git a/gvfs/ginputstream.c b/gvfs/ginputstream.c
index 03c5fea0..5bb0d54b 100644
--- a/gvfs/ginputstream.c
+++ b/gvfs/ginputstream.c
@@ -160,6 +160,63 @@ g_input_stream_read (GInputStream *stream,
}
/**
+ * g_input_stream_read_all:
+ * @stream: a #GInputStream.
+ * @buffer: a buffer to read data into (which should be at least count bytes long).
+ * @count: the number of bytes that will be read from the stream
+ * @error: location to store the error occuring, or %NULL to ignore
+ *
+ * Tries to read @count bytes from the stream into the buffer starting at
+ * @buffer. Will block during this read.
+ *
+ * This function behaves like g_input_stream_read (), except it tries to
+ * read as many bytes as requested, only stopping on an error or a true
+ * end of stream.
+ *
+ * On success, the number of bytes read into the buffer is returned.
+ * On error -1 is returned and @error is set accordingly.
+ *
+ * Return value: Number of bytes read, or -1 on error
+ **/
+gssize
+g_input_stream_read_all (GInputStream *stream,
+ void *buffer,
+ gsize count,
+ GError **error)
+{
+ gsize bytes_read;
+ gssize res;
+ GError *internal_error;
+
+ bytes_read = 0;
+
+ internal_error = NULL;
+ while (bytes_read < count)
+ {
+ res = g_input_stream_read (stream, (char *)buffer + bytes_read, count - bytes_read,
+ &internal_error);
+ if (res == -1)
+ {
+ if (bytes_read == 0)
+ {
+ g_propagate_error (error, internal_error);
+ return -1;
+ }
+ else
+ {
+ g_error_free (internal_error);
+ return bytes_read;
+ }
+ }
+ if (res == 0)
+ return bytes_read;
+
+ bytes_read += res;
+ }
+ return bytes_read;
+}
+
+/**
* g_input_stream_skip:
* @stream: a #GInputStream.
* @count: the number of bytes that will be skipped from the stream
diff --git a/gvfs/ginputstream.h b/gvfs/ginputstream.h
index 266acbc5..304538d3 100644
--- a/gvfs/ginputstream.h
+++ b/gvfs/ginputstream.h
@@ -135,6 +135,10 @@ gssize g_input_stream_read (GInputStream *strea
void *buffer,
gsize count,
GError **error);
+gssize g_input_stream_read_all (GInputStream *stream,
+ void *buffer,
+ gsize count,
+ GError **error);
gssize g_input_stream_skip (GInputStream *stream,
gsize count,
GError **error);
diff --git a/gvfs/goutputstream.c b/gvfs/goutputstream.c
index 312a8c56..3b5f24c2 100644
--- a/gvfs/goutputstream.c
+++ b/gvfs/goutputstream.c
@@ -153,6 +153,63 @@ g_output_stream_write (GOutputStream *stream,
}
/**
+ * g_output_stream_write_all:
+ * @stream: a #GOutputStream.
+ * @buffer: the buffer containing the data to write.
+ * @count: the number of bytes to write
+ * @error: location to store the error occuring, or %NULL to ignore
+ *
+ * Tries to write @count bytes from @buffer into the stream. Will block
+ * during the operation.
+ *
+ * This function behaves like g_output_stream_read (), except it tries to
+ * read as many bytes as requested, only stopping on an error or a true
+ * end of stream.
+ *
+ * On success, the number of bytes written to the stream is returned.
+ * On error -1 is returned and @error is set accordingly.
+ *
+ * Return value: Number of bytes written, or -1 on error
+ **/
+gssize
+g_output_stream_write_all (GOutputStream *stream,
+ void *buffer,
+ gsize count,
+ GError **error)
+{
+ gsize bytes_written;
+ gssize res;
+ GError *internal_error;
+
+ bytes_written = 0;
+
+ internal_error = NULL;
+ while (bytes_written < count)
+ {
+ res = g_output_stream_write (stream, (char *)buffer + bytes_written, count - bytes_written,
+ &internal_error);
+ if (res == -1)
+ {
+ if (bytes_written == 0)
+ {
+ g_propagate_error (error, internal_error);
+ return -1;
+ }
+ else
+ {
+ g_error_free (internal_error);
+ return bytes_written;
+ }
+ }
+ if (res == 0)
+ return bytes_written;
+
+ bytes_written += res;
+ }
+ return bytes_written;
+}
+
+/**
* g_output_stream_flush:
* @stream: a #GOutputStream.
* @error: location to store the error occuring, or %NULL to ignore
diff --git a/gvfs/goutputstream.h b/gvfs/goutputstream.h
index cef51fae..8566e399 100644
--- a/gvfs/goutputstream.h
+++ b/gvfs/goutputstream.h
@@ -112,6 +112,10 @@ gssize g_output_stream_write (GOutputStream *str
void *buffer,
gsize count,
GError **error);
+gssize g_output_stream_write_all (GOutputStream *stream,
+ void *buffer,
+ gsize count,
+ GError **error);
gboolean g_output_stream_flush (GOutputStream *stream,
GError **error);
gboolean g_output_stream_close (GOutputStream *stream,