public override int Read(byte[] buffer, int offset, int count) { if (!CanRead) { throw new InvalidOperationException("The current stream is not readable."); } if (count == 0) { return(0); } long p = file.Position; long s = file.Length; // The amount to read, either the length of the array or the amount of // data left available, whichever is smaller. long to_read = Math.Min(count, s - p); if (to_read == 0) { return(0); } // Fill up the array int act_read = (int)to_read; return(file.Read(buffer, offset, act_read)); }
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 static bool LoadDataFile(string filename, IDataFile dataFile, out string statusMsg) { bool success = true; statusMsg = ""; Stream stream = null; try { stream = File.OpenRead(filename); dataFile.Read(stream); } catch (FileNotFoundException) { statusMsg = string.Format("Error opening data file {0}:\r\nFile not found.", filename); success = false; } catch (PathTooLongException) { statusMsg = string.Format("Error opening data file {0}:\r\nPath specified is too long.", filename); success = false; } catch (DirectoryNotFoundException) { statusMsg = string.Format("Error opening data file {0}:\r\nDirectory not found. ", filename); success = false; } catch (UnauthorizedAccessException) { statusMsg = string.Format("Error opening data file {0}:\r\nPermission denied.", filename); success = false; } catch (InvalidDataException exc) { statusMsg = string.Format("Error reading data file {0}:\r\n{1}", filename, exc.Message); success = false; } catch (Exception exc) { statusMsg = string.Format("Unexpected error reading data file {0}:\r\n{1}", filename, exc.Message); success = false; } finally { if (stream != null) { stream.Close(); } } return(success); }
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); }
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 should_overwrite_part_of_page() { var data = new byte[_pageSize]; for (var i = 0; i < _pageSize; i++) { data[i] = (byte)i; } for (var pageNumber = 0; pageNumber < 5; pageNumber++) { Assert.IsTrue(_dataFile.Write((ulong)pageNumber, data)); } Assert.IsTrue(_dataFile.Write(1UL, new byte[] { 99, 100, 101, 102, 103 }, 10)); _dataFile.Dispose(); _dataFile = new DataFile(_file, _startupLog); for (var pageNumber = 0; pageNumber < 5; pageNumber++) { Assert.IsTrue(_dataFile.Read((ulong)pageNumber, data)); for (var i = 0; i < _pageSize; i++) { if (pageNumber == 1 && i >= 10 && i < 15) { Assert.AreEqual(89 + i, data[i]); } else { Assert.AreEqual(i, data[i]); } } } }
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 int Read(byte[] buffer, int offset, int count) { return(df.Read(buffer, offset, count)); }
public int Read(byte[] buffer, int offset, int count) { transaction.CheckValid(); return(parent.Read(buffer, offset, count)); }
public void should_not_return_nonexistent_pages() { var data = new byte[_pageSize]; Assert.IsFalse(_dataFile.Read(0, data)); }
bool IDataFile.Read(ulong pageNumber, byte[] data, uint offset) { return(_versionDataFile.Read(pageNumber, data, offset)); }