Changeset 1797 for trunk/sources/HeuristicLab.Persistence
- Timestamp:
- 05/14/09 13:49:36 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r1795 r1797 8 8 using HeuristicLab.Persistence.Default.Decomposers.Storable; 9 9 using System.Text; 10 using System.Reflection; 11 using System.IO; 10 12 11 13 namespace HeuristicLab.Persistence.Core { … … 35 37 public List<TypeMapping> TypeCache { 36 38 get { 37 List<TypeMapping> result = new List<TypeMapping>(); 38 foreach (var pair in typeCache) { 39 string serializer = null; 40 IFormatter f = configuration.GetFormatter(pair.Key); 41 if (f != null) { 42 serializer = f.GetType().AssemblyQualifiedName; 43 } else { 44 IDecomposer d = configuration.GetDecomposer(pair.Key); 45 serializer = d.GetType().AssemblyQualifiedName; 46 } 47 result.Add(new TypeMapping(pair.Value, pair.Key.AssemblyQualifiedName, serializer)); 39 BuildTypeCache(); 40 return externalTypeCache; 41 } 42 } 43 44 public List<string> RequiredFiles { 45 get { 46 BuildTypeCache(); 47 return requiredFiles; 48 } 49 } 50 51 private List<TypeMapping> externalTypeCache; 52 private List<string> requiredFiles; 53 private void BuildTypeCache() { 54 externalTypeCache = new List<TypeMapping>(); 55 Dictionary<Assembly, bool> assemblies = new Dictionary<Assembly, bool>(); 56 foreach (var pair in typeCache) { 57 string serializer = null; 58 IFormatter f = configuration.GetFormatter(pair.Key); 59 if (f != null) { 60 serializer = f.GetType().AssemblyQualifiedName; 61 assemblies[f.GetType().Assembly] = true; 62 } else { 63 IDecomposer d = configuration.GetDecomposer(pair.Key); 64 serializer = d.GetType().AssemblyQualifiedName; 65 assemblies[d.GetType().Assembly] = true; 48 66 } 49 return result; 67 externalTypeCache.Add(new TypeMapping(pair.Value, pair.Key.AssemblyQualifiedName, serializer)); 68 assemblies[pair.Key.Assembly] = true; 50 69 } 70 Dictionary<string, bool> files = new Dictionary<string, bool>(); 71 foreach (Assembly a in assemblies.Keys) { 72 files[a.CodeBase] = true; 73 } 74 requiredFiles = new List<string>(files.Keys); 51 75 } 52 76 -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r1733 r1797 161 161 162 162 public static void Serialize(object obj, string filename, Configuration config) { 163 Serialize(obj, filename, config, false); 164 } 165 166 public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies) { 163 167 string tempfile = Path.GetTempFileName(); 164 168 try { 165 Serialize(obj, File.Create(tempfile), config );169 Serialize(obj, File.Create(tempfile), config, includeAssemblies); 166 170 File.Copy(tempfile, filename, true); 167 171 File.Delete(tempfile); … … 172 176 } 173 177 178 174 179 public static void Serialize(object obj, Stream stream, Configuration config) { 180 Serialize(obj, stream, config, false); 181 } 182 183 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies) { 175 184 try { 176 185 Serializer serializer = new Serializer(obj, config); … … 193 202 } 194 203 writer.Flush(); 204 if (includeAssemblies) { 205 foreach (string name in serializer.RequiredFiles) { 206 Uri uri = new Uri(name); 207 if (!uri.IsFile) { 208 Logger.Warn("cannot read non-local files"); 209 continue; 210 } 211 zipStream.PutNextEntry(new ZipEntry(Path.GetFileName(uri.PathAndQuery))); 212 FileStream reader = File.OpenRead(uri.PathAndQuery); 213 byte[] buffer = new byte[1024 * 1024]; 214 while (true) { 215 int bytesRead = reader.Read(buffer, 0, 1024 * 1024); 216 if (bytesRead == 0) 217 break; 218 zipStream.Write(buffer, 0, bytesRead); 219 } 220 writer.Flush(); 221 } 222 } 195 223 } 196 224 } catch (PersistenceException e) {
Note: See TracChangeset
for help on using the changeset viewer.