Exemplo n.º 1
0
    protected IEnumerator DownloadFromFirebaseStorageWithStream()
    {
        StorageReference reference = FirebaseStorage.DefaultInstance
                                     .GetReferenceFromUrl(firebaseStorageLocation);
        // Used to buffer data from the downloaded stream.
        var memoryStream = new System.IO.MemoryStream();
        // Limit the amount of data to render in the text view.
        const int bytesToStoreInMemoryStream = 64 * 1024;

        // Download the file using a stream.
        var task = reference.GetStreamAsync((stream) => {
            var buffer    = new byte[1024];
            int remaining = bytesToStoreInMemoryStream;
            int read;
            // Read data to render in the text view.
            while (remaining > 0 &&
                   (read = stream.Read(buffer, 0,
                                       System.Math.Min(buffer.Length, remaining))) > 0)
            {
                memoryStream.Write(buffer, 0, read);
                remaining -= read;
            }
            // Read remaining data from the stream to simulate the complete download.
            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
            }
        },
                                            // Report as the stream download progresses.
                                            new StorageProgress <DownloadState>(
                                                state => {
            DebugLog(String.Format("Progress: {0} out of {1}", state.BytesTransferred,
                                   state.TotalByteCount));
        }),
                                            System.Threading.CancellationToken.None);

        yield return(new WaitUntil(() => task.IsCompleted));

        if (task.IsFaulted)
        {
            DebugLog(task.Exception.ToString());
            throw task.Exception;
        }
        else
        {
            memoryStream.Position = 0;
            fileContents          = (new System.IO.StreamReader(memoryStream)).ReadToEnd();
            DebugLog("Finished downloading...");
            DebugLog("Contents=" + fileContents);
        }
    }