public void validate_get_file_stream() { var myString = new StringBuilder(); myString.AppendLine("This is a test string"); var stream = StreamFinder.GetFileStream("file4.txt", FileMode.Create); Assert.IsNotNull(stream); }
public void validate_write_text_to_stream() { StringBuilder myString = new StringBuilder(); myString.AppendLine("This is a test string"); Stream stream = StreamFinder.GetFileStream("file4.txt", FileMode.Create); FileIO.WriteTextToStream(myString.ToString(), stream); Assert.IsNotNull(stream); }
private static void LocalWriteArrayToBinary(Array dataIN, string filename, FileMode mode) { // Create a file to write binary data using (Stream s = StreamFinder.GetFileStream(filename, mode)) { using (BinaryWriter bw = new BinaryWriter(s)) { new ArrayCustomBinaryWriter().WriteToBinary(bw, dataIN); } } }
/// <summary> /// Reads Detector from File with given fileName. /// </summary> /// <param name="fileName">filename string of file to be read</param> /// <param name="folderPath">path string where file resides</param> /// <returns>IDetector</returns> public static IDetector ReadDetectorFromFile(string fileName, string folderPath) { try { // allow null filePaths in case writing to isolated storage string filePath; if (folderPath == "") { filePath = fileName; } else { filePath = folderPath + @"/" + fileName; } var detector = FileIO.ReadFromJson <IDetector>(filePath + ".txt"); var binaryArraySerializers = detector.GetBinarySerializers(); if (binaryArraySerializers == null) { return(detector); } foreach (var binaryArraySerializer in binaryArraySerializers) { if (binaryArraySerializer == null) { continue; } using (Stream s = StreamFinder.GetFileStream(filePath + binaryArraySerializer.FileTag, FileMode.Open)) { if (s == null) { continue; } using (BinaryReader br = new BinaryReader(s)) { binaryArraySerializer.ReadData(br); } } } return(detector); } catch (Exception e) { Console.WriteLine("Problem reading detector information from file.\n\nDetails:\n\n" + e + "\n"); } return(null); }
public void validate_copy_stream() { var name = Assembly.GetExecutingAssembly().FullName; var assemblyName = new AssemblyName(name).Name; var stream1 = StreamFinder.GetFileStreamFromResources("Resources/resourcefile.txt", assemblyName); var stream2 = StreamFinder.GetFileStream("file5.txt", FileMode.CreateNew); Assert.IsNotNull(stream1); FileIO.CopyStream(stream1, stream2); Assert.IsNotNull(stream2); Assert.AreEqual(stream1, stream2); }
public void validate_write_json_to_stream() { var pos = new Position(2, 4, 6); Stream stream = StreamFinder.GetFileStream("file6.txt", FileMode.Create); FileIO.WriteJsonToStream(pos, stream); var pos2 = FileIO.ReadFromJson <Position>("file6.txt"); Assert.AreEqual(pos2.X, 2.0); Assert.AreEqual(pos2.Y, 4.0); Assert.AreEqual(pos2.Z, 6.0); }
/// <summary> /// Writes Detector xml for scalar detectors, writes Detector xml and /// binary for 1D and larger detectors. Detector.Name is used for filename. /// </summary> /// <param name="detector">IDetector being written.</param> /// <param name="folderPath">location of written file.</param> public static void WriteDetectorToFile(IDetector detector, string folderPath) { try { // allow null folderPath in case writing to isolated storage string filePath = folderPath; if (folderPath == "") { filePath = detector.Name; } else { filePath = folderPath + @"/" + detector.Name; // desktop folder FileIO.CreateDirectory(folderPath); } FileIO.WriteToJson(detector, filePath + ".txt"); var binaryArraySerializers = detector.GetBinarySerializers(); if (binaryArraySerializers == null) { return; } foreach (var binaryArraySerializer in binaryArraySerializers) { if (binaryArraySerializer == null) { continue; } // Create a file to write binary data using (Stream s = StreamFinder.GetFileStream(filePath + binaryArraySerializer.FileTag, FileMode.OpenOrCreate)) { if (s == null) { continue; } using (BinaryWriter bw = new BinaryWriter(s)) { binaryArraySerializer.WriteData(bw); } } } } catch (Exception e) { Console.WriteLine("Problem writing detector information to file.\n\nDetails:\n\n" + e + "\n"); } }
public void validate_write_to_xml_stream() { var name = Assembly.GetExecutingAssembly().FullName; var assemblyName = new AssemblyName(name).Name; var xmlFile = FileIO.ReadFromXMLInResources <Position>("Resources/fileiotest/file7.xml", assemblyName); Stream stream = StreamFinder.GetFileStream("file8.xml", FileMode.Create); FileIO.WriteToXMLStream(xmlFile, stream); Assert.IsNotNull(stream); Assert.IsTrue(FileIO.FileExists("file8.xml")); Assert.IsTrue(new FileInfo("file8.xml").Length != 0); stream.Close(); }
public void validate_read_from_stream() { var name = Assembly.GetExecutingAssembly().FullName; var assemblyName = new AssemblyName(name).Name; Position pos; // read file from resources and write it so that can be read in var xml = FileIO.ReadFromXMLInResources <Position>("Resources/fileiotest/file7.xml", assemblyName); FileIO.WriteToXML <Position>(xml, "file7.xml"); using (Stream stream = StreamFinder.GetFileStream("file7.xml", FileMode.Open)) { pos = FileIO.ReadFromStream <Position>(stream); } Assert.AreEqual(pos.X, 2); Assert.AreEqual(pos.Y, 4); Assert.AreEqual(pos.Z, 6); }
public void validate_read_from_binary_stream() { var name = Assembly.GetExecutingAssembly().FullName; var assemblyName = new AssemblyName(name).Name; int size = 100; var arrayWritten = new double[size]; // read file from resources and write it so that can be read in arrayWritten = (double[])FileIO.ReadArrayFromBinaryInResources <double> ("Resources/fileiotest/ROfRho", assemblyName, size); FileIO.WriteToBinary <double[]>(arrayWritten, "array5"); double[] arrayRead = new double[100]; using (Stream stream = StreamFinder.GetFileStream("array5", FileMode.Open)) { arrayRead = FileIO.ReadFromBinaryStream <double[]>(stream); } Assert.IsTrue(Math.Abs(arrayRead[2] - 0.052445) < 0.000001); }
public void validate_write_to_binary_stream_and_read_from_binary_stream() { // first create stream, write array, validate written and close stream double[] array = new double[3] { 10, 11, 12 }; Stream streamWrite = StreamFinder.GetFileStream("array4", FileMode.Create); FileIO.WriteToBinaryStream(array, streamWrite); Assert.IsNotNull(streamWrite); Assert.IsTrue(FileIO.FileExists("array4")); Assert.IsTrue(new FileInfo("array4").Length != 0); streamWrite.Close(); // then open stream, read array, validate values and close stream Stream streamRead = StreamFinder.GetFileStream("array4", FileMode.Open); var data = FileIO.ReadFromBinaryStream <double[]>(streamRead); Assert.AreEqual(data[0], 10); streamRead.Close(); }
/// <summary> /// /// </summary> /// <param name="importFiles">the name(s) of the file(s) to import</param> /// <param name="importPath">the path of the files to import (relative or absolute)</param> /// <param name="outname">the name of the resulting output xml spectral dictionary</param> /// <param name="outpath">the output directory of the generated xml dictionary (relative or absolute)</param> public static ChromophoreSpectrumDictionary ImportSpectraFromFile( string[] importFiles = null, string importPath = "", string outname = "SpectralDictionary", string outpath = "") { if (importFiles == null || importFiles.Length == 0 || string.IsNullOrEmpty(importFiles[0])) { importFiles = new string[] { "absorber-*.txt" }; } var allFiles = importFiles.SelectMany(file => Directory.GetFiles( importPath ?? Directory.GetCurrentDirectory(), file)); var chromophoreDictionary = new ChromophoreSpectrumDictionary(); logger.Info(() => "Importing spectral data files"); foreach (var file in allFiles) { if (File.Exists(file)) { try { logger.Info("Importing file: " + file); var stream = StreamFinder.GetFileStream(file, FileMode.Open); SpectralDatabase.AppendDatabaseFromFile(chromophoreDictionary, stream); } catch (Exception e) { logger.Info("**** An error occurred while importing file: " + file); logger.Info("Detailed error: " + e.Message); } } } return(chromophoreDictionary); }