Changeset 1734


Ignore:
Timestamp:
05/04/09 16:50:34 (12 years ago)
Author:
epitzer
Message:

Serialization with streams. (#603)

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/PersistenceManager.cs

    r1667 r1734  
    2828using HeuristicLab.PluginInfrastructure;
    2929using HeuristicLab.Persistence.Default.Xml;
     30using HeuristicLab.Persistence.Core;
    3031
    3132namespace HeuristicLab.Core {
     
    5152    /// <param name="stream">The (file) stream where the object should be saved.</param>
    5253    public static void Save(IStorable instance, Stream stream) {
    53       string tempfile = Path.GetTempFileName();
    54       XmlGenerator.Serialize(instance, tempfile);
    55       Stream reader = new FileStream(tempfile, FileMode.Open);
    56       byte[] buffer = new byte[1024];
    57       int bytesRead = 0;
    58       do {
    59         bytesRead = reader.Read(buffer, 0, buffer.Length);
    60         stream.Write(buffer, 0, bytesRead);
    61       } while (bytesRead > 0);
    62       reader.Close();
    63       stream.Close();
    64       File.Delete(tempfile);
     54      XmlGenerator.Serialize(instance, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()));     
    6555    }
    6656    /// <summary>
     
    7262    /// <returns>The loaded object.</returns>
    7363    public static IStorable Load(string filename) {
    74       return (IStorable)XmlParser.DeSerialize(filename);
     64      return (IStorable)XmlParser.Deserialize(filename);
    7565    }
    7666    /// <summary>
     
    8272    /// <returns>The loaded object.</returns>
    8373    public static IStorable Load(Stream stream) {
    84       string tempfile = Path.GetTempFileName();
    85       Stream writer = new FileStream(tempfile, FileMode.CreateNew);
    86       byte[] buffer = new byte[1024];
    87       int bytesRead = 0;
    88       do {
    89         bytesRead = stream.Read(buffer, 0, buffer.Length);
    90         writer.Write(buffer, 0, bytesRead);
    91       } while (bytesRead > 0);
    92       stream.Close();
    93       writer.Close();
    94       object o = XmlParser.DeSerialize(tempfile);
    95       File.Delete(tempfile);
    96       return (IStorable)o;
     74      return (IStorable)XmlParser.Deserialize(stream);
    9775    }
    9876
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs

    r1703 r1734  
    122122    }
    123123
    124     public static object DeSerialize(string filename) {
    125       try {
    126         ZipFile zipFile = new ZipFile(filename);
     124    public static object Deserialize(string filename) {
     125      return Deserialize(new ZipFile(filename));
     126    }
     127
     128    public static object Deserialize(Stream stream) {
     129      return Deserialize(new ZipFile(stream));
     130    }
     131
     132    private static object Deserialize(ZipFile zipFile) {
     133      try {       
    127134        Deserializer deSerializer = new Deserializer(
    128135          ParseTypeCache(
  • trunk/sources/HeuristicLab.Persistence/UnitTests/UseCases.cs

    r1733 r1734  
    219219      r.uninitialized = null;
    220220      XmlGenerator.Serialize(r, tempFile);
    221       Root newR = (Root)XmlParser.DeSerialize(tempFile);
     221      Root newR = (Root)XmlParser.Deserialize(tempFile);
    222222      Assert.AreEqual(
    223223        DebugStringGenerator.Serialize(r),
     
    283283      c.kvpList = new KeyValuePair<List<C>, C>(new List<C> { c }, c);
    284284      XmlGenerator.Serialize(cs, tempFile);
    285       object o = XmlParser.DeSerialize(tempFile);
     285      object o = XmlParser.Deserialize(tempFile);
    286286      Assert.AreEqual(
    287287        DebugStringGenerator.Serialize(cs),
     
    315315      arrayListArray[2].Add(a);
    316316      XmlGenerator.Serialize(arrayListArray, tempFile);
    317       object o = XmlParser.DeSerialize(tempFile);
     317      object o = XmlParser.Deserialize(tempFile);
    318318      Assert.AreEqual(
    319319        DebugStringGenerator.Serialize(arrayListArray),
     
    330330      Manager m = new Manager();
    331331      XmlGenerator.Serialize(m, tempFile);
    332       Manager newM = (Manager)XmlParser.DeSerialize(tempFile);
     332      Manager newM = (Manager)XmlParser.Deserialize(tempFile);
    333333      Assert.AreNotEqual(
    334334        DebugStringGenerator.Serialize(m),
     
    344344      PrimitivesTest sdt = new PrimitivesTest();
    345345      XmlGenerator.Serialize(sdt, tempFile);
    346       object o = XmlParser.DeSerialize(tempFile);
     346      object o = XmlParser.Deserialize(tempFile);
    347347      Assert.AreEqual(
    348348        DebugStringGenerator.Serialize(sdt),
     
    357357      };
    358358      XmlGenerator.Serialize(mDimString, tempFile);
    359       object o = XmlParser.DeSerialize(tempFile);
     359      object o = XmlParser.Deserialize(tempFile);
    360360      Assert.AreEqual(
    361361        DebugStringGenerator.Serialize(mDimString),
     
    372372      NestedType t = new NestedType();
    373373      XmlGenerator.Serialize(t, tempFile);
    374       object o = XmlParser.DeSerialize(tempFile);
     374      object o = XmlParser.Deserialize(tempFile);
    375375      Assert.AreEqual(
    376376        DebugStringGenerator.Serialize(t),
     
    383383      string[] strings = { "ora", "et", "labora" };
    384384      XmlGenerator.Serialize(strings, tempFile);
    385       object o = XmlParser.DeSerialize(tempFile);
     385      object o = XmlParser.Deserialize(tempFile);
    386386      Assert.AreEqual(
    387387        DebugStringGenerator.Serialize(strings),
     
    400400    public void PrimitiveRoot() {
    401401      XmlGenerator.Serialize(12.3f, tempFile);
    402       object o = XmlParser.DeSerialize(tempFile);
     402      object o = XmlParser.Deserialize(tempFile);
    403403      Assert.AreEqual(
    404404        DebugStringGenerator.Serialize(12.3f),
     
    456456            new StorableDecomposer(),
    457457            new Number2StringDecomposer() }));
    458       object o = XmlParser.DeSerialize(tempFile);
     458      object o = XmlParser.Deserialize(tempFile);
    459459      Assert.AreEqual(
    460460        DebugStringGenerator.Serialize(sdt),
     
    467467      et.OnChange += (o) => o;
    468468      XmlGenerator.Serialize(et, tempFile);
    469       EventTest newEt = (EventTest)XmlParser.DeSerialize(tempFile);
     469      EventTest newEt = (EventTest)XmlParser.Deserialize(tempFile);
    470470    }
    471471
     
    477477      et.trickyEnum = TrickyEnum.two | TrickyEnum.one;
    478478      XmlGenerator.Serialize(et, tempFile);
    479       EnumTest newEt = (EnumTest)XmlParser.DeSerialize(tempFile);
     479      EnumTest newEt = (EnumTest)XmlParser.Deserialize(tempFile);
    480480      Assert.AreEqual(et.simpleEnum, SimpleEnum.two);
    481481      Assert.AreEqual(et.complexEnum, ComplexEnum.three);
     
    491491      Assert.AreNotSame(ints[0], ints[1]);
    492492      XmlGenerator.Serialize(ints, tempFile);
    493       List<IntWrapper> newInts = (List<IntWrapper>)XmlParser.DeSerialize(tempFile);
     493      List<IntWrapper> newInts = (List<IntWrapper>)XmlParser.Deserialize(tempFile);
    494494      Assert.AreEqual(newInts[0].Value, 1);
    495495      Assert.AreEqual(newInts[1].Value, 1);
     
    517517        Assert.Fail("Exception expected");
    518518      } catch (PersistenceException) { }
    519       List<int> newList = (List<int>)XmlParser.DeSerialize(tempFile);
     519      List<int> newList = (List<int>)XmlParser.Deserialize(tempFile);
    520520      Assert.AreEqual(list[0], newList[0]);
    521521      Assert.AreEqual(list[1], newList[1]);
Note: See TracChangeset for help on using the changeset viewer.