From a9aa65397cc62cfc94f9f744d434fa6db7db3a57 Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 3 Jul 2014 18:29:51 +0000 Subject: [PATCH] Convert a Util method to an extension method --- BizHawk.Client.Common/XmlGame.cs | 3 +- BizHawk.Client.Common/movie/MovieImport.cs | 31 ++++++++++--------- BizHawk.Client.EmuHawk/MainForm.cs | 5 ++- .../tools/HexEditor/HexEditor.cs | 3 +- BizHawk.Common/Extensions/IOExtensions.cs | 17 ++++++++++ BizHawk.Common/Util.cs | 17 ---------- 6 files changed, 41 insertions(+), 35 deletions(-) diff --git a/BizHawk.Client.Common/XmlGame.cs b/BizHawk.Client.Common/XmlGame.cs index b33fba6ece..9015f5f608 100644 --- a/BizHawk.Client.Common/XmlGame.cs +++ b/BizHawk.Client.Common/XmlGame.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Xml; using BizHawk.Common; +using BizHawk.Common.IOExtensions; using BizHawk.Emulation.Common; namespace BizHawk.Client.Common @@ -62,7 +63,7 @@ namespace BizHawk.Client.Common f.Unbind(); f.BindArchiveMember(ai); - data = Util.ReadAllBytes(f.GetStream()); + data = f.GetStream().ReadAllBytes(); } else { diff --git a/BizHawk.Client.Common/movie/MovieImport.cs b/BizHawk.Client.Common/movie/MovieImport.cs index 7460febafd..d2fe02b255 100644 --- a/BizHawk.Client.Common/movie/MovieImport.cs +++ b/BizHawk.Client.Common/movie/MovieImport.cs @@ -5,6 +5,7 @@ using System.Text; using System.IO; using BizHawk.Common; +using BizHawk.Common.IOExtensions; using BizHawk.Emulation.Common; using BizHawk.Client.Common.MovieConversionExtensions; @@ -1054,7 +1055,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string authors = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)); + string authors = Encoding.UTF8.GetString(stream.ReadAllBytes()); string author_list = ""; string author_last = ""; using (StringReader reader = new StringReader(authors)) @@ -1089,7 +1090,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string coreversion = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string coreversion = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); m.Comments.Add(COREORIGIN + " " + coreversion); hf.Unbind(); } @@ -1097,7 +1098,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string gamename = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string gamename = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); m.Header[HeaderKeys.GAMENAME] = gamename; hf.Unbind(); } @@ -1105,7 +1106,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string gametype = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string gametype = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); // TODO: Handle the other types. switch (gametype) { @@ -1130,7 +1131,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string input = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)); + string input = Encoding.UTF8.GetString(stream.ReadAllBytes()); int lineNum = 0; using (StringReader reader = new StringReader(input)) { @@ -1156,7 +1157,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - byte[] moviesram = Util.ReadAllBytes(stream); + byte[] moviesram = stream.ReadAllBytes(); if (moviesram.Length != 0) { errorMsg = "Movies that begin with SRAM are not supported."; @@ -1169,7 +1170,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string port1 = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string port1 = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); m.Header[PORT1] = port1; hf.Unbind(); } @@ -1177,7 +1178,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string port2 = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string port2 = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); m.Header[PORT2] = port2; hf.Unbind(); } @@ -1185,7 +1186,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string projectid = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string projectid = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); m.Header[PROJECTID] = projectid; hf.Unbind(); } @@ -1193,7 +1194,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string rerecords = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)); + string rerecords = Encoding.UTF8.GetString(stream.ReadAllBytes()); int rerecordCount; // Try to parse the re-record count as an integer, defaulting to 0 if it fails. try @@ -1211,7 +1212,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string rom = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string rom = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); int pos = item.Name.LastIndexOf(".sha256"); string name = item.Name.Substring(0, pos); m.Header[SHA256 + "_" + name] = rom; @@ -1226,7 +1227,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string subtitles = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)); + string subtitles = Encoding.UTF8.GetString(stream.ReadAllBytes()); using (StringReader reader = new StringReader(subtitles)) { string line; @@ -1239,7 +1240,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string startSecond = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string startSecond = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); m.Header[STARTSECOND] = startSecond; hf.Unbind(); } @@ -1247,7 +1248,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string startSubSecond = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string startSubSecond = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); m.Header[STARTSUBSECOND] = startSubSecond; hf.Unbind(); } @@ -1255,7 +1256,7 @@ namespace BizHawk.Client.Common { hf.BindArchiveMember(item.Index); var stream = hf.GetStream(); - string systemid = Encoding.UTF8.GetString(Util.ReadAllBytes(stream)).Trim(); + string systemid = Encoding.UTF8.GetString(stream.ReadAllBytes()).Trim(); m.Comments.Add(EMULATIONORIGIN + " " + systemid); hf.Unbind(); } diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs index 596639cc38..16fb87f486 100644 --- a/BizHawk.Client.EmuHawk/MainForm.cs +++ b/BizHawk.Client.EmuHawk/MainForm.cs @@ -10,6 +10,7 @@ using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Common; +using BizHawk.Common.IOExtensions; using BizHawk.Bizware.BizwareGL; using BizHawk.Emulation.Common; using BizHawk.Emulation.Common.IEmulatorExtensions; @@ -112,7 +113,9 @@ namespace BizHawk.Client.EmuHawk using (var NesCartFile = new HawkFile(Path.Combine(PathManager.GetExeDirectoryAbsolute(), "gamedb", "NesCarts.7z")).BindFirst()) { - return Util.ReadAllBytes(NesCartFile.GetStream()); + return NesCartFile + .GetStream() + .ReadAllBytes(); } }; diff --git a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs index cdaf19b88d..9a5ba4e578 100644 --- a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs +++ b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs @@ -11,6 +11,7 @@ using System.Windows.Forms; using BizHawk.Common; using BizHawk.Common.NumberExtensions; using BizHawk.Common.StringExtensions; +using BizHawk.Common.IOExtensions; using BizHawk.Emulation.Common; using BizHawk.Client.Common; @@ -355,7 +356,7 @@ namespace BizHawk.Client.EmuHawk if (file.IsArchive) { var stream = file.GetStream(); - return Util.ReadAllBytes(stream); + return stream.ReadAllBytes(); } return File.ReadAllBytes(path); diff --git a/BizHawk.Common/Extensions/IOExtensions.cs b/BizHawk.Common/Extensions/IOExtensions.cs index ee628f0853..8d63178a3a 100644 --- a/BizHawk.Common/Extensions/IOExtensions.cs +++ b/BizHawk.Common/Extensions/IOExtensions.cs @@ -5,6 +5,23 @@ namespace BizHawk.Common.IOExtensions { public static class 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); + } + + return outStream.ToArray(); + } + public static void CopyTo(this Stream src, Stream dest) { int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000; diff --git a/BizHawk.Common/Util.cs b/BizHawk.Common/Util.cs index b091aa6d84..f7aa038fa8 100644 --- a/BizHawk.Common/Util.cs +++ b/BizHawk.Common/Util.cs @@ -354,23 +354,6 @@ namespace BizHawk.Common } } - public static byte[] ReadAllBytes(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); - } - - return outStream.ToArray(); - } - public static byte BinToBCD(this byte v) { return (byte)(((v / 10) * 16) + (v % 10));