static gboolean g_resource_file_input_stream_can_seek (GFileInputStream *stream) { GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream); return G_IS_SEEKABLE (file->stream) && g_seekable_can_seek (G_SEEKABLE (file->stream)); }
/** * seahorse_util_read_to_memory: * @input: Data to read. The #GInputStream is read till the end. * @len: Length of the data read (out) * * Reads data from the input stream and returns them as #guchar * * Returns: The string read from data. The returned string should be freed * with #g_free when no longer needed. **/ guchar* seahorse_util_read_to_memory (GInputStream *input, guint *len) { gsize size = 128; gchar *buffer, *text; gsize nread = 0; GString *string; GSeekable *seek; if (G_IS_SEEKABLE (input)) { seek = G_SEEKABLE (input); g_seekable_seek (seek, 0, SEEK_SET, NULL, NULL); } string = g_string_new (""); buffer = g_new (gchar, size); while (g_input_stream_read_all (input, buffer, size, &nread, NULL, NULL)) { string = g_string_append_len (string, buffer, nread); if (nread != size) break; } if (len) *len = string->len; text = g_string_free (string, FALSE); g_free (buffer); return (guchar*)text; }
static gboolean gst_gio_base_src_start (GstBaseSrc * base_src) { GstGioBaseSrc *src = GST_GIO_BASE_SRC (base_src); GstGioBaseSrcClass *gbsrc_class = GST_GIO_BASE_SRC_GET_CLASS (src); src->position = 0; /* FIXME: This will likely block */ src->stream = gbsrc_class->get_stream (src); if (G_UNLIKELY (!G_IS_INPUT_STREAM (src->stream))) { GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL), ("No input stream provided by subclass")); return FALSE; } else if (G_UNLIKELY (g_input_stream_is_closed (src->stream))) { GST_ELEMENT_ERROR (src, LIBRARY, FAILED, (NULL), ("Input stream is already closed")); return FALSE; } if (G_IS_SEEKABLE (src->stream)) src->position = g_seekable_tell (G_SEEKABLE (src->stream)); GST_DEBUG_OBJECT (src, "started source"); return TRUE; }
/** * seahorse_util_memory_output_length: * @output: a stream * * A replacement for #g_memory_output_stream_get_data_size (since 2.18) * * Returns: The length of the stream */ gsize seahorse_util_memory_output_length (GMemoryOutputStream *output) { GSeekable *seekable; goffset offset, end; /* * This is a replacement for g_memory_output_stream_get_data_size() * which is not available in current version of glib. */ g_return_val_if_fail (G_IS_MEMORY_OUTPUT_STREAM (output), 0); g_return_val_if_fail (G_IS_SEEKABLE (output), 0); seekable = G_SEEKABLE (output); offset = g_seekable_tell (seekable); if (!g_seekable_seek (seekable, 0, G_SEEK_END, NULL, NULL)) g_return_val_if_reached (0); end = g_seekable_tell (seekable); if (offset != end) { if (!g_seekable_seek (seekable, offset, G_SEEK_SET, NULL, NULL)) g_return_val_if_reached (0); } return (gsize)end; }
static gboolean can_seek (GInputStream *stream) { if (!G_IS_SEEKABLE (stream)) return FALSE; return g_seekable_can_seek (G_SEEKABLE (stream)); }
/* Called from gpgme to seek a file */ static off_t vfs_data_seek (void *handle, off_t offset, int whence) { VfsAsyncHandle* ah = (VfsAsyncHandle*)handle; GSeekable *seekable = NULL; GSeekType wh; /* If the file isn't open yet, then do that now */ if (!ah->ostream && !ah->istream && ah->state == VFS_ASYNC_READY) vfs_data_open_helper (ah); /* Just in case we have an operation, like open */ if (!vfs_data_wait_results(ah, TRUE)) return (off_t)-1; g_assert (ah->state == VFS_ASYNC_READY); if (ah->writer && G_IS_SEEKABLE (ah->ostream)) seekable = G_SEEKABLE (ah->ostream); else seekable = G_SEEKABLE (ah->istream); if (!seekable || !g_seekable_can_seek (seekable)) { errno = ENOTSUP; return -1; } switch(whence) { case SEEK_SET: wh = G_SEEK_SET; break; case SEEK_CUR: wh = G_SEEK_CUR; break; case SEEK_END: wh = G_SEEK_END; break; default: g_assert_not_reached(); break; } /* All seek operations are not async */ g_clear_error (&ah->error); g_seekable_seek (seekable, (goffset)offset, wh, ah->cancellable, &ah->error); /* Start async operation */ ah->state = VFS_ASYNC_READY; if (!vfs_data_wait_results (ah, TRUE)) return -1; /* Return results */ ah->state = VFS_ASYNC_READY; return offset; }
static goffset g_resource_file_input_stream_tell (GFileInputStream *stream) { GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream);; if (!G_IS_SEEKABLE (file->stream)); return 0; return g_seekable_tell (G_SEEKABLE (file->stream)); }
arrow::Status Tell(int64_t *position) const override { if (!G_IS_SEEKABLE(output_stream_)) { std::string message("[gio-output-stream][tell] " "not seekable output stream: <"); message += G_OBJECT_CLASS_NAME(G_OBJECT_GET_CLASS(output_stream_)); message += ">"; return arrow::Status::NotImplemented(message); } *position = g_seekable_tell(G_SEEKABLE(output_stream_)); return arrow::Status::OK(); }
/* *** open_read () *** */ static void open_for_read_ready (GObject *source_object, GAsyncResult *result, gpointer user_data) { GInputStream *stream; GVfsJob *job; SoupMessage *msg; gboolean res; gboolean can_seek; GError *error; stream = G_INPUT_STREAM (source_object); error = NULL; job = G_VFS_JOB (user_data); res = g_vfs_http_input_stream_send_finish (stream, result, &error); if (res == FALSE) { g_vfs_job_failed_literal (G_VFS_JOB (job), error->domain, error->code, error->message); g_error_free (error); g_object_unref (stream); return; } msg = g_vfs_http_input_stream_get_message (stream); if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) { http_job_failed (G_VFS_JOB (job), msg); g_object_unref (msg); g_object_unref (stream); return; } g_object_unref (msg); can_seek = G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream)); g_vfs_job_open_for_read_set_can_seek (G_VFS_JOB_OPEN_FOR_READ (job), can_seek); g_vfs_job_open_for_read_set_handle (G_VFS_JOB_OPEN_FOR_READ (job), stream); g_vfs_job_succeeded (job); }
static gboolean data_wrapper_construct_from_stream_sync (CamelDataWrapper *data_wrapper, CamelStream *stream, GCancellable *cancellable, GError **error) { CamelStream *memory_stream; gssize bytes_written; camel_data_wrapper_lock ( data_wrapper, CAMEL_DATA_WRAPPER_STREAM_LOCK); /* Check for cancellation after locking. */ if (g_cancellable_set_error_if_cancelled (cancellable, error)) { camel_data_wrapper_unlock ( data_wrapper, CAMEL_DATA_WRAPPER_STREAM_LOCK); return FALSE; } if (G_IS_SEEKABLE (stream)) { if (!g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, cancellable, error)) { camel_data_wrapper_unlock (data_wrapper, CAMEL_DATA_WRAPPER_STREAM_LOCK); return FALSE; } } /* Wipe any previous contents from our byte array. */ g_byte_array_set_size (data_wrapper->priv->byte_array, 0); memory_stream = camel_stream_mem_new (); /* We retain ownership of the byte array. */ camel_stream_mem_set_byte_array ( CAMEL_STREAM_MEM (memory_stream), data_wrapper->priv->byte_array); /* Transfer incoming contents to our byte array. */ bytes_written = camel_stream_write_to_stream ( stream, memory_stream, cancellable, error); g_object_unref (memory_stream); camel_data_wrapper_unlock ( data_wrapper, CAMEL_DATA_WRAPPER_STREAM_LOCK); return (bytes_written >= 0); }
static gboolean g_resource_file_input_stream_seek (GFileInputStream *stream, goffset offset, GSeekType type, GCancellable *cancellable, GError **error) { GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream); if (!G_IS_SEEKABLE (file->stream)) { g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("Input stream doesn't implement seek")); return FALSE; } return g_seekable_seek (G_SEEKABLE (file->stream), offset, type, cancellable, error); }
static gboolean read_head_data(GInputStream *stream, guchar *buffer, gsize buffer_size, gsize *read_bytes) { GSeekable *seekable; gsize original_position; GError *error = NULL; if (!G_IS_SEEKABLE(stream)) return FALSE; seekable = G_SEEKABLE(stream); original_position = g_seekable_tell(seekable); g_seekable_seek(seekable, 0, G_SEEK_SET, NULL, &error); if (error) { chupa_error("failed to seek to the head to guess content-type: %s", error->message); g_error_free(error); return FALSE; } *read_bytes = g_input_stream_read(stream, buffer, buffer_size, NULL, &error); if (error) { chupa_error("failed to read head data to guess content-type: %s", error->message); g_error_free(error); g_seekable_seek(seekable, original_position, G_SEEK_SET, NULL, NULL); return FALSE; } g_seekable_seek(seekable, original_position, G_SEEK_SET, NULL, &error); if (error) { chupa_error("failed to re-seek to the original position " "to reset position to guess content-type: %s", error->message); g_error_free(error); return FALSE; } return TRUE; }
/* Called from gpgme to seek a file */ static off_t input_seek (void *handle, off_t offset, int whence) { GSeekable *seek; GSeekType from; GError *err = NULL; GInputStream* input = handle; g_return_val_if_fail (G_IS_INPUT_STREAM (input), -1); if (!G_IS_SEEKABLE (input)) { errno = EOPNOTSUPP; return -1; } switch(whence) { case SEEK_SET: from = G_SEEK_SET; break; case SEEK_CUR: from = G_SEEK_CUR; break; case SEEK_END: from = G_SEEK_END; break; default: g_assert_not_reached(); break; }; seek = G_SEEKABLE (input); if (!g_seekable_seek (seek, offset, from, NULL, &err)) return handle_gio_error (err); return offset; }
static CamelMimePart * multipart_signed_get_part (CamelMultipart *multipart, guint index) { CamelMultipartSigned *mps = (CamelMultipartSigned *) multipart; CamelDataWrapper *data_wrapper; CamelStream *stream; GByteArray *byte_array; data_wrapper = CAMEL_DATA_WRAPPER (multipart); byte_array = camel_data_wrapper_get_byte_array (data_wrapper); switch (index) { case CAMEL_MULTIPART_SIGNED_CONTENT: if (mps->content) return mps->content; if (mps->contentraw) { g_return_val_if_fail ( G_IS_SEEKABLE (mps->contentraw), NULL); stream = g_object_ref (mps->contentraw); } else if (mps->start1 == -1 && multipart_signed_parse_content (mps) == -1 && byte_array->len == 0) { g_warning ("Trying to get content on an invalid multipart/signed"); return NULL; } else if (byte_array->len == 0) { return NULL; } else if (mps->start1 == -1) { stream = camel_stream_mem_new (); camel_stream_mem_set_byte_array ( CAMEL_STREAM_MEM (stream), byte_array); } else { stream = multipart_signed_clip_stream ( mps, mps->start1, mps->end1); } g_seekable_seek ( G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, NULL); mps->content = camel_mime_part_new (); camel_data_wrapper_construct_from_stream_sync ( CAMEL_DATA_WRAPPER (mps->content), stream, NULL, NULL); g_object_unref (stream); return mps->content; case CAMEL_MULTIPART_SIGNED_SIGNATURE: if (mps->signature) return mps->signature; if (mps->start1 == -1 && multipart_signed_parse_content (mps) == -1) { g_warning ("Trying to get signature on invalid multipart/signed"); return NULL; } else if (byte_array->len == 0) { return NULL; } stream = multipart_signed_clip_stream ( mps, mps->start2, mps->end2); g_seekable_seek ( G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, NULL); mps->signature = camel_mime_part_new (); camel_data_wrapper_construct_from_stream_sync ( CAMEL_DATA_WRAPPER (mps->signature), stream, NULL, NULL); g_object_unref (stream); return mps->signature; default: g_warning ("trying to get object out of bounds for multipart"); } return NULL; }