wasSharpNET – Diff between revs 16 and 22

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 16 Rev 22
1 /////////////////////////////////////////////////////////////////////////// 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // 2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // 3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. // 4 // rights of fair usage, the disclaimer and warranty conditions. //
5 /////////////////////////////////////////////////////////////////////////// 5 ///////////////////////////////////////////////////////////////////////////
6 // Based on: Danilow @ https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer/ 6 // Based on: Danilow @ https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer/
7   7  
8 using System; 8 using System;
9 using System.Collections.Generic; 9 using System.Collections.Generic;
10 using System.IO; 10 using System.IO;
11 using System.Text; 11 using System.Linq;
12 using System.Threading; 12 using System.Threading;
13 using System.Xml; 13 using System.Xml;
14 using System.Xml.Linq; 14 using System.Xml.Linq;
15 using System.Xml.Serialization; 15 using System.Xml.Serialization;
16 using wasSharp; -  
17   16  
18 namespace wasSharpNET.Serialization 17 namespace wasSharpNET.Serialization
19 { 18 {
20 public static class XmlSerializerCache 19 public static class XmlSerializerCache
21 { 20 {
22 private static ReaderWriterLockSlim SerializerCacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); 21 private static readonly ReaderWriterLockSlim SerializerCacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
23 private static Dictionary<string, XmlSerializer> SerializerCache = new Dictionary<string, XmlSerializer>(); 22 private static readonly Dictionary<string, XmlSerializer> SerializerCache = new Dictionary<string, XmlSerializer>();
24   23  
25 public static XmlSerializer GetSerializer<T>() 24 public static XmlSerializer GetSerializer<T>()
26 { 25 {
27 return GetSerializer<T>(null); 26 return GetSerializer<T>(null);
28 } 27 }
29   28  
30 public static XmlSerializer GetSerializer<T>(Type[] ExtraTypes) 29 public static XmlSerializer GetSerializer<T>(Type[] ExtraTypes)
31 { 30 {
32 return GetSerializer(typeof(T), ExtraTypes); 31 return GetSerializer(typeof(T), ExtraTypes);
33 } 32 }
34   33  
35 public static XmlSerializer GetSerializer(Type MainTypeForSerialization) 34 public static XmlSerializer GetSerializer(Type MainTypeForSerialization)
36 { 35 {
37 return GetSerializer(MainTypeForSerialization, null); 36 return GetSerializer(MainTypeForSerialization, null);
38 } 37 }
39   38  
40 public static XmlSerializer GetSerializer(Type MainTypeForSerialization, Type[] ExtraTypes) 39 public static XmlSerializer GetSerializer(Type MainTypeForSerialization, Type[] ExtraTypes)
41 { 40 {
42 string Signature = MainTypeForSerialization.FullName; 41 var Signature = MainTypeForSerialization.FullName;
43 if (ExtraTypes != null) 42 if (ExtraTypes != null)
44 { 43 {
45 foreach (Type Tp in ExtraTypes) -  
46 Signature += "-" + Tp.FullName; 44 Signature = ExtraTypes.Aggregate(Signature, (current, tp) => current + ("-" + tp.FullName));
47 } 45 }
48   46  
49 SerializerCacheLock.EnterReadLock(); 47 SerializerCacheLock.EnterReadLock();
50 XmlSerializer XmlEventSerializer = null; 48 XmlSerializer XmlEventSerializer;
51 if (SerializerCache.TryGetValue(Signature, out XmlEventSerializer)) 49 if (SerializerCache.TryGetValue(Signature, out XmlEventSerializer))
52 { 50 {
53 SerializerCacheLock.ExitReadLock(); 51 SerializerCacheLock.ExitReadLock();
54 return XmlEventSerializer; 52 return XmlEventSerializer;
55 } 53 }
56 SerializerCacheLock.ExitReadLock(); 54 SerializerCacheLock.ExitReadLock();
57   55  
58 if (ExtraTypes == null) 56 if (ExtraTypes == null)
59 return new XmlSerializer(MainTypeForSerialization); 57 return new XmlSerializer(MainTypeForSerialization);
60   58  
61 XmlEventSerializer = new XmlSerializer(MainTypeForSerialization, ExtraTypes); 59 XmlEventSerializer = new XmlSerializer(MainTypeForSerialization, ExtraTypes);
62 SerializerCacheLock.EnterWriteLock(); 60 SerializerCacheLock.EnterWriteLock();
63 SerializerCache.Add(Signature, XmlEventSerializer); 61 SerializerCache.Add(Signature, XmlEventSerializer);
64 SerializerCacheLock.ExitWriteLock(); 62 SerializerCacheLock.ExitWriteLock();
65   63  
66 return XmlEventSerializer; 64 return XmlEventSerializer;
67 } 65 }
68   66  
69 public static T Deserialize<T>(XDocument XmlData) 67 public static T Deserialize<T>(XDocument XmlData)
70 { 68 {
71 return Deserialize<T>(XmlData, null); 69 return Deserialize<T>(XmlData, null);
72 } 70 }
73   71  
74 public static T Deserialize<T>(XDocument XmlData, Type[] ExtraTypes) 72 public static T Deserialize<T>(XDocument XmlData, Type[] ExtraTypes)
75 { 73 {
76 try 74 try
77 { 75 {
78 using (var XmlReader = XmlData.Root.CreateReader()) 76 using (var XmlReader = XmlData.Root.CreateReader())
79 { 77 {
80 return (T)GetSerializer<T>(ExtraTypes).Deserialize(XmlReader); 78 return (T) GetSerializer<T>(ExtraTypes).Deserialize(XmlReader);
81 } 79 }
82 } 80 }
83 catch (Exception ex) 81 catch (Exception ex)
84 { 82 {
85 throw new Exception("Could not deserialize to " + typeof(T).Name, ex); 83 throw new Exception("Could not deserialize to " + typeof(T).Name, ex);
86 } 84 }
87 } 85 }
88   86  
89 public static T Deserialize<T>(string XmlData) 87 public static T Deserialize<T>(string XmlData)
90 { 88 {
91 return Deserialize<T>(XmlData, null); 89 return Deserialize<T>(XmlData, null);
92 } 90 }
93   91  
94 public static T Deserialize<T>(string XmlData, Type[] ExtraTypes) 92 public static T Deserialize<T>(string XmlData, Type[] ExtraTypes)
95 { 93 {
96 try 94 try
97 { 95 {
98 using (MemoryStream memoryStream = new MemoryStream()) 96 using (MemoryStream memoryStream = new MemoryStream())
99 { 97 {
100 using (StreamWriter streamWriter = new StreamWriter(memoryStream)) 98 using (StreamWriter streamWriter = new StreamWriter(memoryStream))
101 { 99 {
102 streamWriter.Write(XmlData); 100 streamWriter.Write(XmlData);
103 streamWriter.Flush(); 101 streamWriter.Flush();
104 memoryStream.Position = 0; 102 memoryStream.Position = 0;
105 return (T)GetSerializer<T>(ExtraTypes).Deserialize(memoryStream); 103 return (T)GetSerializer<T>(ExtraTypes).Deserialize(memoryStream);
106 } 104 }
107 } 105 }
108 } 106 }
109 catch (Exception ex) 107 catch (Exception ex)
110 { 108 {
111 throw new Exception("Could not deserialize to " + typeof(T).Name, ex); 109 throw new Exception("Could not deserialize to " + typeof(T).Name, ex);
112 } 110 }
113 } 111 }
114   112  
115 public static XDocument Serialize<T>(T Object, Type[] ExtraTypes) 113 public static XDocument Serialize<T>(T Object, Type[] ExtraTypes)
116 { 114 {
117 try 115 try
118 { 116 {
119 using (MemoryStream memoryStream = new MemoryStream()) 117 using (MemoryStream memoryStream = new MemoryStream())
120 { 118 {
121 using (StreamReader streamReader = new StreamReader(memoryStream)) 119 using (StreamReader streamReader = new StreamReader(memoryStream))
122 { 120 {
123 using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings { Indent = true })) 121 using (var xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings { Indent = true }))
124 { 122 {
125 XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 123 var ns = new XmlSerializerNamespaces();
126 ns.Add(string.Empty, string.Empty); 124 ns.Add(string.Empty, string.Empty);
127 GetSerializer<T>(ExtraTypes).Serialize(xmlWriter, Object, ns); 125 GetSerializer<T>(ExtraTypes).Serialize(xmlWriter, Object, ns);
128 xmlWriter.Flush(); 126 xmlWriter.Flush();
129 memoryStream.Position = 0L; 127 memoryStream.Position = 0L;
130 return XDocument.Load(streamReader, LoadOptions.None); 128 return XDocument.Load(streamReader, LoadOptions.None);
131 } 129 }
132 } 130 }
133 } 131 }
134 } 132 }
135 catch (Exception ex) 133 catch (Exception ex)
136 { 134 {
137 throw new Exception("Could not serialize from " + typeof(T).Name + " to xml string", ex); 135 throw new Exception("Could not serialize from " + typeof(T).Name + " to xml string", ex);
138 } 136 }
139 } 137 }
140   138  
141 public static XDocument Serialize<T>(T Object) 139 public static XDocument Serialize<T>(T Object)
142 { 140 {
143 return Serialize(Object, null); 141 return Serialize(Object, null);
144 } 142 }
145   143  
146 public static T Deserialize<T>(StreamReader streamReader) 144 public static T Deserialize<T>(StreamReader streamReader)
147 { 145 {
148 return Deserialize<T>(streamReader.ReadToEnd(), null); 146 return Deserialize<T>(streamReader.ReadToEnd(), null);
149 } 147 }
150   148  
151 public static T Deserialize<T>(Stream stream) 149 public static T Deserialize<T>(Stream stream)
152 { 150 {
153 using (MemoryStream memoryStream = new MemoryStream()) 151 using (var memoryStream = new MemoryStream())
154 { 152 {
155 stream.CopyTo(memoryStream); 153 stream.CopyTo(memoryStream);
156 memoryStream.Position = 0L; 154 memoryStream.Position = 0L;
157 using (var streamReader = new StreamReader(memoryStream)) 155 using (var streamReader = new StreamReader(memoryStream))
158 { 156 {
159 return Deserialize<T>(streamReader.ReadToEnd(), null); 157 return Deserialize<T>(streamReader.ReadToEnd(), null);
160 } 158 }
161 } 159 }
162 } 160 }
163   161  
164 public static void Serialize<T>(Stream stream, T value) 162 public static void Serialize<T>(Stream stream, T value)
165 { 163 {
166 Serialize(value, null).Save(stream); 164 Serialize(value, null).Save(stream);
167 } 165 }
168   166  
169 public static void Serialize<T>(StreamWriter streamWriter, T value) 167 public static void Serialize<T>(StreamWriter streamWriter, T value)
170 { 168 {
171 Serialize(value, null).Save(streamWriter); 169 Serialize(value, null).Save(streamWriter);
172 } 170 }
173   171  
174 public static void Serialize<T>(StringWriter stringWriter, T value) 172 public static void Serialize<T>(StringWriter stringWriter, T value)
175 { 173 {
176 Serialize(value, null).Save(stringWriter); 174 Serialize(value, null).Save(stringWriter);
177 } 175 }
178   176  
179 public static T Deserialize<T>(TextReader reader) 177 public static T Deserialize<T>(TextReader reader)
180 { 178 {
181 return Deserialize<T>(reader.ReadToEnd(), null); 179 return Deserialize<T>(reader.ReadToEnd(), null);
182 } 180 }
183 } 181 }
184 } 182 }
185   183