CmisSync.Lib.FileTransmission.ChunkedDownloader.DownloadFile C# (CSharp) Метод

DownloadFile() публичный Метод

Downloads the file and returns the SHA-1 hash of the content of the saved file
On any disc or network io exception If the remote object has been disposed before the dowload is finished If download is aborted On exceptions thrown by the CMIS Server/Client
public DownloadFile ( IDocument remoteDocument, Stream localFileStream, Transmission transmission, HashAlgorithm hashAlg, UpdateChecksum update = null ) : void
remoteDocument IDocument Remote document.
localFileStream Stream Local taget file stream.
transmission Transmission Transmission status.
hashAlg System.Security.Cryptography.HashAlgorithm Hash algoritm, which should be used to calculate hash of the uploaded stream content
update UpdateChecksum Not or not yet used
Результат void
        public void DownloadFile(
            IDocument remoteDocument,
            Stream localFileStream,
            Transmission transmission,
            HashAlgorithm hashAlg,
            UpdateChecksum update = null)
        {
            {
                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = localFileStream.Read(buffer, 0, buffer.Length)) > 0) {
                    hashAlg.TransformBlock(buffer, 0, len, buffer, 0);
                }
            }

            long? fileLength = remoteDocument.ContentStreamLength;

            // Download content if exists
            if (fileLength > 0) {
                long offset = localFileStream.Position;
                long remainingBytes = (fileLength != null) ? (long)fileLength - offset : this.ChunkSize;
                try {
                    do {
                        offset += this.DownloadNextChunk(remoteDocument, offset, remainingBytes, transmission, localFileStream, hashAlg);
                    } while(fileLength == null);
                } catch (DotCMIS.Exceptions.CmisConstraintException) {
                }
            } else {
                transmission.Position = 0;
                transmission.Length = 0;
            }

            hashAlg.TransformFinalBlock(new byte[0], 0, 0);
        }

Usage Example

        public void ResumeDownloadWithUtils() {
            long startPos = this.remoteLength / 2;
            SetupResumeDownload(startPos);

            using (var memorystream = new MemoryStream(this.remoteChunk)) {
                this.mockedStream.Setup(stream => stream.Stream).Returns(memorystream);

                using (IFileDownloader downloader = new ChunkedDownloader(this.chunkSize)) {
                    ContentTaskUtils.PrepareResume(startPos, this.localFileStream, this.hashAlg);
                    downloader.DownloadFile(this.mock.Object, this.localFileStream, this.transmission, this.hashAlg);
                    Assert.AreEqual(this.remoteContent.Length, this.localFileStream.Length);
                    Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.remoteContent), this.hashAlg.Hash);
                    Assert.AreEqual(SHA1Managed.Create().ComputeHash(this.localFileStream.ToArray()), this.hashAlg.Hash);
                }
            }
        }
All Usage Examples Of CmisSync.Lib.FileTransmission.ChunkedDownloader::DownloadFile