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