/[RomCheater]/trunk/RomCheater/Serialization/SerializationReader.cs
ViewVC logotype

Contents of /trunk/RomCheater/Serialization/SerializationReader.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 451 - (show annotations) (download)
Sun Jun 2 21:37:09 2013 UTC (8 years, 1 month ago) by william
File size: 5070 byte(s)
attempt to reduce memory usage by:
1) opening the serialized results
2) seeking to the current result entry
3) read the data
4) close the data stream
5) return the result

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6
7 namespace RomCheater.Serialization
8 {
9 public abstract class SerializationReader : ISerializationResultCount, IDisposable
10 {
11 private void CreateFilename(Guid guid)
12 {
13 if (guid == Guid.Empty)
14 {
15 if (this.TemporaryFolder != string.Empty)
16 {
17 FileName = string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, DEFAULT_FILENAME);
18 }
19 else
20 {
21 FileName = string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME);
22 }
23
24 }
25 else
26 {
27 if (this.TemporaryFolder != string.Empty)
28 {
29 FileName = string.Format(@"{0}\{1}\{2}", DEFAULT_FOLDER, TemporaryFolder, string.Format("{0}.tmp", guid.ToString()));
30 }
31 else
32 {
33 FileName = string.Format(@"{0}\{1}", DEFAULT_FOLDER, string.Format("{0}.tmp", guid.ToString()));
34 }
35 }
36 }
37
38 public const string DEFAULT_FOLDER = "tmp";
39 protected const string DEFAULT_FILENAME = "SerializedMemoryResults.tmp";
40 //private static string DEFAULT_PATH = string.Format(@"{0}\{1}", DEFAULT_FOLDER, DEFAULT_FILENAME);
41 //public SerializationReader() : this(CreateFilename(Guid.Empty)) { CreateReader(Guid.Empty); }
42 public SerializationReader(Guid guid) { CreateFilename(guid); }
43 //protected SerializationReader(string filename) { FileName = filename; }
44
45 protected virtual string TemporaryFolder { get { return string.Empty; } }
46
47 protected bool Cancelled { get; set; }
48
49 public void CancelRequest() { Cancelled = true; }
50
51 #region ISerializationResultCount members
52 public int ResultCount { get; protected set; }
53 #endregion
54 //private FileStream fileStream;
55 //protected BinaryReader binReader;
56 protected string FileName { get; private set; }
57 protected FileStream CreateReader(Guid guid)
58 {
59 return new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
60 }
61 #region IDisposable Support
62 // Track whether Dispose has been called.
63 private bool disposed = false;
64 // Implement IDisposable.
65 // Do not make this method virtual.
66 // A derived class should not be able to override this method.
67 public void Dispose()
68 {
69 Dispose(true);
70 // This object will be cleaned up by the Dispose method.
71 // Therefore, you should call GC.SupressFinalize to
72 // take this object off the finalization queue
73 // and prevent finalization code for this object
74 // from executing a second time.
75 GC.SuppressFinalize(this);
76 }
77 // Dispose(bool disposing) executes in two distinct scenarios.
78 // If disposing equals true, the method has been called directly
79 // or indirectly by a user's code. Managed and unmanaged resources
80 // can be disposed.
81 // If disposing equals false, the method has been called by the
82 // runtime from inside the finalizer and you should not reference
83 // other objects. Only unmanaged resources can be disposed.
84 protected virtual void Dispose(bool disposing)
85 {
86 // Check to see if Dispose has already been called.
87 if (!this.disposed)
88 {
89 // If disposing equals true, dispose all managed
90 // and unmanaged resources.
91 if (disposing)
92 {
93 // Dispose managed resources.
94 //component.Dispose();
95 }
96
97 // Call the appropriate methods to clean up
98 // unmanaged resources here.
99 // If disposing is false,
100 // only the following code is executed.
101
102 //// clean up any resources
103 //binReader.Close();
104 //binReader = null;
105 //fileStream = null;
106 // Note disposing has been done.
107 disposed = true;
108
109 }
110 }
111 // Use C# destructor syntax for finalization code.
112 // This destructor will run only if the Dispose method
113 // does not get called.
114 // It gives your base class the opportunity to finalize.
115 // Do not provide destructors in types derived from this class.
116 ~SerializationReader()
117 {
118 // Do not re-create Dispose clean-up code here.
119 // Calling Dispose(false) is optimal in terms of
120 // readability and maintainability.
121 Dispose(false);
122 }
123 #endregion
124 }
125 }

  ViewVC Help
Powered by ViewVC 1.1.22