/[cits3200i]/branches/metadata-branch-rvvs89/UWAPlugins/MetadataPlugin/Source/MetadataListSerializer.cs


UCC Code Repository

Contents of /branches/metadata-branch-rvvs89/UWAPlugins/MetadataPlugin/Source/MetadataListSerializer.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 98 - (show annotations) (download)
Wed Oct 13 01:09:10 2010 UTC (11 years, 1 month ago) by rvvs89
File size: 2435 byte(s)
Replace Crystal icons with ones made by Joel for licensing reasons.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Runtime.Serialization;
5 using System.Runtime.Serialization.Formatters.Binary;
6 using System.Text;
7 using System.Reflection;
8
9 namespace MetadataPlugin.Source
10 {
11 class MetadataListSerializer
12 {
13 private MetadataListSerializer() {}
14
15 // NOTE: Increment this if the data storage mechanism changes
16 private static int SERIAL_VERSION = 1;
17
18 static MetadataListSerializer()
19 {
20 AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
21 }
22
23 static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
24 {
25 string shortName = args.Name.Split(',')[0];
26 Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
27 foreach (Assembly assembly in assemblies)
28 {
29 if (shortName == assembly.FullName.Split(',')[0])
30 {
31 return assembly;
32 }
33 }
34 return null;
35 }
36
37 public static MetadataList Deserialize(byte[] input)
38 {
39 if (input == null || input.Length <= 0)
40 {
41 return null;
42 }
43 MemoryStream stream = new MemoryStream(input);
44 BinaryFormatter formatter = new BinaryFormatter();
45 formatter.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full;
46 int version = (int)formatter.Deserialize(stream);
47 if (version != SERIAL_VERSION)
48 {
49 // FIXME: warn when finding incompatible data
50 return null;
51 }
52 MetadataList ret = (MetadataList)formatter.Deserialize(stream);
53 stream.Close();
54 return ret;
55 }
56
57 public static byte[] Serialize(MetadataList input)
58 {
59 if (input == null)
60 {
61 return null;
62 }
63 MemoryStream stream = new MemoryStream();
64 IFormatter formatter = new BinaryFormatter();
65 formatter.Serialize(stream, SERIAL_VERSION);
66 formatter.Serialize(stream, input);
67 byte[] ret = stream.ToArray();
68 stream.Close();
69 return ret;
70 }
71 }
72 }

Managed by UCC Webmasters ViewVC Help
Powered by ViewVC 1.1.26