Пример #1
0
        /// <summary>
        /// Computes the content crc64.
        /// </summary>
        /// <returns>The content crc64.</returns>
        /// <param name="input">Input.</param>
        /// <param name="length">stream length</param>
        public static string ComputeContentCrc64(Stream input, long length)
        {
            using (Crc64Stream crcStream = new Crc64Stream(input, null, length))
            {
                byte[] buffer    = new byte[32 * 1024];
                int    readCount = 0;
                while (readCount < length)
                {
                    int read = crcStream.Read(buffer, 0, buffer.Length);
                    if (read == 0)
                    {
                        break;
                    }
                    readCount += read;
                }

                if (crcStream.CalculatedHash == null)
                {
                    crcStream.CalculateHash();
                }

                if (crcStream.CalculatedHash == null || crcStream.CalculatedHash.Length == 0)
                {
                    return(string.Empty);
                }
                else
                {
                    return(BitConverter.ToUInt64(crcStream.CalculatedHash, 0).ToString());
                }
            }
        }
        public override void Handle(ServiceResponse response)
        {
            if (_inputStream is Crc64Stream)
            {
                Crc64Stream stream = (Crc64Stream)_inputStream;

                if (stream.CalculatedHash == null)
                {
                    stream.CalculateHash();
                }
                if (response.Headers.ContainsKey(HttpHeaders.HashCrc64Ecma) && stream.CalculatedHash != null && stream.CalculatedHash.Length != 0)
                {
                    var sdkCalculatedHash    = BitConverter.ToUInt64(stream.CalculatedHash, 0);
                    var ossCalculatedHashStr = response.Headers[HttpHeaders.HashCrc64Ecma];
                    if (!sdkCalculatedHash.ToString().Equals(ossCalculatedHashStr))
                    {
                        response.Dispose();
                        throw new ClientException("Crc64 validation failed. Expected hash not equal to calculated hash");
                    }
                }
            }
        }
        private void DoResumableDownloadSingleThread(DownloadObjectRequest request, ResumableDownloadContext resumableContext,
                                                     EventHandler <StreamTransferProgressArgs> downloadProgressCallback)
        {
            _downloadedBytes = resumableContext.GetDownloadedBytes();
            if (!request.MatchingETagConstraints.Contains(resumableContext.ETag))
            {
                request.MatchingETagConstraints.Add(resumableContext.ETag);
            }

            long totalBytes = resumableContext.GetTotalBytes();

            foreach (var part in resumableContext.PartContextList)
            {
                if (part.IsCompleted)
                {
                    // is CRC is enabled and part.Crc64 is 0, then redownload the data
                    if (!_conf.EnableCrcCheck || part.Crc64 != 0)
                    {
                        continue;
                    }
                }

                using (Stream fs = File.Open(GetTempDownloadFile(request), FileMode.OpenOrCreate))
                {
                    fs.Seek(part.Position, SeekOrigin.Begin);
                    var originalStream = fs;
                    if (downloadProgressCallback != null)
                    {
                        originalStream = _ossClient.SetupDownloadProgressListeners(fs,
                                                                                   totalBytes,
                                                                                   _downloadedBytes,
                                                                                   _conf.ProgressUpdateInterval,
                                                                                   downloadProgressCallback);
                    }

                    if (_conf.EnableCrcCheck)
                    {
                        originalStream = new Crc64Stream(originalStream, null, part.Length);
                    }

                    var getPartRequest = request.ToGetObjectRequest();
                    getPartRequest.SetRange(part.Position, part.Length + part.Position - 1);

                    var partResult = _ossClient.GetObject(getPartRequest);
                    WriteTo(partResult.Content, originalStream);

                    if (originalStream is Crc64Stream)
                    {
                        Crc64Stream crcStream = originalStream as Crc64Stream;

                        if (crcStream.CalculatedHash == null)
                        {
                            crcStream.CalculateHash();
                        }

                        part.Crc64 = BitConverter.ToUInt64(crcStream.CalculatedHash, 0);
                    }
                }

                part.IsCompleted = true;
                resumableContext.Dump();
                _downloadedBytes += part.Length;
            }

            Validate(request, resumableContext);
        }