public void DownloadFile(
IDocument remoteDocument,
Stream localFileStream,
Transmission transmission,
HashAlgorithm hashAlg,
UpdateChecksum update = null)
{
byte[] buffer = new byte[8 * 1024];
int len;
if (localFileStream.Length > 0) {
localFileStream.Seek(0, SeekOrigin.Begin);
while ((len = localFileStream.Read(buffer, 0, buffer.Length)) > 0) {
hashAlg.TransformBlock(buffer, 0, len, buffer, 0);
}
}
long offset = localFileStream.Position;
long? fileLength = remoteDocument.ContentStreamLength;
if (fileLength <= offset) {
transmission.Length = fileLength.GetValueOrDefault();
transmission.Position = offset;
hashAlg.TransformFinalBlock(new byte[0], 0, 0);
return;
}
DotCMIS.Data.IContentStream contentStream = null;
if (offset > 0) {
long remainingBytes = (long)fileLength - offset;
transmission.Length = remoteDocument.ContentStreamLength;
transmission.Position = offset;
contentStream = remoteDocument.GetContentStream(remoteDocument.ContentStreamId, offset, remainingBytes);
} else {
contentStream = remoteDocument.GetContentStream();
}
using (var transmissionStream = transmission.CreateStream(localFileStream))
using (CryptoStream hashstream = new CryptoStream(transmissionStream, hashAlg, CryptoStreamMode.Write))
using (Stream remoteStream = contentStream != null ? contentStream.Stream : new MemoryStream(0)) {
transmission.Length = remoteDocument.ContentStreamLength;
transmission.Position = offset;
int written = 0;
while ((len = remoteStream.Read(buffer, 0, buffer.Length)) > 0) {
lock (this.disposeLock) {
if (this.disposed) {
transmission.Status = TransmissionStatus.ABORTED;
throw new ObjectDisposedException(transmission.Path);
}
try {
hashstream.Write(buffer, 0, len);
hashstream.Flush();
written += len;
} catch (Exception) {
this.UpdateHash(hashAlg, localFileStream.Length, update);
throw;
}
if (written >= 1024 * 1024) {
this.UpdateHash(hashAlg, localFileStream.Length, update);
written = 0;
}
}
}
if (written > 0) {
this.UpdateHash(hashAlg, localFileStream.Length, update);
}
}
}