Index: totem-pl-parser-private.h
===================================================================
--- totem-pl-parser-private.h	(revision 165)
+++ totem-pl-parser-private.h	(working copy)
@@ -85,6 +85,12 @@
 	guint disable_unsafe : 1;
 };
 
+typedef struct
+{
+	GByteArray *buffer;
+	GInputStream *stream;
+} TotemPlParserData;
+
 #ifndef TOTEM_PL_PARSER_MINI
 char *totem_pl_parser_read_ini_line_string	(char **lines, const char *key,
 						 gboolean dos_mode);
Index: totem-pl-parser.c
===================================================================
--- totem-pl-parser.c	(revision 166)
+++ totem-pl-parser.c	(working copy)
@@ -132,7 +132,7 @@
 typedef const char * (*PlaylistIdenCallback) (const char *data, gsize len);
 
 #ifndef TOTEM_PL_PARSER_MINI
-typedef TotemPlParserResult (*PlaylistCallback) (TotemPlParser *parser, GFile *url, GFile *base_file, gpointer data);
+typedef TotemPlParserResult (*PlaylistCallback) (TotemPlParser *parser, GFile *url, GFile *base_file, TotemPlParserData *data);
 #endif
 
 typedef struct {
@@ -633,9 +633,9 @@
 }
 
 static char *
-my_g_file_info_get_mime_type_with_data (GFile *file, gpointer *data, TotemPlParser *parser)
+my_g_file_info_get_mime_type_with_data (GFile *file, TotemPlParserData **data, TotemPlParser *parser)
 {
-	char *buffer;
+	guchar *buffer;
 	gssize bytes_read;
 	GFileInputStream *stream;
 	GError *error = NULL;
@@ -672,25 +672,38 @@
 	/* Read the whole thing, up to MIME_READ_CHUNK_SIZE */
 	buffer = g_malloc (MIME_READ_CHUNK_SIZE);
 	bytes_read = g_input_stream_read (G_INPUT_STREAM (stream), buffer, MIME_READ_CHUNK_SIZE, NULL, &error);
-	g_input_stream_close (G_INPUT_STREAM (stream), NULL, NULL);
-	if (bytes_read == -1) {
+
+	/* If we get an error, or an empty file, exit */
+	if (bytes_read <= 0) {
+		g_input_stream_close (G_INPUT_STREAM (stream), NULL, NULL);
 		g_free (buffer);
+
+		/* Empty file */
+		if (bytes_read == 0) {
+			DEBUG(file, g_print ("URL '%s' is empty in _get_mime_type_with_data\n", uri));
+			return g_strdup (EMPTY_FILE_TYPE);
+		}
 		return NULL;
 	}
 
-	/* Empty file */
-	if (bytes_read == 0) {
-		g_free (buffer);
-		DEBUG(file, g_print ("URL '%s' is empty in _get_mime_type_with_data\n", uri));
-		return g_strdup (EMPTY_FILE_TYPE);
-	}
+	*data = g_new0 (TotemPlParserData, 1);
+	(*data)->buffer = g_byte_array_sized_new (bytes_read);
+	g_byte_array_append ((*data)->buffer, buffer, bytes_read);
+	(*data)->stream = G_INPUT_STREAM (stream);
 
-	/* Return the file null-terminated. */
-	buffer = g_realloc (buffer, bytes_read + 1);
-	buffer[bytes_read] = '\0';
-	*data = buffer;
+	return totem_pl_parser_mime_type_from_data ((*data)->buffer->data, bytes_read);
+}
 
-	return totem_pl_parser_mime_type_from_data (*data, bytes_read);
+static void
+totem_pl_parser_data_free (TotemPlParserData *data)
+{
+	if (data == NULL)
+		return;
+	if (data->stream != NULL)
+		g_object_unref (data->stream);
+	if (data->buffer != NULL)
+		g_byte_array_free (data->buffer, TRUE);
+	g_free (data);
 }
 
 /**
@@ -1405,7 +1418,7 @@
 {
 	char *mimetype;
 	guint i;
-	gpointer data = NULL;
+	TotemPlParserData *data;
 	TotemPlParserResult ret = TOTEM_PL_PARSER_RESULT_UNHANDLED;
 	gboolean found = FALSE;
 
@@ -1465,12 +1478,12 @@
 	}
 
 	if (mimetype == NULL) {
-		g_free (data);
+		totem_pl_parser_data_free (data);
 		return TOTEM_PL_PARSER_RESULT_UNHANDLED;
 	}
 
 	if (strcmp (mimetype, EMPTY_FILE_TYPE) == 0) {
-		g_free (data);
+		totem_pl_parser_data_free (data);
 		g_free (mimetype);
 		return TOTEM_PL_PARSER_RESULT_SUCCESS;
 	}
@@ -1489,7 +1502,7 @@
 
 	if (totem_pl_parser_mimetype_is_ignored (parser, mimetype) != FALSE) {
 		g_free (mimetype);
-		g_free (data);
+		totem_pl_parser_data_free (data);
 		return TOTEM_PL_PARSER_RESULT_IGNORED;
 	}
 
@@ -1502,7 +1515,7 @@
 				if (parser->priv->disable_unsafe != FALSE && special_types[i].unsafe != FALSE) {
 					DEBUG(file, g_print ("URL '%s' is unsafe so was ignored\n", uri));
 					g_free (mimetype);
-					g_free (data);
+					totem_pl_parser_data_free (data);
 					return TOTEM_PL_PARSER_RESULT_IGNORED;
 				}
 				if (base_file == NULL)
@@ -1547,7 +1560,7 @@
 			}
 		}
 
-		g_free (data);
+		totem_pl_parser_data_free (data);
 
 		parser->priv->recurse_level--;
 	}
