private void ValidateWrittenStreamSize(long bytesWritten)
{
#if !PCL
// Check if response stream or it's base stream is a AESDecryptionStream
var stream = Runtime.Internal.Util.WrapperStream.SearchWrappedStream(this.ResponseStream,
(s => s is Runtime.Internal.Util.DecryptStream));
// Don't validate length if response is an encrypted object.
if (stream!=null)
return;
#endif
if (bytesWritten != this.ContentLength)
{
string amzId2;
if(!this.ResponseMetadata.Metadata.TryGetValue(HeaderKeys.XAmzId2Header, out amzId2))
amzId2 = string.Empty;
string amzCfId;
if(!this.ResponseMetadata.Metadata.TryGetValue(HeaderKeys.XAmzCloudFrontIdHeader, out amzCfId))
amzCfId = string.Empty;
string message = null;
if (string.IsNullOrEmpty(amzCfId))
{
message = string.Format(CultureInfo.InvariantCulture,
"The total bytes read {0} from response stream is not equal to the Content-Length {1} for the object {2} in bucket {3}." +
" Request ID = {4} , AmzId2 = {5}.",
bytesWritten, this.ContentLength, this.Key, this.BucketName, this.ResponseMetadata.RequestId, amzId2);
}
else
{
message = string.Format(CultureInfo.InvariantCulture,
"The total bytes read {0} from response stream is not equal to the Content-Length {1} for the object {2} in bucket {3}." +
" Request ID = {4} , AmzId2 = {5} , AmzCfId = {6}.",
bytesWritten, this.ContentLength, this.Key, this.BucketName, this.ResponseMetadata.RequestId, amzId2, amzCfId);
}
throw new StreamSizeMismatchException(message, this.ContentLength, bytesWritten, this.ResponseMetadata.RequestId, amzId2, amzCfId);
}
}