Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/07/15 13:02:06 (9 years ago)
Author:
ascheibe
Message:

#2368

  • created enum for distinguishing between zip and gzip
  • shrank interface of XmlParser by merging methods
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs

    r12456 r12638  
    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 using GZip compression.
    221     /// </summary>
    222     /// <param name="stream">The stream.</param>
    223     /// <returns>A fresh object instance.</returns>
    224     private static object DeserializeWithGZip(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 
    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 
    250225    /// <summary>
    251226    /// Deserializes an object from the specified stream.
     
    253228    /// <typeparam name="T">object type expected from the serialized stream</typeparam>
    254229    /// <param name="stream">The stream.</param>
    255     /// <param name="useZip">If true, uses zip for decompression, otherwise gzip.</param>
     230    /// <param name="compressionType">Type of compression, default is GZip.</param>
    256231    /// <returns>A fresh object instance.</returns>
    257     public static T Deserialize<T>(Stream stream, bool useZip = false) {
    258       return (T)Deserialize(stream, useZip);
     232    public static T Deserialize<T>(Stream stream, CompressionType compressionType = CompressionType.GZip) {
     233      return (T)Deserialize(stream, compressionType);
    259234    }
    260235
     
    263238    /// </summary>
    264239    /// <param name="stream">The stream.</param>
    265     /// <param name="useZip">If true, uses zip for decompression, otherwise gzip.</param>
     240    /// <param name="compressionType">Type of compression, default is GZip.</param>
    266241    /// <returns>A fresh object instance.</returns>
    267     public static object Deserialize(Stream stream, bool useZip = false) {
    268       if (useZip) {
    269         return DeserializeWithZip(stream);
     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);
    270246      } else {
    271         return DeserializeWithGZip(stream);
     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        }
    272260      }
    273261    }
Note: See TracChangeset for help on using the changeset viewer.