[longomatch/redesign2: 108/140] Allow serialization to Stream instead of only file paths



commit 43de32dc7e6ccc1b651394c2ca3782323cf0577a
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Sat Apr 9 16:41:25 2011 +0200

    Allow serialization to Stream instead of only file paths

 LongoMatch/Common/SerializableObject.cs |   23 +++++++++++++++++------
 1 files changed, 17 insertions(+), 6 deletions(-)
---
diff --git a/LongoMatch/Common/SerializableObject.cs b/LongoMatch/Common/SerializableObject.cs
index b4f3068..92634b2 100644
--- a/LongoMatch/Common/SerializableObject.cs
+++ b/LongoMatch/Common/SerializableObject.cs
@@ -23,20 +23,31 @@ namespace LongoMatch.Common
 {
 	public class SerializableObject
 	{
-		public static void Save<T>(T obj, string filepath) {
+		public static void Save<T>(T obj, Stream stream) {
 			BinaryFormatter formatter = new  BinaryFormatter();
-			Stream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None);
 			formatter.Serialize(stream, obj);
-			stream.Close();
+		}
+		
+		public static void Save<T>(T obj, string filepath) {
+			Stream stream = new FileStream(filepath, FileMode.Create, FileAccess.Write, FileShare.None);
+			using (stream) {
+				Save<T> (obj, stream);
+				stream.Close();
+			}
 		}
 
-		public static T Load<T>(string filepath) {
+		public static T Load<T>(Stream stream) {
 			BinaryFormatter formatter = new BinaryFormatter();
-			Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
 			var obj = formatter.Deserialize(stream);
-			stream.Close();
 			return (T)obj;
 		}
+		
+		public static T Load<T>(string filepath) {
+			Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
+			using (stream) {
+				return Load<T> (stream);
+			}
+		}
 	}
 }
 



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