public async Task <int> ReadAsync(byte[] buffer, int bufferOffset, int count, long offset)
        {
            if (_writer == null)
            {
                throw new InvalidDataException("Writer not initialized");
            }
            if (_torrentFile == null)
            {
                throw new InvalidDataException("Torrent file not initialized");
            }

            Position = offset + count;
            return(await Task.Run(() => _writer.Read(_torrentFile, offset, buffer, bufferOffset, count)));
        }
Beispiel #2
0
        byte [] CalcPiecesHash(List <TorrentFile> files, IPieceWriter writer, CancellationToken token)
        {
            int  bufferRead  = 0;
            long fileRead    = 0;
            long overallRead = 0;
            MD5  md5Hasher   = null;

            var shaHasher     = HashAlgoFactory.Create <SHA1> ();
            var torrentHashes = new List <byte> ();
            var overallTotal  = files.Sum(t => t.Length);
            var buffer        = new byte [PieceLength];

            if (StoreMD5)
            {
                md5Hasher = HashAlgoFactory.Create <MD5> ();
            }

            try {
                foreach (TorrentFile file in files)
                {
                    fileRead = 0;
                    md5Hasher?.Initialize();

                    while (fileRead < file.Length)
                    {
                        int toRead = (int)Math.Min(buffer.Length - bufferRead, file.Length - fileRead);
                        int read   = writer.Read(file, fileRead, buffer, bufferRead, toRead);
                        token.ThrowIfCancellationRequested();

                        md5Hasher?.TransformBlock(buffer, bufferRead, read, buffer, bufferRead);
                        shaHasher.TransformBlock(buffer, bufferRead, read, buffer, bufferRead);

                        bufferRead  += read;
                        fileRead    += read;
                        overallRead += read;

                        if (bufferRead == buffer.Length)
                        {
                            bufferRead = 0;
                            shaHasher.TransformFinalBlock(buffer, 0, 0);
                            torrentHashes.AddRange(shaHasher.Hash);
                            shaHasher.Initialize();
                        }
                        Hashed?.InvokeAsync(this, new TorrentCreatorEventArgs(file.Path, fileRead, file.Length, overallRead, overallTotal));
                    }

                    md5Hasher?.TransformFinalBlock(buffer, 0, 0);
                    md5Hasher?.Initialize();
                    file.MD5 = md5Hasher?.Hash;
                }
                if (bufferRead > 0)
                {
                    shaHasher.TransformFinalBlock(buffer, 0, 0);
                    torrentHashes.AddRange(shaHasher.Hash);
                }
            } finally {
                shaHasher.Dispose();
                md5Hasher?.Dispose();
            }
            return(torrentHashes.ToArray());
        }