[banshee] MetadataService: refactor SaveHttpStream() with 'using' pattern



commit 7dfb0b512f68c1fa19da0cb54561daa0d19bc172
Author: Andrés G. Aragoneses <knocte gmail com>
Date:   Wed Apr 2 14:24:29 2014 +0200

    MetadataService: refactor SaveHttpStream() with 'using' pattern
    
    This method was extremely convoluted because it was checking
    for nulls every time and calling Close() only in the proper
    situations.
    
    By employing the "using" pattern, we can achieve the same with
    less checks and less explicit calls to Close(), as this pattern
    checks for null before disposing, and the Dispose() methods of
    both HttpWebResponse and Stream call Close() underneath.
    
    The code becomes much more readable this way. (And this is also
    safer against resource leaks because the calls to Dispose()
    happen in a finally{} block.)

 .../Banshee.Metadata/MetadataServiceJob.cs         |   23 +++++++++----------
 1 files changed, 11 insertions(+), 12 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Metadata/MetadataServiceJob.cs 
b/src/Core/Banshee.Services/Banshee.Metadata/MetadataServiceJob.cs
index 810cacb..8cd398e 100644
--- a/src/Core/Banshee.Services/Banshee.Metadata/MetadataServiceJob.cs
+++ b/src/Core/Banshee.Services/Banshee.Metadata/MetadataServiceJob.cs
@@ -34,7 +34,6 @@ using System.Collections.Generic;
 using Hyena;
 
 using Banshee.Base;
-using Banshee.Kernel;
 using Banshee.Collection;
 using Banshee.Streaming;
 using Banshee.Networking;
@@ -163,20 +162,20 @@ namespace Banshee.Metadata
 
         protected bool SaveHttpStream(Uri uri, string path, string [] ignoreMimeTypes)
         {
-            HttpWebResponse response = GetHttpStream(uri, ignoreMimeTypes);
-            Stream from_stream = response == null ? null : response.GetResponseStream ();
-            if(from_stream == null) {
-                if (response != null) {
-                    response.Close ();
+            using (HttpWebResponse response = GetHttpStream (uri, ignoreMimeTypes)) {
+                if (response == null) {
+                    return false;
                 }
-                return false;
-            }
-
-            SaveAtomically (path, from_stream);
 
-            from_stream.Close ();
+                using (Stream from_stream = response.GetResponseStream ()) {
+                    if (from_stream == null) {
+                        return false;
+                    }
 
-            return true;
+                    SaveAtomically (path, from_stream);
+                    return true;
+                }
+            }
         }
 
         protected bool SaveHttpStreamCover (Uri uri, string albumArtistId, string [] ignoreMimeTypes)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]