Changeset 12495 for branches/GBT/HeuristicLab.Persistence
- Timestamp:
- 06/23/15 12:50:05 (9 years ago)
- Location:
- branches/GBT
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GBT
- Property svn:mergeinfo changed
/trunk/sources merged: 12392-12393,12397-12401,12422,12424,12428-12435,12442-12443,12445,12455-12458,12461,12463-12465,12470-12476,12478-12482,12485,12488,12490-12494 -
Property
svn:global-ignores
set to
*.nuget
packages
- Property svn:mergeinfo changed
-
branches/GBT/HeuristicLab.Persistence
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Persistence (added) merged: 12455-12456,12475
- Property svn:mergeinfo changed
-
branches/GBT/HeuristicLab.Persistence/3.3/Default/Xml/EasyXmlGenerator.cs
r12012 r12495 167 167 /// <param name="obj">The object.</param> 168 168 /// <param name="stream">The stream.</param> 169 public newstatic void Serialize(object obj, Stream stream) {169 public static void Serialize(object obj, Stream stream) { 170 170 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat())); 171 171 } … … 178 178 /// <param name="stream">The stream.</param> 179 179 /// <param name="config">The configuration.</param> 180 public newstatic void Serialize(object obj, Stream stream, Configuration config) {180 public static void Serialize(object obj, Stream stream, Configuration config) { 181 181 try { 182 182 using (StreamWriter writer = new StreamWriter(stream)) { -
branches/GBT/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r12012 r12495 311 311 } 312 312 313 private static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies 314 , CompressionLevel compression) { 315 Serializer serializer = new Serializer(obj, config); 316 Serialize(stream, includeAssemblies, compression, serializer); 317 } 318 319 private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Serializer serializer) { 320 try { 321 DateTime start = DateTime.Now; 322 serializer.InterleaveTypeInformation = false; 323 XmlGenerator generator = new XmlGenerator(); 324 using (ZipArchive zipArchive = new ZipArchive(stream, ZipArchiveMode.Create)) { 325 ZipArchiveEntry entry = zipArchive.CreateEntry("data.xml", compression); 326 using (StreamWriter writer = new StreamWriter(entry.Open())) { 327 foreach (ISerializationToken token in serializer) { 328 string line = generator.Format(token); 329 writer.Write(line); 330 } 331 } 332 entry = zipArchive.CreateEntry("typecache.xml", compression); 333 using (StreamWriter writer = new StreamWriter(entry.Open())) { 334 foreach (string line in generator.Format(serializer.TypeCache)) { 335 writer.Write(line); 336 } 337 } 338 if (includeAssemblies) { 339 foreach (string name in serializer.RequiredFiles) { 340 Uri uri = new Uri(name); 341 if (!uri.IsFile) { 342 Logger.Warn("cannot read non-local files"); 343 continue; 344 } 345 entry = zipArchive.CreateEntry(Path.GetFileName(uri.PathAndQuery), compression); 346 using (BinaryWriter bw = new BinaryWriter(entry.Open())) { 347 using (FileStream reader = File.OpenRead(uri.PathAndQuery)) { 348 byte[] buffer = new byte[1024 * 1024]; 349 while (true) { 350 int bytesRead = reader.Read(buffer, 0, 1024 * 1024); 351 if (bytesRead == 0) 352 break; 353 bw.Write(buffer, 0, bytesRead); 354 } 355 } 356 } 357 } 358 } 359 } 360 Logger.Info(String.Format("serialization took {0} seconds with compression level {1}", 361 (DateTime.Now - start).TotalSeconds, compression)); 362 } 363 catch (Exception) { 364 Logger.Warn("Exception caught, no data has been serialized."); 365 throw; 366 } 367 } 368 313 369 /// <summary> 314 370 /// Serializes the specified object into a file. … … 322 378 try { 323 379 string tempfile = Path.GetTempFileName(); 324 DateTime start = DateTime.Now; 380 325 381 using (FileStream stream = File.Create(tempfile)) { 326 Serializer serializer = new Serializer(obj, config); 327 serializer.InterleaveTypeInformation = false; 328 XmlGenerator generator = new XmlGenerator(); 329 using (ZipArchive zipArchive = new ZipArchive(stream, ZipArchiveMode.Create)) { 330 ZipArchiveEntry entry = zipArchive.CreateEntry("data.xml", compression); 331 using (StreamWriter writer = new StreamWriter(entry.Open())) { 332 foreach (ISerializationToken token in serializer) { 333 string line = generator.Format(token); 334 writer.Write(line); 335 } 336 } 337 entry = zipArchive.CreateEntry("typecache.xml", compression); 338 using (StreamWriter writer = new StreamWriter(entry.Open())) { 339 foreach (string line in generator.Format(serializer.TypeCache)) { 340 writer.Write(line); 341 } 342 } 343 if (includeAssemblies) { 344 foreach (string name in serializer.RequiredFiles) { 345 Uri uri = new Uri(name); 346 if (!uri.IsFile) { 347 Logger.Warn("cannot read non-local files"); 348 continue; 349 } 350 entry = zipArchive.CreateEntry(Path.GetFileName(uri.PathAndQuery), compression); 351 using (BinaryWriter bw = new BinaryWriter(entry.Open())) { 352 using (FileStream reader = File.OpenRead(uri.PathAndQuery)) { 353 byte[] buffer = new byte[1024 * 1024]; 354 while (true) { 355 int bytesRead = reader.Read(buffer, 0, 1024 * 1024); 356 if (bytesRead == 0) 357 break; 358 bw.Write(buffer, 0, bytesRead); 359 } 360 } 361 } 362 } 363 } 364 } 365 } 366 Logger.Info(String.Format("serialization took {0} seconds with compression level {1}", 367 (DateTime.Now - start).TotalSeconds, compression)); 382 Serialize(obj, stream, config, includeAssemblies, compression); 383 } 384 368 385 File.Copy(tempfile, filename, true); 369 386 File.Delete(tempfile); … … 381 398 /// <param name="obj">The object.</param> 382 399 /// <param name="stream">The stream.</param> 383 public static void Serialize(object obj, Stream stream) { 384 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat())); 400 /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param> 401 public static void Serialize(object obj, Stream stream, bool useZip = false) { 402 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), useZip); 385 403 } 386 404 … … 392 410 /// <param name="stream">The stream.</param> 393 411 /// <param name="config">The configuration.</param> 394 public static void Serialize(object obj, Stream stream, Configuration config) { 395 Serialize(obj, stream, config, false); 412 /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param> 413 public static void Serialize(object obj, Stream stream, Configuration config, bool useZip = false) { 414 Serialize(obj, stream, config, false, useZip); 396 415 } 397 416 … … 403 422 /// <param name="config">The configuration.</param> 404 423 /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param> 405 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies) { 424 /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param> 425 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, bool useZip = false) { 406 426 try { 407 427 Serializer serializer = new Serializer(obj, config); 408 Serialize(stream, serializer); 428 if (useZip) { 429 Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal); 430 } else { 431 Serialize(stream, serializer); 432 } 409 433 } 410 434 catch (PersistenceException) { … … 424 448 /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param> 425 449 /// <param name="types">The list of all serialized types.</param> 426 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types) { 450 /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param> 451 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types, bool useZip = false) { 427 452 try { 428 453 Serializer serializer = new Serializer(obj, config); 429 Serialize(stream, serializer); 454 if (useZip) { 455 Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer); 456 } else { 457 Serialize(stream, serializer); 458 } 430 459 types = serializer.SerializedTypes; 431 460 } -
branches/GBT/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs
r12012 r12495 218 218 219 219 /// <summary> 220 /// Deserializes an object from the specified stream .220 /// Deserializes an object from the specified stream using GZip compression. 221 221 /// </summary> 222 222 /// <param name="stream">The stream.</param> 223 223 /// <returns>A fresh object instance.</returns> 224 p ublic static object Deserialize(Stream stream) {224 private static object DeserializeWithGZip(Stream stream) { 225 225 try { 226 226 using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) { … … 239 239 240 240 /// <summary> 241 /// Deserializes an object from the specified stream using Zip compression. 242 /// </summary> 243 /// <param name="stream">The stream.</param> 244 /// <returns>A fresh object instance.</returns> 245 private static object DeserializeWithZip(Stream stream) { 246 ZipArchive zipFile = new ZipArchive(stream); 247 return Deserialize(zipFile); 248 } 249 250 /// <summary> 241 251 /// Deserializes an object from the specified stream. 242 252 /// </summary> 243 253 /// <typeparam name="T">object type expected from the serialized stream</typeparam> 244 254 /// <param name="stream">The stream.</param> 255 /// <param name="useZip">If true, uses zip for decompression, otherwise gzip.</param> 245 256 /// <returns>A fresh object instance.</returns> 246 public static T Deserialize<T>(Stream stream) { 247 return (T)Deserialize(stream); 257 public static T Deserialize<T>(Stream stream, bool useZip = false) { 258 return (T)Deserialize(stream, useZip); 259 } 260 261 /// <summary> 262 /// Deserializes an object from the specified stream. 263 /// </summary> 264 /// <param name="stream">The stream.</param> 265 /// <param name="useZip">If true, uses zip for decompression, otherwise gzip.</param> 266 /// <returns>A fresh object instance.</returns> 267 public static object Deserialize(Stream stream, bool useZip = false) { 268 if (useZip) { 269 return DeserializeWithZip(stream); 270 } else { 271 return DeserializeWithGZip(stream); 272 } 248 273 } 249 274
Note: See TracChangeset
for help on using the changeset viewer.