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

@@ -33,21 +33,21 @@ namespace OpenRA.FileFormats
GameInfo = info;
}
ReplayMetadata(BinaryReader reader, string path)
ReplayMetadata(FileStream fs, string path)
{
FilePath = path;
// Read start marker
if (reader.ReadInt32() != MetaStartMarker)
if (fs.ReadInt32() != MetaStartMarker)
throw new InvalidOperationException("Expected MetaStartMarker but found an invalid value.");
// Read version
var version = reader.ReadInt32();
var version = fs.ReadInt32();
if (version != MetaVersion)
throw new NotSupportedException("Metadata version {0} is not supported".F(version));
// Read game info (max 100K limit as a safeguard against corrupted files)
string data = ReadUtf8String(reader, 1024 * 100);
string data = fs.ReadString(Encoding.UTF8, 1024 * 100);
GameInfo = GameInformation.Deserialize(data);
}
@@ -61,7 +61,8 @@ namespace OpenRA.FileFormats
int dataLength = 0;
{
// Write lobby info data
dataLength += WriteUtf8String(writer, GameInfo.Serialize());
writer.Flush();
dataLength += writer.BaseStream.WriteString(Encoding.UTF8, GameInfo.Serialize());
}
// Write total length & end marker
@@ -93,25 +94,22 @@ namespace OpenRA.FileFormats
try
{
fs.Seek(-(4 + 4), SeekOrigin.End);
using (var reader = new BinaryReader(fs))
var dataLength = fs.ReadInt32();
if (fs.ReadInt32() == MetaEndMarker)
{
var dataLength = reader.ReadInt32();
if (reader.ReadInt32() == MetaEndMarker)
// go back by (end marker + length storage + data + version + start marker) bytes
fs.Seek(-(4 + 4 + dataLength + 4 + 4), SeekOrigin.Current);
try
{
// go back by (end marker + length storage + data + version + start marker) bytes
fs.Seek(-(4 + 4 + dataLength + 4 + 4), SeekOrigin.Current);
try
{
return new ReplayMetadata(reader, path);
}
catch (InvalidOperationException ex)
{
Log.Write("debug", ex.ToString());
}
catch (NotSupportedException ex)
{
Log.Write("debug", ex.ToString());
}
return new ReplayMetadata(fs, path);
}
catch (InvalidOperationException ex)
{
Log.Write("debug", ex.ToString());
}
catch (NotSupportedException ex)
{
Log.Write("debug", ex.ToString());
}
}
}
@@ -122,29 +120,5 @@ namespace OpenRA.FileFormats
return null;
}
static int WriteUtf8String(BinaryWriter writer, string text)
{
byte[] bytes;
if (!string.IsNullOrEmpty(text))
bytes = Encoding.UTF8.GetBytes(text);
else
bytes = new byte[0];
writer.Write(bytes.Length);
writer.Write(bytes);
return 4 + bytes.Length;
}
static string ReadUtf8String(BinaryReader reader, int maxLength)
{
var length = reader.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.UTF8.GetString(reader.ReadBytes(length));
}
}
}

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;
}
}
}