Exemplo n.º 1
0
        /// <summary>
        /// Updates the total number of written bytes by the length of a Turtle document.
        /// </summary>
        /// <param name="writer">The Turtle writer.</param>
        /// <exception cref="ArgumentNullException"><paramref name="writer"/> is <see langword="null"/>.</exception>
        public static void UpdateStats(SequentialTurtleWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            long bytes   = writer.GetCurrentStreamSize();
            long triples = writer.GetCurrentTripleCount();

            ConsoleHelper.WriteSuccessLine("{0:F1} MB written; {1} triple(s) created.", (double)bytes / 1024 / 1024, triples);
            byteCount   += bytes;
            tripleCount += triples;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Downloads the file to the local file system.
        /// </summary>
        /// <param name="baseUri">The base URI that the relative file name can be expanded from.</param>
        /// <param name="directory">The destination directory.</param>
        /// <returns>The path to the downloaded file.</returns>
        /// <exception cref="ArgumentNullException">Any of the arguments is <see langword="null"/>.</exception>
        /// <remarks>
        /// <para>This method downloads the file to the local file system.
        ///   After downloading, the MD5 hash of the file contents is verified.</para>
        /// </remarks>
        public string Download(Uri baseUri, string directory)
        {
            if (baseUri == null)
            {
                throw new ArgumentNullException("baseUri");
            }

            string fn = Path.Combine(directory, name);

            ConsoleHelper.WriteMilestone("Downloading {0} ...", name);
            using (var client = new WebClient()) {
                client.DownloadFile(new Uri(baseUri, name), fn);
            }
            System.Console.WriteLine(" done.");

            var fInfo = new FileInfo(fn);

            long fileSize = fInfo.Length;

            if (fileSize == this.size)
            {
                ConsoleHelper.WriteSuccessLine("File size of {0} bytes verified.", fileSize);
            }
            else
            {
                ConsoleHelper.WriteWarningLine("File size is {0} bytes, which differs from the expected size of {1} bytes.", fileSize, this.size);
            }

            byte[] hash;
            using (var algo = MD5.Create()) {
                using (var fs = fInfo.OpenRead()) {
                    hash = algo.ComputeHash(fs);
                }
            }
            string fileMD5 = string.Join("", hash.Select(b => string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:X2}", b))).ToLowerInvariant();

            if (fileMD5 == this.md5)
            {
                ConsoleHelper.WriteSuccessLine("MD5 hash ({0}) verified.", fileMD5);
            }
            else
            {
                ConsoleHelper.WriteWarningLine("MD5 hash of file ({0}) does not match the expected one ({1}).", fileMD5, this.md5);
            }

            return(fn);
        }