SanAndreasUnity/Assets/Scripts/Utilities/Tools.cs

36 lines
923 B
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using System;
using System.IO;
using System.Text;
namespace SanAndreasUnity.Utilities
{
public static class Tools
{
public static byte[] ReadBytes(this Stream self, int count)
{
var data = new byte[count];
for (var i = 0; i < count; ++i)
{
var bt = self.ReadByte();
if (bt == -1) throw new EndOfStreamException();
data[i] = (byte)bt;
}
return data;
}
public static String ReadString(this BinaryReader reader, int length)
{
var bytes = reader.ReadBytes(length);
return Encoding.UTF8.GetString(bytes).TrimNullChars();
}
public static String TrimNullChars(this String str)
{
for (var i = 0; i < str.Length; ++i) if (str[i] == '\0') return str.Substring(0, i);
return str;
}
}
}