// constructors public GridFSSeekableDownloadStream( GridFSBucket bucket, IReadBinding binding, GridFSFilesCollectionDocument filesCollectionDocument) : base(bucket, binding, filesCollectionDocument) { }
// constructors protected GridFSDownloadStreamBase( GridFSBucket bucket, IReadBinding binding, GridFSFilesCollectionDocument filesCollectionDocument) { _bucket = bucket; _binding = binding; _filesCollectionDocument = filesCollectionDocument; }
// constructors public GridFSForwardOnlyDownloadStream( GridFSBucket bucket, IReadBinding binding, GridFSFilesCollectionDocument filesCollectionDocument, bool checkMD5) : base(bucket, binding, filesCollectionDocument) { _checkMD5 = checkMD5; if (_checkMD5) { _md5 = MD5.Create(); } _lastChunkNumber = (int)((filesCollectionDocument.Length - 1) / filesCollectionDocument.ChunkSizeBytes); _lastChunkSize = (int)(filesCollectionDocument.Length % filesCollectionDocument.ChunkSizeBytes); if (_lastChunkSize == 0) { _lastChunkSize = filesCollectionDocument.ChunkSizeBytes; } }
private async Task DownloadToStreamAsyncHelper(IReadBindingHandle binding, GridFSFilesCollectionDocument filesCollectionDocument, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { var checkMD5 = options.CheckMD5 ?? false; using (var source = new GridFSForwardOnlyDownloadStream(this, binding.Fork(), filesCollectionDocument, checkMD5)) { var count = source.Length; var buffer = new byte[filesCollectionDocument.ChunkSizeBytes]; while (count > 0) { var partialCount = (int)Math.Min(buffer.Length, count); await source.ReadBytesAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false); await destination.WriteAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false); count -= partialCount; } await source.CloseAsync(cancellationToken).ConfigureAwait(false); } }
private async Task DownloadToStreamHelperAsync(IReadBindingHandle binding, GridFSFilesCollectionDocument filesCollectionDocument, Stream destination, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { var checkMD5 = options.CheckMD5 ?? false; using (var source = new GridFSForwardOnlyDownloadStream(this, binding.Fork(), filesCollectionDocument, checkMD5)) { var count = source.Length; var buffer = new byte[filesCollectionDocument.ChunkSizeBytes]; while (count > 0) { var partialCount = (int)Math.Min(buffer.Length, count); await source.ReadBytesAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false); await destination.WriteAsync(buffer, 0, partialCount, cancellationToken).ConfigureAwait(false); count -= partialCount; } await source.CloseAsync(cancellationToken).ConfigureAwait(false); } }
private async Task<byte[]> DownloadAsBytesHelperAsync(IReadBindingHandle binding, GridFSFilesCollectionDocument filesCollectionDocument, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken)) { if (filesCollectionDocument.Length > int.MaxValue) { throw new NotSupportedException("GridFS stored file is too large to be returned as a byte array."); } using (var destination = new MemoryStream((int)filesCollectionDocument.Length)) { await DownloadToStreamHelperAsync(binding, filesCollectionDocument, destination, options, cancellationToken).ConfigureAwait(false); return destination.GetBuffer(); } }
private GridFSDownloadStream CreateDownloadStream(IReadBindingHandle binding, GridFSFilesCollectionDocument filesCollectionDocument, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { var checkMD5 = options.CheckMD5 ?? false; var seekable = options.Seekable ?? false; if (checkMD5 && seekable) { throw new ArgumentException("CheckMD5 can only be used when Seekable is false."); } if (seekable) { return new GridFSSeekableDownloadStream(this, binding, filesCollectionDocument); } else { return new GridFSForwardOnlyDownloadStream(this, binding, filesCollectionDocument, checkMD5); } }
private async Task <byte[]> DownloadAsBytesAsyncHelper(IReadBindingHandle binding, GridFSFilesCollectionDocument filesCollectionDocument, GridFSDownloadOptions options, CancellationToken cancellationToken = default(CancellationToken)) { if (filesCollectionDocument.Length > int.MaxValue) { throw new NotSupportedException("GridFS stored file is too large to be returned as a byte array."); } using (var destination = new MemoryStream((int)filesCollectionDocument.Length)) { await DownloadToStreamAsyncHelper(binding, filesCollectionDocument, destination, options, cancellationToken).ConfigureAwait(false); return(destination.GetBuffer()); } }
private GridFSDownloadStream CreateDownloadStream(IReadBindingHandle binding, GridFSFilesCollectionDocument filesCollectionDocument, GridFSDownloadOptions options = null, CancellationToken cancellationToken = default(CancellationToken)) { var checkMD5 = options.CheckMD5 ?? false; var seekable = options.Seekable ?? false; if (checkMD5 && seekable) { throw new ArgumentException("CheckMD5 can only be used when Seekable is false."); } if (seekable) { return(new GridFSSeekableDownloadStream(this, binding, filesCollectionDocument)); } else { return(new GridFSForwardOnlyDownloadStream(this, binding, filesCollectionDocument, checkMD5)); } }