Merge pull request #2007 from jvitkauskas/disposeStreams

Dispose MemoryStream objects after using them
This commit is contained in:
Jaex 2016-10-16 21:05:08 +03:00 committed by GitHub
commit d3af917bf0

View file

@ -88,14 +88,15 @@ public static string BinaryToText(string binary)
{
binary = Regex.Replace(binary, @"[^01]", "");
MemoryStream stream = new MemoryStream();
for (int i = 0; i + 8 <= binary.Length; i += 8)
using (MemoryStream stream = new MemoryStream())
{
stream.WriteByte(BinaryToByte(binary.Substring(i, 8)));
}
for (int i = 0; i + 8 <= binary.Length; i += 8)
{
stream.WriteByte(BinaryToByte(binary.Substring(i, 8)));
}
return Encoding.UTF8.GetString(stream.ToArray());
return Encoding.UTF8.GetString(stream.ToArray());
}
}
#endregion Binary to ...
@ -149,14 +150,15 @@ public static string HexadecimalToText(string hex)
{
hex = Regex.Replace(hex, @"[^0-9a-fA-F]", "");
MemoryStream stream = new MemoryStream();
for (int i = 0; i + 2 <= hex.Length; i += 2)
using (MemoryStream stream = new MemoryStream())
{
stream.WriteByte(HexadecimalToByte(hex.Substring(i, 2)));
}
for (int i = 0; i + 2 <= hex.Length; i += 2)
{
stream.WriteByte(HexadecimalToByte(hex.Substring(i, 2)));
}
return Encoding.UTF8.GetString(stream.ToArray());
return Encoding.UTF8.GetString(stream.ToArray());
}
}
#endregion Hexadecimal to ...
@ -177,18 +179,19 @@ public static string ASCIIToText(string ascii)
{
string[] numbers = Regex.Split(ascii, @"\D+");
MemoryStream stream = new MemoryStream();
foreach (string number in numbers)
using (MemoryStream stream = new MemoryStream())
{
byte b;
if (byte.TryParse(number, out b))
foreach (string number in numbers)
{
stream.WriteByte(b);
byte b;
if (byte.TryParse(number, out b))
{
stream.WriteByte(b);
}
}
}
return Encoding.ASCII.GetString(stream.ToArray());
return Encoding.ASCII.GetString(stream.ToArray());
}
}
#endregion ASCII to ...