byte[] ReadFileBytes(long Position, long Size) { if (FileBytes != null) { return(FileBytes.SubArray(Position, Size)); } #if USE_MEMORY_MAPPED_FILE var Data = new byte[Size]; // gr: [on OSX at least] you can read past the file size, (but within capacity) // this doesn't error, but does fill the bytes with zeros. var BytesRead = FileView.ReadArray(Position, Data, 0, (int)Size); if (BytesRead != Size) { throw new System.Exception("Memory mapped file only read " + BytesRead + "/" + Size + " bytes"); } return(Data); #elif USE_FILE_HANDLE var Data = new byte[Size]; var NewPos = File.Seek(Position, System.IO.SeekOrigin.Begin); if (NewPos != Position) { throw new System.Exception("Seeked to " + Position + " but stream is at " + NewPos); } var BytesRead = File.Read(Data, 0, (int)Size); if (BytesRead != Size) { throw new System.Exception("FileStream only read " + BytesRead + "/" + Size + " bytes"); } return(Data); #elif USE_JAVA_FILEHANDLE return(File.ReadBytes(Position, Size)); #else return(FileBytes.SubArray(Position, Size)); #endif }
/// <summary> /// Given a memory mapped file, creates a buffere, and reads data into it. /// </summary> public static byte[] ReadBytes(this MemoryMappedFile mmf, long offset, int count) { return(mmf.ReadBytes(offset, new byte[count])); }