From f1e11dfc366f2c7ddb77a997a1d480a4542fc881 Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Mon, 15 Aug 2022 20:35:57 +0200 Subject: [PATCH] simplify Stream.ReadAllBytes extension and use it more --- src/BizHawk.Client.Common/RomLoader.cs | 5 ++--- .../movie/tasproj/ZwinderStateManager.cs | 16 +++++++--------- .../config/FirmwaresConfig.cs | 6 ++---- src/BizHawk.Common/Extensions/IOExtensions.cs | 11 +---------- 4 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/BizHawk.Client.Common/RomLoader.cs b/src/BizHawk.Client.Common/RomLoader.cs index 3337128054..d22b4eff2e 100644 --- a/src/BizHawk.Client.Common/RomLoader.cs +++ b/src/BizHawk.Client.Common/RomLoader.cs @@ -5,6 +5,7 @@ using System.IO.Compression; using System.Linq; using BizHawk.Common; +using BizHawk.Common.IOExtensions; using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common; using BizHawk.Emulation.Cores; @@ -497,9 +498,7 @@ namespace BizHawk.Client.Common // TODO: Why does the PSF loader need CbDeflater provided? Surely this is a matter internal to it. static byte[] CbDeflater(Stream instream, int size) { - var ret = new MemoryStream(); - new GZipStream(instream, CompressionMode.Decompress).CopyTo(ret); - return ret.ToArray(); + return new GZipStream(instream, CompressionMode.Decompress).ReadAllBytes(); } var psf = new PSF(); psf.Load(path, CbDeflater); diff --git a/src/BizHawk.Client.Common/movie/tasproj/ZwinderStateManager.cs b/src/BizHawk.Client.Common/movie/tasproj/ZwinderStateManager.cs index 264e96678c..73683157da 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/ZwinderStateManager.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/ZwinderStateManager.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using BizHawk.Common; using BizHawk.Common.CollectionExtensions; +using BizHawk.Common.IOExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Client.Common @@ -67,17 +68,16 @@ namespace BizHawk.Client.Common { get { - var (f, data) = GetStateClosestToFrame(frame); + var (f, dataStream) = GetStateClosestToFrame(frame); if (f != frame) { - data.Dispose(); + dataStream.Dispose(); return NonState; } - var ms = new MemoryStream(); - data.CopyTo(ms); - data.Dispose(); - return ms.ToArray(); + var data = dataStream.ReadAllBytes(); + dataStream.Dispose(); + return data; } } @@ -286,10 +286,8 @@ namespace BizHawk.Client.Common return; } - var ms = new MemoryStream(); using var s = state.GetReadStream(); - s.CopyTo(ms); - _reserved.Add(state.Frame, ms.ToArray()); + _reserved.Add(state.Frame, s.ReadAllBytes()); AddStateCache(state.Frame); } diff --git a/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs b/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs index 6f6e257df5..b637df607d 100644 --- a/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs @@ -10,6 +10,7 @@ using System.Windows.Forms; using BizHawk.Common; using BizHawk.Client.Common; using BizHawk.Common.CollectionExtensions; +using BizHawk.Common.IOExtensions; using BizHawk.Emulation.Common; // notes: eventually, we intend to have a "firmware acquisition interface" exposed to the emulator cores. @@ -656,13 +657,10 @@ namespace BizHawk.Client.EmuHawk foreach (var ai in hf.ArchiveItems) { hf.BindArchiveMember(ai); - var stream = hf.GetStream(); - var ms = new MemoryStream(); - Util.CopyStream(hf.GetStream(), ms, stream.Length); string outfile = ai.Name; string myname = Path.GetFileName(outfile); outfile = Path.Combine(extractPath, myname); - File.WriteAllBytes(outfile, ms.ToArray()); + File.WriteAllBytes(outfile, hf.GetStream().ReadAllBytes()); hf.Unbind(); if (_cbAllowImport.Checked || Manager.CanFileBeImported(outfile)) diff --git a/src/BizHawk.Common/Extensions/IOExtensions.cs b/src/BizHawk.Common/Extensions/IOExtensions.cs index cf4c705418..ddfbe00e3b 100644 --- a/src/BizHawk.Common/Extensions/IOExtensions.cs +++ b/src/BizHawk.Common/Extensions/IOExtensions.cs @@ -11,17 +11,8 @@ namespace BizHawk.Common.IOExtensions public static byte[] ReadAllBytes(this Stream stream) { - const int BUFF_SIZE = 4096; - var buffer = new byte[BUFF_SIZE]; - - int bytesRead; - var inStream = new BufferedStream(stream); var outStream = new MemoryStream(); - - while ((bytesRead = inStream.Read(buffer, 0, BUFF_SIZE)) > 0) - { - outStream.Write(buffer, 0, bytesRead); - } + stream.CopyTo(outStream); return outStream.ToArray(); }