libgnomevfs2-mythtv/modules/mythtv-method.c
branchtrunk
changeset 119 96d4296e4492
parent 111 dfa72795bd32
child 127 a7237ce91c3b
     1.1 --- a/libgnomevfs2-mythtv/modules/mythtv-method.c	Fri Nov 24 21:19:49 2006 +0000
     1.2 +++ b/libgnomevfs2-mythtv/modules/mythtv-method.c	Mon Nov 27 22:07:59 2006 +0000
     1.3 @@ -48,8 +48,7 @@
     1.4      guint64 content_size;
     1.5      guint64 bytes_read;
     1.6  
     1.7 -    guint8 *buffer;
     1.8 -    gsize buffer_offset;
     1.9 +    GByteArray *buffer;
    1.10      gsize buffer_remain;
    1.11  } MythtvHandle;
    1.12  
    1.13 @@ -119,8 +118,7 @@
    1.14  
    1.15      myth_handle->content_size = myth_handle->file_transfer->filesize;
    1.16      
    1.17 -    myth_handle->buffer = g_malloc0 (MYTHTV_BUFFER_SIZE);
    1.18 -    myth_handle->buffer_offset = 0;
    1.19 +    myth_handle->buffer = g_byte_array_sized_new (MYTHTV_BUFFER_SIZE);
    1.20      myth_handle->buffer_remain = 0;
    1.21  
    1.22      *method_handle = (GnomeVFSMethodHandle *) myth_handle;
    1.23 @@ -139,9 +137,9 @@
    1.24      MythtvHandle *myth_handle = (MythtvHandle *) method_handle;
    1.25      GnomeVFSFileSize bytes_to_read;
    1.26  
    1.27 -    g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu, buffer_offset: %du, buffer_remain: %du", 
    1.28 +    g_debug ("do_read(): file size: %llu, already read: %llu, requested: %llu, buffer_remain: %du", 
    1.29  		    myth_handle->content_size, myth_handle->bytes_read, num_bytes, 
    1.30 -		    myth_handle->buffer_offset, myth_handle->buffer_remain);
    1.31 +				myth_handle->buffer_remain);
    1.32  
    1.33      *bytes_read = 0;
    1.34  
    1.35 @@ -156,43 +154,42 @@
    1.36  
    1.37      /* Loop sending the Myth File Transfer request:
    1.38      * Retry whilst authentication fails and we supply it. */
    1.39 +  
    1.40 +  if ( bytes_to_read > myth_handle->buffer_remain ) {
    1.41 +  	GByteArray *tmp_buffer = g_byte_array_new();
    1.42  
    1.43 -    if (bytes_to_read > myth_handle->buffer_remain ) {
    1.44 -	guint8 *tmp_buffer = g_malloc0 (MYTHTV_BUFFER_SIZE);
    1.45 -	gint len;
    1.46 +    gint len = gmyth_file_transfer_read( myth_handle->file_transfer,
    1.47 +          tmp_buffer, MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain, TRUE );
    1.48  
    1.49 -	if (myth_handle->buffer_remain > (MYTHTV_BUFFER_SIZE >> 1)) {
    1.50 -	    // Avoid overlap creating another buffer, in the case the remaining data is bigger than half buffer size
    1.51 -	    tmp_buffer = g_malloc0 (MYTHTV_BUFFER_SIZE);
    1.52 -	    memcpy (tmp_buffer, myth_handle->buffer + myth_handle->buffer_offset, myth_handle->buffer_remain);
    1.53 -	    g_free (myth_handle->buffer);
    1.54 -	    myth_handle->buffer = tmp_buffer;
    1.55 -	} else {
    1.56 -	    memcpy (myth_handle->buffer, myth_handle->buffer + myth_handle->buffer_offset, myth_handle->buffer_remain);
    1.57 -	}
    1.58 +		if (len < 0)
    1.59 +		    return GNOME_VFS_ERROR_IO;
    1.60  
    1.61 -	g_debug ("Reading %d data from backend", MYTHTV_BUFFER_SIZE- myth_handle->buffer_remain);
    1.62 +    myth_handle->buffer = g_byte_array_append( myth_handle->buffer, 
    1.63 +    				g_memdup( tmp_buffer->data, len ), len );
    1.64 +
    1.65 +		myth_handle->buffer_remain += len;
    1.66 +
    1.67 +		g_debug ( "Reading %d data from backend", MYTHTV_BUFFER_SIZE - myth_handle->buffer_remain );
    1.68  	
    1.69 -        len = gmyth_file_transfer_read( myth_handle->file_transfer,
    1.70 -	            myth_handle->buffer + myth_handle->buffer_remain, 
    1.71 -		    MYTHTV_BUFFER_SIZE- myth_handle->buffer_remain, TRUE );
    1.72 +    if ( tmp_buffer != NULL ) {
    1.73 +    	g_byte_array_free( tmp_buffer, TRUE );
    1.74 +    	tmp_buffer = NULL;    	
    1.75 +    }
    1.76  
    1.77 -	if (len < 0)
    1.78 -	    return GNOME_VFS_ERROR_IO;
    1.79 -
    1.80 -	myth_handle->buffer_offset = 0;
    1.81 -	myth_handle->buffer_remain += len;
    1.82 -	
    1.83 -    }
    1.84 +  }
    1.85      
    1.86      bytes_to_read = (bytes_to_read > myth_handle->buffer_remain) ? myth_handle->buffer_remain : bytes_to_read;
    1.87  
    1.88 -    g_debug ("Returning %du bytes to gnomevfs", (int) bytes_to_read);
    1.89 -    memcpy (buffer, myth_handle->buffer + myth_handle->buffer_offset, bytes_to_read);
    1.90 -  
    1.91 +    g_debug ("Returning %du bytes to gnomevfs", (gint) bytes_to_read);
    1.92 +	  /* gets the first buffer_size bytes from the byte array buffer variable */ 
    1.93 +
    1.94 +	  g_memmove( buffer, myth_handle->buffer->data, bytes_to_read );
    1.95 +
    1.96      myth_handle->bytes_read += bytes_to_read;
    1.97 -    myth_handle->buffer_offset += bytes_to_read;
    1.98 -    myth_handle->buffer_remain -= bytes_to_read;
    1.99 +    myth_handle->buffer_remain -= bytes_to_read;    
   1.100 +
   1.101 +  	/* flushs the newly buffer got from byte array */
   1.102 +  	myth_handle->buffer = g_byte_array_remove_range( myth_handle->buffer, 0, bytes_to_read );
   1.103      *bytes_read = bytes_to_read;
   1.104    
   1.105      return GNOME_VFS_OK;