wasSharpNET – Diff between revs 27 and 28

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