Enclose PEC parsing with using() to ensure file is closed

Branch_2.1.x
Nathan Crawford 2016-03-22 18:49:30 -04:00
rodzic 02ed3d8794
commit 5aef99f6e5
1 zmienionych plików z 194 dodań i 195 usunięć

Wyświetl plik

@ -32,7 +32,6 @@ namespace PesFile
public class PesFile
{
System.IO.BinaryReader fileIn;
int imageWidth;
int imageHeight;
string _filename;
@ -112,8 +111,12 @@ namespace PesFile
private void OpenFile(string filename)
{
_filename = filename;
fileIn = new System.IO.BinaryReader(System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read));
// The using statements ensure fileIn is closed, no matter how the statement is exited.
using (System.IO.FileStream fileStreamIn = System.IO.File.Open(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
using (System.IO.BinaryReader fileIn = new System.IO.BinaryReader(fileStreamIn))
{
string startFileSig = "";
for (int i = 0; i < 4; i++) // 4 bytes
{
@ -124,7 +127,6 @@ namespace PesFile
// This is not a file that we can read
readyStatus = statusEnum.ParseError;
lastError = "Missing #PES at beginning of file";
fileIn.Close();
return;
}
@ -134,29 +136,27 @@ namespace PesFile
{
versionString += fileIn.ReadChar();
}
if(!UInt16.TryParse(versionString, out pesVersion))
if (!UInt16.TryParse(versionString, out pesVersion))
{
// This is not a file that we can read
readyStatus = statusEnum.ParseError;
lastError = "PES version is not the correct format";
fileIn.Close();
return;
}
int pecstart = fileIn.ReadInt32();
// Sanity check on PEC start position
if(fileIn.BaseStream.Length < (pecstart + 532))
if (fileIn.BaseStream.Length < (pecstart + 532))
{
// This file is probably truncated
readyStatus = statusEnum.ParseError;
lastError = "File appears to be truncated (PEC section is beyond end of file)";
fileIn.Close();
return;
}
// Read number of colors in this design
fileIn.BaseStream.Position = pecstart + 48;
int numColors = fileIn.ReadByte() +1;
int numColors = fileIn.ReadByte() + 1;
List<byte> colorList = new List<byte>();
for (int x = 0; x < numColors; x++)
{
@ -319,9 +319,8 @@ namespace PesFile
translateStart.X = -minX;
translateStart.Y = -minY;
readyStatus = statusEnum.Ready;
// Close the file
fileIn.Close();
}
}
}
public int GetWidth()