Use the stream extensions instead of the BinaryReader/Writer

This commit is contained in:
Pavlos Touboulidis
2014-05-20 17:40:17 +03:00
parent 39cc4cc014
commit fe1eb1f3e0
2 changed files with 52 additions and 46 deletions

View File

@@ -64,6 +64,11 @@ namespace OpenRA
return BitConverter.ToInt32(s.ReadBytes(4), 0);
}
public static void Write(this Stream s, int value)
{
s.Write(BitConverter.GetBytes(value));
}
public static float ReadFloat(this Stream s)
{
return BitConverter.ToSingle(s.ReadBytes(4), 0);
@@ -134,5 +139,32 @@ namespace OpenRA
}
}
}
// The string is assumed to be length-prefixed, as written by WriteString()
public static string ReadString(this Stream s, Encoding encoding, int maxLength)
{
var length = s.ReadInt32();
if (length > maxLength)
throw new InvalidOperationException("The length of the string ({0}) is longer than the maximum allowed ({1}).".F(length, maxLength));
return encoding.GetString(s.ReadBytes(length));
}
// Writes a length-prefixed string using the specified encoding and returns
// the number of bytes written.
public static int WriteString(this Stream s, Encoding encoding, string text)
{
byte[] bytes;
if (!string.IsNullOrEmpty(text))
bytes = encoding.GetBytes(text);
else
bytes = new byte[0];
s.Write(bytes.Length);
s.Write(bytes);
return 4 + bytes.Length;
}
}
}