Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12727


Ignore:
Timestamp:
07/11/15 17:13:22 (9 years ago)
Author:
ascheibe
Message:

#2368 merged r12455, r12456, r12475, r12638 into stable

Location:
stable
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Persistence

  • stable/HeuristicLab.Persistence/3.3/Default/Xml/EasyXmlGenerator.cs

    r12009 r12727  
    167167    /// <param name="obj">The object.</param>
    168168    /// <param name="stream">The stream.</param>
    169     public new static void Serialize(object obj, Stream stream) {
     169    public static void Serialize(object obj, Stream stream) {
    170170      Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()));
    171171    }
     
    178178    /// <param name="stream">The stream.</param>
    179179    /// <param name="config">The configuration.</param>
    180     public new static void Serialize(object obj, Stream stream, Configuration config) {
     180    public static void Serialize(object obj, Stream stream, Configuration config) {
    181181      try {
    182182        using (StreamWriter writer = new StreamWriter(stream)) {
  • stable/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs

    r12009 r12727  
    311311    }
    312312
     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
    313369    /// <summary>
    314370    /// Serializes the specified object into a file.
     
    322378      try {
    323379        string tempfile = Path.GetTempFileName();
    324         DateTime start = DateTime.Now;
     380
    325381        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
    368385        File.Copy(tempfile, filename, true);
    369386        File.Delete(tempfile);
     
    381398    /// <param name="obj">The object.</param>
    382399    /// <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="compressionType">Type of compression, default is GZip.</param>
     401    public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip) {
     402      Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType);
    385403    }
    386404
     
    392410    /// <param name="stream">The stream.</param>
    393411    /// <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="compressionType">Type of compression, default is GZip.</param>
     413    public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip) {
     414      Serialize(obj, stream, config, false, compressionType);
    396415    }
    397416
     
    403422    /// <param name="config">The configuration.</param>
    404423    /// <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="compressionType">Type of compression, default is GZip.</param>
     425    public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies,
     426                                 CompressionType compressionType = CompressionType.GZip) {
    406427      try {
    407428        Serializer serializer = new Serializer(obj, config);
    408         Serialize(stream, serializer);
     429        if (compressionType == CompressionType.Zip) {
     430          Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal);
     431        } else {
     432          Serialize(stream, serializer);
     433        }
    409434      }
    410435      catch (PersistenceException) {
     
    424449    /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param>
    425450    /// <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) {
     451    /// <param name="compressionType">Type of compression, default is GZip.</param>
     452    public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types,
     453                                 CompressionType compressionType = CompressionType.GZip) {
    427454      try {
    428455        Serializer serializer = new Serializer(obj, config);
    429         Serialize(stream, serializer);
     456        if (compressionType == CompressionType.Zip) {
     457          Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer);
     458        } else {
     459          Serialize(stream, serializer);
     460        }
    430461        types = serializer.SerializedTypes;
    431462      }
  • stable/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs

    r12009 r12727  
    3131
    3232namespace HeuristicLab.Persistence.Default.Xml {
     33  /// <summary>
     34  /// Type of compression used for the Xml stream or file.
     35  /// </summary>
     36  public enum CompressionType {
     37    GZip,
     38    Zip
     39  }
    3340
    3441  /// <summary>
     
    216223    }
    217224
    218 
    219     /// <summary>
    220     /// Deserializes an object from the specified stream.
    221     /// </summary>
    222     /// <param name="stream">The stream.</param>
    223     /// <returns>A fresh object instance.</returns>
    224     public static object Deserialize(Stream stream) {
    225       try {
    226         using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) {
    227           XmlParser parser = new XmlParser(reader);
    228           Deserializer deserializer = new Deserializer(new TypeMapping[] { });
    229           return deserializer.Deserialize(parser);
    230         }
    231       }
    232       catch (PersistenceException) {
    233         throw;
    234       }
    235       catch (Exception x) {
    236         throw new PersistenceException("Unexpected exception during deserialization", x);
    237       }
    238     }
    239 
    240225    /// <summary>
    241226    /// Deserializes an object from the specified stream.
     
    243228    /// <typeparam name="T">object type expected from the serialized stream</typeparam>
    244229    /// <param name="stream">The stream.</param>
     230    /// <param name="compressionType">Type of compression, default is GZip.</param>
    245231    /// <returns>A fresh object instance.</returns>
    246     public static T Deserialize<T>(Stream stream) {
    247       return (T)Deserialize(stream);
     232    public static T Deserialize<T>(Stream stream, CompressionType compressionType = CompressionType.GZip) {
     233      return (T)Deserialize(stream, compressionType);
     234    }
     235
     236    /// <summary>
     237    /// Deserializes an object from the specified stream.
     238    /// </summary>
     239    /// <param name="stream">The stream.</param>
     240    /// <param name="compressionType">Type of compression, default is GZip.</param>
     241    /// <returns>A fresh object instance.</returns>
     242    public static object Deserialize(Stream stream, CompressionType compressionType = CompressionType.GZip) {
     243      if (compressionType == CompressionType.Zip) {
     244        ZipArchive zipFile = new ZipArchive(stream);
     245        return Deserialize(zipFile);
     246      } else {
     247        try {
     248          using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) {
     249            XmlParser parser = new XmlParser(reader);
     250            Deserializer deserializer = new Deserializer(new TypeMapping[] { });
     251            return deserializer.Deserialize(parser);
     252          }
     253        }
     254        catch (PersistenceException) {
     255          throw;
     256        }
     257        catch (Exception x) {
     258          throw new PersistenceException("Unexpected exception during deserialization", x);
     259        }
     260      }
    248261    }
    249262
Note: See TracChangeset for help on using the changeset viewer.