public override int Read(byte[] buffer, int offset, int count)
            {
                if (this.currentBuffer == null)
                {
                    if (documentIndex < sortedDocuments.Count)
                    {
                        this.currentBuffer = DocumentDBMediaExtension.MediaFromDocument(
                            sortedDocuments[documentIndex++]);
                    }
                    else
                    {
                        return(0);
                    }
                }

                int copyLength = Math.Min(count, currentBuffer.Length - (this.streamIndex));

                Buffer.BlockCopy(this.currentBuffer, this.streamIndex, buffer, offset, copyLength);

                this.streamIndex += copyLength;
                this.position    += copyLength;

                if (this.streamIndex >= this.currentBuffer.Length)
                {
                    this.currentBuffer = null;
                    this.streamIndex   = 0;
                }

                return(copyLength);
            }
        public async static Task UploadMediaAsync(this DocumentClient client,
                                                  string databaseId,
                                                  string collectionId,
                                                  string mediaId,
                                                  Stream mediaStream)
        {
            IList <MediaDocument> mediaDocuments = await DocumentDBMediaExtension.DocumentFromMedia(
                mediaId, mediaStream);

            IList <Task> uploadTasks = new List <Task>();

            foreach (MediaDocument mediaDocument in mediaDocuments)
            {
                uploadTasks.Add(client.CreateDocumentAsync(
                                    UriFactory.CreateDocumentCollectionUri(databaseId, collectionId),
                                    mediaDocument));
            }

            await Task.WhenAll(uploadTasks);
        }