public void Process(IList <string> filesIn, IList <string> filesOut, ContourFormat formatIn, ContourFormat formatOut, bool changeFileExtension) { ContourFileFactory factory = new ContourFileFactory(); IDataFile <Contour> dataFileIn = factory.GetFile(formatIn); IDataFile <Contour> dataFileOut = factory.GetFile(formatOut); int successCount = 0; for (int i = 0; i < filesIn.Count(); i++) { try { Logger.AddMessage(String.Format("Обрабатывается файл '{0}'. {1} из {2}", filesIn[i], i + 1, filesIn.Count)); Contour contour = dataFileIn.Read(filesIn[i]); if (changeFileExtension) { filesOut[i] = Path.ChangeExtension(filesOut[i], dataFileOut.DefaultExtension); } dataFileOut.Write(filesOut[i], contour); successCount++; Logger.AddMessage(String.Format("Файл успешно обработан.")); } catch (Exception e) { Logger.AddMessage(String.Format("Ошибка при обработке файла. {0}", e.Message)); } } Logger.AddEmptyLine(); Logger.AddMessage(String.Format("Успешно обработано файлов: {0} из {1}.", successCount, filesIn.Count)); }
public override void Write(byte[] buffer, int offset, int count) { if (!CanWrite) { throw new InvalidOperationException("The current stream is not writeable."); } file.Write(buffer, offset, count); }
public void should_save_and_return_pages() { for (var pageNumber = 0; pageNumber < 5; pageNumber++) { var data = new byte[_pageSize]; data[0] = (byte)(pageNumber * 3); Assert.IsTrue(_dataFile.Write((ulong)pageNumber, data)); } _dataFile.Dispose(); _dataFile = new DataFile(_file, _startupLog); Assert.AreEqual(_pageSize, _dataFile.PageSize); for (var pageNumber = 0; pageNumber < 5; pageNumber++) { var data = new byte[_pageSize]; Assert.IsTrue(_dataFile.Read((ulong)pageNumber, data)); Assert.AreEqual((byte)(pageNumber * 3), data[0]); } }
public void ReplicateTo(IDataFile destFile) { // This is a little complex. If 'destFile' is an instance of SubDataFile // we use the raw 'ReplicateTo' method on the data files and preserve // the header on the target by making a copy of it before the replicateTo // function. // Otherwise, we use a 'CopyTo' implementation. // If replicating to a SubDataFile if (destFile is SubDataFile) { // Preserve the header of the target SubDataFile targetFile = (SubDataFile)destFile; long headerSize = targetFile.start; if (headerSize <= 8192) { IDataFile targetDf = targetFile.df; // Make a copy of the header in the target, int iheadSize = (int)headerSize; byte[] header = new byte[iheadSize]; targetDf.Position = 0; targetDf.Read(header, 0, iheadSize); // Replicate the bases df.ReplicateTo(targetDf); // Now 'target_df' will be a replica of this, so we need to copy // the previous header back on the target. // Remove the replicated header on the target and copy the old one // back. targetDf.Position = start; targetDf.Shift(iheadSize - start); targetDf.Position = 0; targetDf.Write(header, 0, iheadSize); // Set position per spec targetDf.Position = targetDf.Length; // Done. return; } } // Fall back to a copy-to implementation destFile.Delete(); destFile.Position = 0; df.Position = start; df.CopyTo(destFile, df.Length - start); }
private static void ByteBufferCopyTo(IDataFile source, IDataFile target, long size) { long pos = target.Position; // Make room to insert the data target.Shift(size); target.Position = pos; // Set a 1k buffer byte[] buf = new byte[1024]; // While there is data to copy, while (size > 0) { // Read an amount of data from the source int toRead = (int) Math.Min(buf.Length, size); // Read it into the buffer source.Read(buf, 0, toRead); // Write from the buffer out to the target target.Write(buf, 0, toRead); // Update the ref size = size - toRead; } }
public void Write(byte[] buffer, int offset, int count) { df.Write(buffer, offset, count); }
public void Write(byte[] buffer, int offset, int count) { transaction.CheckValid(); LogChange(); parent.Write(buffer, offset, count); }
public static bool SaveDataFile(string filename, IDataFile dataFile, out string statusMsg) { bool success = true; statusMsg = ""; string workingFilename = Path.ChangeExtension(filename, "new"); string backupFilename = filename + ".bak"; //Write the temp file Stream stream = null; try { stream = File.OpenWrite(workingFilename); dataFile.Write(stream); } catch (FileNotFoundException) { statusMsg = string.Format("Error saving data file {0}:\r\nFile not found.", filename); success = false; } catch (PathTooLongException) { statusMsg = string.Format("Error saving data file {0}:\r\nPath specified is too long.", filename); success = false; } catch (DirectoryNotFoundException) { statusMsg = string.Format("Error saving data file {0}:\r\nDirectory not found. ", filename); success = false; } catch (UnauthorizedAccessException) { statusMsg = string.Format("Error saving data file {0}:\r\nPermission denied.", filename); success = false; } catch (InvalidDataException exc) { statusMsg = string.Format("Error saving data file {0}:\r\n{1}", filename, exc.Message); success = false; } catch (Exception exc) { statusMsg = string.Format("Unexpected error saving data file {0}:\r\n{1}", filename, exc.Message); success = false; } finally { if (stream != null) { stream.Close(); } } if (!success) { return(success); } //Doing backup try { File.Delete(backupFilename); } catch (FileNotFoundException) { } //Discover this with our face to avoid a 1/1000000 race condition catch (DirectoryNotFoundException) { } //these are common and shouldn't cause the fail condition catch (UnauthorizedAccessException exc) { statusMsg = string.Format("Cannot delete old backup file {0}:\r\nPermission denied.", backupFilename); success = false; } catch (IOException exc) { statusMsg = string.Format("Cannot delete old backup file {0}:\r\nIO error occurred.", backupFilename); success = false; } if (!success) { return(success); //Can potentially recover but eh something's fishy already } try { File.Move(filename, backupFilename); } catch (FileNotFoundException) { } catch (DirectoryNotFoundException) { } //Discover this with our face to avoid a 1/1000000 race condition catch (UnauthorizedAccessException exc) { statusMsg = string.Format("Cannot move old data file {0}:\r\nPermission denied.", filename); } catch (IOException exc) { statusMsg = string.Format("Cannot move old data file {0}:\r\nIO error occurred.\r\n", filename); } if (!success) { return(success); //Well this is fatal, can't rewrite the old file. } try { File.Move(workingFilename, filename); } catch (FileNotFoundException) { } catch (DirectoryNotFoundException) { } //Discover this with our face to avoid a 1/1000000 race condition catch (UnauthorizedAccessException exc) { statusMsg = string.Format("Cannot move new data file {0}:\r\nPermission denied.", filename); } catch (IOException exc) { statusMsg = string.Format("Cannot move new data file {0}:\r\nIO error occurred.\r\n", filename); } if (!success) { return(success); //Well this is fatal, can't rewrite the old file. } return(success); }
bool IDataFile.Write(ulong pageNumber, byte[] data, uint offset) { return(_versionDataFile.Write(pageNumber, data, offset)); }