1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +08:00

Ensure stream disposal & move entire operation inside task

This commit is contained in:
Bartłomiej Dach 2020-12-15 21:25:53 +01:00
parent f9d7945a6f
commit d0668192aa

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Android.App; 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 // there are more performant overloads of this method, but this one is the most backwards-compatible
// (dates back to API 1). // (dates back to API 1).
@ -74,23 +73,16 @@ namespace osu.Android
cursor.MoveToFirst(); cursor.MoveToFirst();
var filenameColumn = cursor.GetColumnIndex(OpenableColumns.DisplayName); var filenameColumn = cursor.GetColumnIndex(OpenableColumns.DisplayName);
var stream = ContentResolver.OpenInputStream(uri);
string filename = cursor.GetString(filenameColumn); 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 // SharpCompress requires archive streams to be seekable, which the stream opened by
// OpenInputStream() seems to not necessarily be. // OpenInputStream() seems to not necessarily be.
// copy to an arbitrary-access memory stream to be able to proceed with the import. // copy to an arbitrary-access memory stream to be able to proceed with the import.
var copy = new MemoryStream(); 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);
} }
} }