/// <summary> /// Reads an object of the specified Type from an existing data file. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="file"></param> /// <param name="method"></param> /// <returns></returns> public static T ReadObject <T>(string file, FormattingMethod method = FormattingMethod.Unknown) { using (FileStream str = File.OpenRead(file)) { return(Formatter.ReadObject <T>(str, method)); } }
/// <summary> /// Saves an object to the specified data stream. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="stream"></param> /// <param name="method"></param> public static void WriteObject <T>(T obj, Stream stream, FormattingMethod method = FormattingMethod.Unknown) { using (Formatter formatter = Formatter.Create(stream, method)) { formatter.WriteObject(obj); } }
/// <summary> /// Reads an object of the specified Type from an existing data Stream. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stream"></param> /// <param name="method"></param> /// <returns></returns> public static T ReadObject <T>(Stream stream, FormattingMethod method = FormattingMethod.Unknown) { using (Formatter formatter = Formatter.Create(stream, method)) { return(formatter.ReadObject <T>()); } }
internal static void InitDefaultMethod() { if (DualityApp.ExecEnvironment == DualityApp.ExecutionEnvironment.Editor) { defaultMethod = FormattingMethod.Xml; } else { defaultMethod = FormattingMethod.Binary; } if (Directory.Exists(DualityApp.DataDirectory)) { foreach (string anyResource in Directory.EnumerateFiles(DualityApp.DataDirectory, "*" + Resource.FileExt, SearchOption.AllDirectories)) { using (FileStream stream = File.OpenRead(anyResource)) { try { defaultMethod = XmlFormatterBase.IsXmlStream(stream) ? FormattingMethod.Xml : FormattingMethod.Binary; break; } catch (Exception) {} } } } }
/// <summary> /// Creates a new MetaFormat Formatter using the specified stream for I/O. /// </summary> /// <param name="stream">The stream to use.</param> /// <param name="method"> /// The formatting method to prefer. If <see cref="FormattingMethod.Unknown"/> is specified, if the stream /// is read- and seekable, auto-detection is used. Otherwise, the <see cref="DefaultMethod">default formatting method</see> is used. /// </param> /// <returns>A newly created MetaFormat Formatter meeting the specified criteria.</returns> public static Formatter CreateMeta(Stream stream, FormattingMethod method = FormattingMethod.Unknown) { if (method == FormattingMethod.Unknown) { if (stream.CanRead && stream.CanSeek && stream.Length > 0) { if (XmlFormatterBase.IsXmlStream(stream)) { method = FormattingMethod.Xml; } else { method = FormattingMethod.Binary; } } else { method = defaultMethod; } } if (method == FormattingMethod.Xml) { return(new MetaFormat.XmlMetaFormatter(stream)); } else { return(new MetaFormat.BinaryMetaFormatter(stream)); } }
private void CreateReferenceFile <T>(string name, T writeObj, FormattingMethod format) { string filePath = TestHelper.GetEmbeddedResourcePath(GetReferenceResourceName(name, format), ".dat"); using (FileStream stream = File.Open(filePath, FileMode.Create)) using (Formatter formatter = Formatter.Create(stream, format)) { formatter.WriteObject(writeObj); } }
private void TestDataEqual <T>(string name, T writeObj, FormattingMethod format) { T readObj; byte[] data = (byte[])TestRes.ResourceManager.GetObject(this.GetReferenceResourceName(name, format), System.Globalization.CultureInfo.InvariantCulture); using (MemoryStream stream = new MemoryStream(data)) using (Formatter formatter = Formatter.Create(stream, format)) { formatter.ReadObject(out readObj); } Assert.IsTrue(writeObj.Equals(readObj), "Failed data equality check of Type {0} with Value {1}", typeof(T), writeObj); }
/// <summary> /// Saves an object to the specified data file. If it already exists, the file will be overwritten. /// Automatically creates the appropriate directory structure, if it doesn't exist yet. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <param name="file"></param> /// <param name="method"></param> public static void WriteObject <T>(T obj, string file, FormattingMethod method = FormattingMethod.Unknown) { string dirName = Path.GetDirectoryName(file); if (!string.IsNullOrEmpty(dirName) && !Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } using (FileStream str = File.Open(file, FileMode.Create)) { Formatter.WriteObject <T>(obj, str, method); } }
/// <summary> /// Reads an object of the specified Type from an existing data Stream, expecting that it might fail. /// This method does not throw an Exception when the file does not exist or an expected /// error occurred during the read operation. Instead, it will simply return null in these cases. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="stream"></param> /// <param name="method"></param> /// <returns></returns> public static T TryReadObject <T>(Stream stream, FormattingMethod method = FormattingMethod.Unknown) { try { using (Formatter formatter = Formatter.Create(stream, method)) { return(formatter.ReadObject <T>()); } } catch (Exception) { return(default(T)); } }
/// <summary> /// Reads an object of the specified Type from an existing data file, expecting that it might fail. /// This method does not throw an Exception when the file does not exist or another /// error occurred during the read operation. Instead, it will simply return null in these cases. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="file"></param> /// <param name="method"></param> /// <returns></returns> public static T TryReadObject <T>(string file, FormattingMethod method = FormattingMethod.Unknown) { try { if (!File.Exists(file)) { return(default(T)); } using (FileStream str = File.OpenRead(file)) { return(Formatter.TryReadObject <T>(str, method)); } } catch (Exception) { return(default(T)); } }
private void TestRandomAccess <T>(T writeObjA, T writeObjB, FormattingMethod format) { T readObjA; T readObjB; using (MemoryStream stream = new MemoryStream()) { long posB = 0; long posA = 0; // Write using (Formatter formatter = Formatter.Create(stream, format)) { posB = stream.Position; formatter.WriteObject(writeObjB); posA = stream.Position; formatter.WriteObject(writeObjA); stream.Position = posB; formatter.WriteObject(writeObjB); stream.Position = posA; readObjA = (T)formatter.ReadObject(); stream.Position = posB; readObjB = (T)formatter.ReadObject(); stream.Position = posA; formatter.WriteObject(writeObjA); stream.Position = posB; formatter.WriteObject(writeObjB); } // Read using (Formatter formatter = Formatter.Create(stream, format)) { stream.Position = posA; readObjA = (T)formatter.ReadObject(); stream.Position = posB; readObjB = (T)formatter.ReadObject(); } } Assert.IsTrue(writeObjA.Equals(readObjA), "Failed random access WriteRead of Type {0} with Value {1}", typeof(T), writeObjA); Assert.IsTrue(writeObjB.Equals(readObjB), "Failed random access WriteRead of Type {0} with Value {1}", typeof(T), writeObjB); }
private void TestWriteRead <T>(T writeObj, FormattingMethod format) { T readObj; using (MemoryStream stream = new MemoryStream()) { // Write using (Formatter formatterWrite = Formatter.Create(stream, format)) { formatterWrite.WriteObject(writeObj); } // Read stream.Position = 0; using (Formatter formatterRead = Formatter.Create(stream)) { readObj = formatterRead.ReadObject <T>(); } } Assert.IsTrue(writeObj.Equals(readObj), "Failed single WriteRead of Type {0} with Value {1}", typeof(T), writeObj); }
[Test] public void ConvertFormat([ValueSource("OtherFormats")] FormattingMethod to) { Random rnd = new Random(); TestObject data = new TestObject(rnd); TestObject dataResult; using (MemoryStream stream = new MemoryStream()) { // Write old format using (Formatter formatterWrite = Formatter.Create(stream, this.PrimaryFormat)) { formatterWrite.WriteObject(data); } // Read stream.Position = 0; using (Formatter formatterRead = Formatter.Create(stream)) { formatterRead.ReadObject(out dataResult); } // Write new format using (Formatter formatterWrite = Formatter.Create(stream, to)) { formatterWrite.WriteObject(data); } // Read stream.Position = 0; using (Formatter formatterRead = Formatter.Create(stream)) { formatterRead.ReadObject(out dataResult); } } Assert.IsTrue(data.Equals(dataResult)); }
private void TestSequential <T>(T writeObjA, T writeObjB, FormattingMethod format) { T readObjA; T readObjB; using (MemoryStream stream = new MemoryStream()) { long beginPos = stream.Position; // Write using (Formatter formatter = Formatter.Create(stream, format)) { stream.Position = beginPos; formatter.WriteObject(writeObjA); formatter.WriteObject(writeObjB); stream.Position = beginPos; readObjA = (T)formatter.ReadObject(); readObjB = (T)formatter.ReadObject(); stream.Position = beginPos; formatter.WriteObject(writeObjA); formatter.WriteObject(writeObjB); } // Read stream.Position = beginPos; using (Formatter formatter = Formatter.Create(stream)) { readObjA = (T)formatter.ReadObject(); readObjB = (T)formatter.ReadObject(); } } Assert.IsTrue(writeObjA.Equals(readObjA), "Failed sequential WriteRead of Type {0} with Value {1}", typeof(T), writeObjA); Assert.IsTrue(writeObjB.Equals(readObjB), "Failed sequential WriteRead of Type {0} with Value {1}", typeof(T), writeObjB); }
private string GetReferenceResourceName(string name, FormattingMethod format) { return(string.Format("FormatterTest{0}{1}Data", name, format)); }
public FormatterTest(FormattingMethod format) { this.format = format; }
/// <summary> /// Creates a new Formatter using the specified stream for I/O. /// </summary> /// <param name="stream">The stream to use.</param> /// <param name="method"> /// The formatting method to prefer. If <see cref="FormattingMethod.Unknown"/> is specified, if the stream /// is read- and seekable, auto-detection is used. Otherwise, the <see cref="DefaultMethod">default formatting method</see> is used. /// </param> /// <returns>A newly created Formatter meeting the specified criteria.</returns> public static Formatter Create( Stream stream, FormattingMethod method = FormattingMethod.Unknown ) { /*if ( method == FormattingMethod.Unknown ) { if ( stream.CanRead && stream.CanSeek && stream.Length > 0 ) { if ( XmlFormatterBase.IsXmlStream( stream ) ) method = FormattingMethod.Xml; else method = FormattingMethod.Binary; } else method = defaultMethod; }*/ //if ( method == FormattingMethod.Xml ) // return new XmlFormatter( stream ); //else return new BinaryFormatter( stream ); }