Separate stream and writer extensions to an IOExtensions object and namespace

This commit is contained in:
adelikat 2014-07-03 17:13:09 +00:00
parent 204e331ea2
commit 47514784eb
7 changed files with 125 additions and 99 deletions

View File

@ -2,6 +2,7 @@
using System.IO;
using BizHawk.Common;
using BizHawk.Common.IOExtensions;
namespace BizHawk.Client.Common
{

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.IO;
using BizHawk.Common;
using BizHawk.Common.IOExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk

View File

@ -54,6 +54,7 @@
<Compile Include="CustomCollections.cs" />
<Compile Include="EnumHelper.cs" />
<Compile Include="Extensions\Extensions.cs" />
<Compile Include="Extensions\IOExtensions.cs" />
<Compile Include="Extensions\NumberExtensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="HawkFile.cs" />

View File

@ -107,38 +107,6 @@ namespace BizHawk.Common
throw new InvalidOperationException("Item not found");
}
public static void CopyTo(this Stream src, Stream dest)
{
int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000;
byte[] buffer = new byte[size];
int n;
do
{
n = src.Read(buffer, 0, buffer.Length);
dest.Write(buffer, 0, n);
} while (n != 0);
}
public static void CopyTo(this MemoryStream src, Stream dest)
{
dest.Write(src.GetBuffer(), (int)src.Position, (int)(src.Length - src.Position));
}
public static void CopyTo(this Stream src, MemoryStream dest)
{
if (src.CanSeek)
{
int pos = (int)dest.Position;
int length = (int)(src.Length - src.Position) + pos;
dest.SetLength(length);
while (pos < length)
pos += src.Read(dest.GetBuffer(), pos, length - pos);
}
else
src.CopyTo(dest);
}
public static bool Bit(this byte b, int index)
{
return (b & (1 << index)) != 0;
@ -235,68 +203,6 @@ namespace BizHawk.Common
writer.WriteLine();
}
public static void Write(this BinaryWriter bw, int[] buffer)
{
foreach (int b in buffer)
{
bw.Write(b);
}
}
public static void Write(this BinaryWriter bw, uint[] buffer)
{
foreach (uint b in buffer)
{
bw.Write(b);
}
}
public static void Write(this BinaryWriter bw, short[] buffer)
{
foreach (short b in buffer)
{
bw.Write(b);
}
}
public static void Write(this BinaryWriter bw, ushort[] buffer)
{
foreach (ushort t in buffer)
{
bw.Write(t);
}
}
public static int[] ReadInt32s(this BinaryReader br, int num)
{
int[] ret = new int[num];
for (int i = 0; i < num; i++)
{
ret[i] = br.ReadInt32();
}
return ret;
}
public static short[] ReadInt16s(this BinaryReader br, int num)
{
short[] ret = new short[num];
for (int i = 0; i < num; i++)
{
ret[i] = br.ReadInt16();
}
return ret;
}
public static ushort[] ReadUInt16s(this BinaryReader br, int num)
{
ushort[] ret = new ushort[num];
for (int i = 0; i < num; i++)
{
ret[i] = br.ReadUInt16();
}
return ret;
}
public static void ReadFromHex(this byte[] buffer, string hex)
{
if (hex.Length%2 != 0)
@ -385,9 +291,5 @@ namespace BizHawk.Common
}
#endregion
//these don't work??? they dont get chosen by compiler
public static void WriteBit(this BinaryWriter bw, Bit bit) { bw.Write((bool)bit); }
public static Bit ReadBit(this BinaryReader br) { return br.ReadBoolean(); }
}
}

View File

@ -0,0 +1,119 @@
using System;
using System.IO;
namespace BizHawk.Common.IOExtensions
{
public static class IOExtensions
{
public static void CopyTo(this Stream src, Stream dest)
{
int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000;
byte[] buffer = new byte[size];
int n;
do
{
n = src.Read(buffer, 0, buffer.Length);
dest.Write(buffer, 0, n);
} while (n != 0);
}
public static void CopyTo(this MemoryStream src, Stream dest)
{
dest.Write(src.GetBuffer(), (int)src.Position, (int)(src.Length - src.Position));
}
public static void CopyTo(this Stream src, MemoryStream dest)
{
if (src.CanSeek)
{
int pos = (int)dest.Position;
int length = (int)(src.Length - src.Position) + pos;
dest.SetLength(length);
while (pos < length)
{
pos += src.Read(dest.GetBuffer(), pos, length - pos);
}
}
else
{
src.CopyTo(dest);
}
}
public static void Write(this BinaryWriter bw, int[] buffer)
{
foreach (int b in buffer)
{
bw.Write(b);
}
}
public static void Write(this BinaryWriter bw, uint[] buffer)
{
foreach (uint b in buffer)
{
bw.Write(b);
}
}
public static void Write(this BinaryWriter bw, short[] buffer)
{
foreach (short b in buffer)
{
bw.Write(b);
}
}
public static void Write(this BinaryWriter bw, ushort[] buffer)
{
foreach (ushort t in buffer)
{
bw.Write(t);
}
}
public static int[] ReadInt32s(this BinaryReader br, int num)
{
int[] ret = new int[num];
for (int i = 0; i < num; i++)
{
ret[i] = br.ReadInt32();
}
return ret;
}
public static short[] ReadInt16s(this BinaryReader br, int num)
{
short[] ret = new short[num];
for (int i = 0; i < num; i++)
{
ret[i] = br.ReadInt16();
}
return ret;
}
public static ushort[] ReadUInt16s(this BinaryReader br, int num)
{
ushort[] ret = new ushort[num];
for (int i = 0; i < num; i++)
{
ret[i] = br.ReadUInt16();
}
return ret;
}
public static void WriteBit(this BinaryWriter bw, Bit bit)
{
bw.Write((bool)bit);
}
public static Bit ReadBit(this BinaryReader br)
{
return br.ReadBoolean();
}
}
}

View File

@ -3,6 +3,8 @@ using System.Collections.Generic;
using System.Globalization;
using System.IO;
using BizHawk.Common.IOExtensions;
namespace BizHawk.Common
{
public unsafe class Serializer

View File

@ -3,6 +3,7 @@ using System.IO;
using System.Globalization;
using BizHawk.Common;
using BizHawk.Common.IOExtensions;
using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Sega.Genesis