Пример #1
0
        /// <summary>
        /// Reads in a block from a file.
        /// </summary>
        /// <param name="a_block">Contains parameters to the blocks location.</param>
        /// <param name="a_torrent">The torrent file that contains save directory,
        ///         the file name, and the piece length needed to read the block.
        /// </param>
        /// <returns>Returns a byte array containing the block requested.</returns>
        /// <remarks>
        /// ReadBlock()
        ///
        /// SYNOPSIS
        ///
        ///     byte[] ReadBlock(OutgoingBlock a_block, Torrent a_torrent);
        ///
        /// DESCRIPTION
        ///
        ///     This function will read in the block at the specified piece index,
        ///     beginning offset. It will return a byte array with the block to
        ///     be used when sending a piece to a peer.
        ///
        /// </remarks>
        public static byte[] ReadBlock(OutgoingBlock a_block, Torrent a_torrent)
        {
            string file  = a_torrent.SaveDirectory + "\\" + a_torrent.Name;
            var    block = new byte[a_torrent.ComputePieceLength(a_block.Index)];

            using (var filestream = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                filestream.Position = a_block.Index * a_torrent.PieceLength + a_block.Begin;
                filestream.Read(block, 0, (int)a_block.Length);
            }

            return(block);
        }