Use using constructs on disposable io objects in LLRaw to ensure they are always closed even if an exception is thrown.

inv-download
Justin Clark-Casey (justincc) 2015-02-28 00:41:11 +00:00
parent 06a52b43df
commit 4717132b82
1 changed files with 111 additions and 113 deletions

View File

@ -74,12 +74,13 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
public ITerrainChannel LoadFile(string filename)
{
FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Open, FileAccess.Read);
ITerrainChannel retval = LoadStream(s);
s.Close();
ITerrainChannel channel;
return retval;
using (FileStream s = file.Open(FileMode.Open, FileAccess.Read))
channel = LoadStream(s);
return channel;
}
public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight)
@ -87,9 +88,10 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight);
FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.Open, FileAccess.Read);
BinaryReader bs = new BinaryReader(s);
using (FileStream s = file.Open(FileMode.Open, FileAccess.Read))
using (BinaryReader bs = new BinaryReader(s))
{
int currFileYOffset = fileHeight - 1;
// if our region isn't on the first Y section of the areas to be landscaped, then
@ -105,6 +107,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
// got to the Y start offset within the file of our region
// so read the file bits associated with our region
int y;
// for each Y within our Y offset
for (y = sectionHeight - 1; y >= 0; y--)
{
@ -139,9 +142,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
currFileXOffset++;
}
}
bs.Close();
s.Close();
}
return retval;
}
@ -158,7 +159,8 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
TerrainChannel retval = new TerrainChannel(trimmedDimension, trimmedDimension);
BinaryReader bs = new BinaryReader(s);
using (BinaryReader bs = new BinaryReader(s))
{
int y;
for (y = 0; y < retval.Height; y++)
{
@ -169,8 +171,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
bs.ReadBytes(11); // Advance the stream to next bytes.
}
}
bs.Close();
}
return retval;
}
@ -178,16 +179,15 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
public void SaveFile(string filename, ITerrainChannel map)
{
FileInfo file = new FileInfo(filename);
FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write);
SaveStream(s, map);
s.Close();
using (FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write))
SaveStream(s, map);
}
public void SaveStream(Stream s, ITerrainChannel map)
{
BinaryWriter binStream = new BinaryWriter(s);
using (BinaryWriter binStream = new BinaryWriter(s))
{
// Output the calculated raw
for (int y = 0; y < map.Height; y++)
{
@ -240,8 +240,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
binStream.Write(alpha10);
}
}
binStream.Close();
}
}
public string FileExtension
@ -269,6 +268,5 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders
{
return false;
}
}
}