diff -r dfa72795bd32 -r 1374c8f47f33 libgnomevfs2-mythtv/modules/mythtv-method.c --- a/libgnomevfs2-mythtv/modules/mythtv-method.c Fri Nov 24 21:19:49 2006 +0000 +++ b/libgnomevfs2-mythtv/modules/mythtv-method.c Mon Nov 27 22:07:09 2006 +0000 @@ -48,8 +48,7 @@ guint64 content_size; guint64 bytes_read; - guint8 *buffer; - gsize buffer_offset; + GByteArray *buffer; gsize buffer_remain; } MythtvHandle; @@ -119,8 +118,7 @@ myth_handle->content_size = myth_handle->file_transfer->filesize; - myth_handle->buffer = g_malloc0 (MYTHTV_BUFFER_SIZE); - myth_handle->buffer_offset = 0; + myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE); myth_handle->buffer_remain = 0; *method_handle = (GnomeVFSMethodHandle *) myth_handle; @@ -139,9 +137,9 @@ MythtvHandle *myth_handle = (MythtvHandle *) method_handle; GnomeVFSFileSize bytes_to_read; - g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu, buffer_offset: %du, buffer_remain: %du", + g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu, buffer_remain: %du", myth_handle->content_size, myth_handle->bytes_read, num_bytes, - myth_handle->buffer_offset, myth_handle->buffer_remain); + myth_handle->buffer_remain); *bytes_read = 0; @@ -156,43 +154,42 @@ /* Loop sending the Myth File Transfer request: * Retry whilst authentication fails and we supply it. */ + + if ( bytes_to_read > myth_handle->buffer_remain ) { + GByteArray *tmp_buffer = g_byte_array_new(); - if (bytes_to_read > myth_handle->buffer_remain ) { - guint8 *tmp_buffer = g_malloc0 (MYTHTV_BUFFER_SIZE); - gint len; + gint len = gmyth_file_transfer_read( myth_handle->file_transfer, + tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE ); - if (myth_handle->buffer_remain > (MYTHTV_BUFFER_SIZE >> 1)) { - // Avoid overlap creating another buffer, in the case the remaining data is bigger than half buffer size - tmp_buffer = g_malloc0 (MYTHTV_BUFFER_SIZE); - memcpy (tmp_buffer, myth_handle->buffer + myth_handle->buffer_offset, myth_handle->buffer_remain); - g_free (myth_handle->buffer); - myth_handle->buffer = tmp_buffer; - } else { - memcpy (myth_handle->buffer, myth_handle->buffer + myth_handle->buffer_offset, myth_handle->buffer_remain); - } + if (len < 0) + return GNOME_VFS_ERROR_IO; - g_debug ("Reading %d data from backend", MYTHTV_BUFFER_SIZE- myth_handle->buffer_remain); + myth_handle->buffer = g_byte_array_append( myth_handle->buffer, + g_memdup( tmp_buffer->data, len ), len ); + + myth_handle->buffer_remain += len; + + g_debug ( "Reading %d data from backend", MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain ); - len = gmyth_file_transfer_read( myth_handle->file_transfer, - myth_handle->buffer + myth_handle->buffer_remain, - MYTHTV_BUFFER_SIZE- myth_handle->buffer_remain, TRUE ); + if ( tmp_buffer != NULL ) { + g_byte_array_free( tmp_buffer, TRUE ); + tmp_buffer = NULL; + } - if (len < 0) - return GNOME_VFS_ERROR_IO; - - myth_handle->buffer_offset = 0; - myth_handle->buffer_remain += len; - - } + } bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read; - g_debug ("Returning %du bytes to gnomevfs", (int) bytes_to_read); - memcpy (buffer, myth_handle->buffer + myth_handle->buffer_offset, bytes_to_read); - + g_debug ("Returning %du bytes to gnomevfs", (gint) bytes_to_read); + /* gets the first buffer_size bytes from the byte array buffer variable */ + + g_memmove( buffer, myth_handle->buffer->data, bytes_to_read ); + myth_handle->bytes_read += bytes_to_read; - myth_handle->buffer_offset += bytes_to_read; - myth_handle->buffer_remain -= bytes_to_read; + myth_handle->buffer_remain -= bytes_to_read; + + /* flushs the newly buffer got from byte array */ + myth_handle->buffer = g_byte_array_remove_range( myth_handle->buffer, 0, bytes_to_read ); *bytes_read = bytes_to_read; return GNOME_VFS_OK;