Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1704 for trunk


Ignore:
Timestamp:
04/29/09 15:31:06 (16 years ago)
Author:
epitzer
Message:

Properly close file during serialization even if an exception occurs. (#606)

File:
1 edited

Legend:

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

    r1625 r1704  
    164164        Serializer serializer = new Serializer(obj, config);
    165165        XmlGenerator generator = new XmlGenerator();
    166         ZipOutputStream zipStream = new ZipOutputStream(File.Create(filename));
    167         zipStream.SetLevel(9);
    168         zipStream.PutNextEntry(new ZipEntry("data.xml"));
    169         StreamWriter writer = new StreamWriter(zipStream);
    170166        ILog logger = Logger.GetDefaultLogger();
    171         foreach (ISerializationToken token in serializer) {
    172           string line = generator.Format(token);
    173           writer.Write(line);
    174           logger.Debug(line.TrimEnd());
     167        using (ZipOutputStream zipStream = new ZipOutputStream(File.Create(filename))) {
     168          zipStream.SetLevel(9);
     169          zipStream.PutNextEntry(new ZipEntry("data.xml"));
     170          StreamWriter writer = new StreamWriter(zipStream);
     171          foreach (ISerializationToken token in serializer) {
     172            string line = generator.Format(token);
     173            writer.Write(line);
     174            logger.Debug(line.TrimEnd());
     175          }
     176          writer.Flush();
     177          zipStream.PutNextEntry(new ZipEntry("typecache.xml"));
     178          foreach (string line in generator.Format(serializer.TypeCache)) {
     179            writer.Write(line);
     180            logger.Debug(line.TrimEnd());
     181          }
     182          writer.Flush();
    175183        }
    176         writer.Flush();
    177         zipStream.PutNextEntry(new ZipEntry("typecache.xml"));
    178         writer = new StreamWriter(zipStream);
    179         foreach (string line in generator.Format(serializer.TypeCache)) {
    180           writer.Write(line);
    181           logger.Debug(line.TrimEnd());
    182         }
    183         writer.Flush();
    184         zipStream.Close();
    185184      } catch (PersistenceException e) {
    186185        throw;
Note: See TracChangeset for help on using the changeset viewer.