Exemplo n.º 1
0
        /// <summary>
        /// Saves block contents to index file
        /// </summary>
        /// <param name="data">Block object</param>
        /// <param name="offset">Desired offset of block in index file, default in null, that means append to the end of file</param>
        /// <returns>offset of the written block in index file</returns>
        public long Write(ISerializableIndexData data, long?offset = null)
        {
            var length = data.SerializedLength;
            var buffer = new byte[length];

            data.Serialize(buffer, 0);

            lock (fileStreamLock)
            {
                var newOffset = fileStream.Seek(offset ?? fileStream.Length, SeekOrigin.Begin);
                fileStream.Write(buffer, 0, length);
                fileStream.Flush();
                return(newOffset);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Loads block contents from index file
        /// </summary>
        /// <param name="data">Block object</param>
        /// <param name="offset">Offset of block in index file</param>
        /// <param name="length">Length of block in index file</param>
        /// <exception cref="IOException">not enough data in index file to fill the block</exception>
        public void Read(ISerializableIndexData data, long offset, int length)
        {
            var buffer = new byte[length];

            lock (fileStreamLock)
            {
                fileStream.Seek(offset, SeekOrigin.Begin);
                var bytesRead = fileStream.Read(buffer, 0, length);
                if (bytesRead != length)
                {
                    throw new IOException(IndexBlockReadErrorMessage);
                }
            }

            data.Deserialize(buffer, 0);
        }