From d0668192aad1d44fea6516ce0d108146177a7737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Tue, 15 Dec 2020 21:25:53 +0100 Subject: [PATCH] Ensure stream disposal & move entire operation inside task --- osu.Android/OsuGameActivity.cs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/osu.Android/OsuGameActivity.cs b/osu.Android/OsuGameActivity.cs index cf0179e2ac..e801c2ca6e 100644 --- a/osu.Android/OsuGameActivity.cs +++ b/osu.Android/OsuGameActivity.cs @@ -1,7 +1,6 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System.Diagnostics; using System.IO; using System.Threading.Tasks; using Android.App; @@ -62,7 +61,7 @@ namespace osu.Android } } - private void handleImportFromUri(Uri uri) + private void handleImportFromUri(Uri uri) => Task.Factory.StartNew(async () => { // there are more performant overloads of this method, but this one is the most backwards-compatible // (dates back to API 1). @@ -74,23 +73,16 @@ namespace osu.Android cursor.MoveToFirst(); var filenameColumn = cursor.GetColumnIndex(OpenableColumns.DisplayName); - - var stream = ContentResolver.OpenInputStream(uri); string filename = cursor.GetString(filenameColumn); - if (stream != null) - Task.Factory.StartNew(() => runImport(stream, filename), TaskCreationOptions.LongRunning); - } - - private Task runImport(Stream stream, string filename) - { // SharpCompress requires archive streams to be seekable, which the stream opened by // OpenInputStream() seems to not necessarily be. // copy to an arbitrary-access memory stream to be able to proceed with the import. var copy = new MemoryStream(); - stream.CopyTo(copy); + using (var stream = ContentResolver.OpenInputStream(uri)) + await stream.CopyToAsync(copy); - return game.Import(copy, filename); - } + await game.Import(copy, filename); + }, TaskCreationOptions.LongRunning); } }