Use using statements around Streams and Readers/Writers instead of manually calling .Close, which is no longer supported on .NET Core

This commit is contained in:
Frederik Carlier
2016-05-22 13:25:19 +02:00
parent accdc880ae
commit 0a7483ff75
6 changed files with 59 additions and 35 deletions

View File

@@ -79,7 +79,8 @@ namespace Claunia.PropertyList
public static NSObject Parse(Stream fs)
{
byte[] buf = PropertyListParser.ReadAll(fs);
fs.Close();
// Don't close the stream - that would be the responisibility of code that class
// Parse
return Parse(buf);
}

View File

@@ -154,7 +154,8 @@ namespace Claunia.PropertyList
{
//Read all bytes into a list
byte[] buf = PropertyListParser.ReadAll(fs);
fs.Close();
// Don't close the stream - that would be the responisibility of code that class
// Parse
return Parse(buf);
}

View File

@@ -117,9 +117,10 @@ namespace Claunia.PropertyList
/// <exception cref="IOException"></exception>
public static void Write(FileInfo file, NSObject root)
{
FileStream fous = file.OpenWrite();
Write(fous, root);
fous.Close();
using (FileStream fous = file.OpenWrite())
{
Write(fous, root);
}
}
/// <summary>

View File

@@ -65,9 +65,10 @@ namespace Claunia.PropertyList
public NSData(FileInfo file)
{
bytes = new byte[(int)file.Length];
FileStream raf = file.OpenRead();
raf.Read(bytes, 0, (int)file.Length);
raf.Close();
using (FileStream raf = file.OpenRead())
{
raf.Read(bytes, 0, (int)file.Length);
}
}
/// <summary>

View File

@@ -158,9 +158,13 @@ namespace Claunia.PropertyList
/// <returns>The root object in the property list. This is usually a NSDictionary but can also be a NSArray.</returns>
public static NSObject Parse(FileInfo f)
{
FileStream fis = f.OpenRead();
int type = DetermineType(fis);
fis.Close();
int type;
using (FileStream fis = f.OpenRead())
{
type = DetermineType(fis);
}
switch (type)
{
case TYPE_BINARY:
@@ -215,9 +219,10 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName;
if (!Directory.Exists(parent))
Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
SaveAsXml(root, fous);
fous.Close();
using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
SaveAsXml(root, fous);
}
}
/// <summary>
@@ -228,9 +233,18 @@ namespace Claunia.PropertyList
/// <exception cref="IOException">When an error occurs during the writing process.</exception>
public static void SaveAsXml(NSObject root, Stream outStream)
{
#if NET40
// The StreamWriter constructor which takes a "leaveOpen" parameter is
// not available on .NET 4.0
StreamWriter w = new StreamWriter(outStream, Encoding.UTF8);
w.Write(root.ToXmlPropertyList());
w.Close();
#else
using (StreamWriter w = new StreamWriter(outStream, Encoding.UTF8, bufferSize: 1024, leaveOpen: true))
{
w.Write(root.ToXmlPropertyList());
}
#endif
}
/// <summary>
@@ -291,11 +305,11 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName;
if (!Directory.Exists(parent))
Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter w = new StreamWriter(fous, Encoding.ASCII);
w.Write(root.ToASCIIPropertyList());
w.Close();
fous.Close();
using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter w = new StreamWriter(fous, Encoding.ASCII))
{
w.Write(root.ToASCIIPropertyList());
}
}
/// <summary>
@@ -309,11 +323,11 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName;
if (!Directory.Exists(parent))
Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter w = new StreamWriter(fous, Encoding.ASCII);
w.Write(root.ToASCIIPropertyList());
w.Close();
fous.Close();
using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter w = new StreamWriter(fous, Encoding.ASCII))
{
w.Write(root.ToASCIIPropertyList());
}
}
/// <summary>
@@ -350,11 +364,11 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName;
if (!Directory.Exists(parent))
Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter w = new StreamWriter(fous, Encoding.ASCII);
w.Write(root.ToGnuStepASCIIPropertyList());
w.Close();
fous.Close();
using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter w = new StreamWriter(fous, Encoding.ASCII))
{
w.Write(root.ToGnuStepASCIIPropertyList());
}
}
/// <summary>
@@ -368,11 +382,11 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName;
if (!Directory.Exists(parent))
Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter w = new StreamWriter(fous, Encoding.ASCII);
w.Write(root.ToGnuStepASCIIPropertyList());
w.Close();
fous.Close();
using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter w = new StreamWriter(fous, Encoding.ASCII))
{
w.Write(root.ToGnuStepASCIIPropertyList());
}
}
/// <summary>

View File

@@ -8,16 +8,19 @@
<RootNamespace>Claunia.PropertyList</RootNamespace>
<AssemblyName>plist-cil</AssemblyName>
<ReleaseVersion>1.14</ReleaseVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<DefineConstants>DEBUG;NET40</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
@@ -37,6 +40,8 @@
<Command type="AfterBuild" command="nuget pack plist-cil.nuspec -Verbosity Detailed" workingdir="${ProjectDir}" />
</CustomCommands>
</CustomCommands>
<Prefer32Bit>false</Prefer32Bit>
<DefineConstants>NET40</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
@@ -55,6 +60,7 @@
<Compile Include="NSNumber.cs" />
<Compile Include="NSString.cs" />
<Compile Include="NSSet.cs" />
<Compile Include="PropertyListException.cs" />
<Compile Include="UID.cs" />
<Compile Include="NSDate.cs" />
<Compile Include="NSArray.cs" />