/// <summary> /// Reads data from the disk /// </summary> /// <param name="position">The starting position</param> /// <param name="buffer">the byte buffer of data to read</param> /// <param name="length">the number of bytes to read</param> /// <returns>the number of bytes read</returns> public int ReadRaw(long position, byte[] buffer, int length) { bool needsOpen = m_stream is null; try { if (needsOpen) { Open(); } int totalLengthRead = 0; int len = 0; while (length > 0) { using (m_isUsingStream.EnterReadLock()) { Task <int> results; lock (m_syncRoot) { m_stream.Position = position; results = m_stream.ReadAsync(buffer, 0, length); } len = results.Result; } totalLengthRead += len; if (len == length) { return(totalLengthRead); } if (len == 0 && position >= m_length) { return(totalLengthRead); //End of the stream has occurred } if (len != 0) { position += len; length -= len; //Keep Reading } else { Log.Publish(MessageLevel.Warning, "File Read Error", $"The OS has closed the following file {m_stream.Name}. Attempting to reopen."); ReopenFile(); } } return(length); } finally { if (needsOpen) { Close(); } } }
/// <summary> /// Reads data from the disk /// </summary> /// <param name="position">The starting position</param> /// <param name="buffer">the byte buffer of data to read</param> /// <param name="length">the number of bytes to read</param> /// <returns>the number of bytes read</returns> public int ReadRaw(long position, byte[] buffer, int length) { using (m_isUsingStream.EnterReadLock()) { Task <int> results; lock (m_syncRoot) { m_stream.Position = position; results = m_stream.ReadAsync(buffer, 0, length); } return(results.Result); } }
/// <summary> /// Creates a new <see cref="SortedTreeTable{TKey,TValue}"/> based on the settings passed to this class. /// Once created, it is up to he caller to make sure that this class is properly disposed of. /// </summary> /// <param name="estimatedSize">The estimated size of the file. -1 to ignore this feature and write to the first available directory.</param> /// <returns></returns> public SortedTreeTable <TKey, TValue> CreateArchiveFile(long estimatedSize = -1) { using (m_lock.EnterReadLock()) { if (m_settings.IsMemoryArchive) { SortedTreeFile af = SortedTreeFile.CreateInMemory(blockSize: 4096, flags: m_settings.Flags.ToArray()); return(af.OpenOrCreateTable <TKey, TValue>(m_settings.EncodingMethod)); } else { string fileName = CreateArchiveName(GetPathWithEnoughSpace(estimatedSize)); SortedTreeFile af = SortedTreeFile.CreateFile(fileName, blockSize: 4096, flags: m_settings.Flags.ToArray()); return(af.OpenOrCreateTable <TKey, TValue>(m_settings.EncodingMethod)); } } }