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) public static NSObject Parse(Stream fs)
{ {
byte[] buf = PropertyListParser.ReadAll(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); return Parse(buf);
} }

View File

@@ -154,7 +154,8 @@ namespace Claunia.PropertyList
{ {
//Read all bytes into a list //Read all bytes into a list
byte[] buf = PropertyListParser.ReadAll(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); return Parse(buf);
} }

View File

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

View File

@@ -65,9 +65,10 @@ namespace Claunia.PropertyList
public NSData(FileInfo file) public NSData(FileInfo file)
{ {
bytes = new byte[(int)file.Length]; bytes = new byte[(int)file.Length];
FileStream raf = file.OpenRead(); using (FileStream raf = file.OpenRead())
{
raf.Read(bytes, 0, (int)file.Length); raf.Read(bytes, 0, (int)file.Length);
raf.Close(); }
} }
/// <summary> /// <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> /// <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) public static NSObject Parse(FileInfo f)
{ {
FileStream fis = f.OpenRead(); int type;
int type = DetermineType(fis);
fis.Close(); using (FileStream fis = f.OpenRead())
{
type = DetermineType(fis);
}
switch (type) switch (type)
{ {
case TYPE_BINARY: case TYPE_BINARY:
@@ -215,9 +219,10 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName; string parent = outFile.DirectoryName;
if (!Directory.Exists(parent)) if (!Directory.Exists(parent))
Directory.CreateDirectory(parent); Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite); using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
SaveAsXml(root, fous); SaveAsXml(root, fous);
fous.Close(); }
} }
/// <summary> /// <summary>
@@ -228,9 +233,18 @@ namespace Claunia.PropertyList
/// <exception cref="IOException">When an error occurs during the writing process.</exception> /// <exception cref="IOException">When an error occurs during the writing process.</exception>
public static void SaveAsXml(NSObject root, Stream outStream) 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); StreamWriter w = new StreamWriter(outStream, Encoding.UTF8);
w.Write(root.ToXmlPropertyList()); w.Write(root.ToXmlPropertyList());
w.Close(); w.Close();
#else
using (StreamWriter w = new StreamWriter(outStream, Encoding.UTF8, bufferSize: 1024, leaveOpen: true))
{
w.Write(root.ToXmlPropertyList());
}
#endif
} }
/// <summary> /// <summary>
@@ -291,11 +305,11 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName; string parent = outFile.DirectoryName;
if (!Directory.Exists(parent)) if (!Directory.Exists(parent))
Directory.CreateDirectory(parent); Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite); using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
StreamWriter w = new StreamWriter(fous, Encoding.ASCII); using (StreamWriter w = new StreamWriter(fous, Encoding.ASCII))
{
w.Write(root.ToASCIIPropertyList()); w.Write(root.ToASCIIPropertyList());
w.Close(); }
fous.Close();
} }
/// <summary> /// <summary>
@@ -309,11 +323,11 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName; string parent = outFile.DirectoryName;
if (!Directory.Exists(parent)) if (!Directory.Exists(parent))
Directory.CreateDirectory(parent); Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite); using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
StreamWriter w = new StreamWriter(fous, Encoding.ASCII); using (StreamWriter w = new StreamWriter(fous, Encoding.ASCII))
{
w.Write(root.ToASCIIPropertyList()); w.Write(root.ToASCIIPropertyList());
w.Close(); }
fous.Close();
} }
/// <summary> /// <summary>
@@ -350,11 +364,11 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName; string parent = outFile.DirectoryName;
if (!Directory.Exists(parent)) if (!Directory.Exists(parent))
Directory.CreateDirectory(parent); Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite); using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
StreamWriter w = new StreamWriter(fous, Encoding.ASCII); using (StreamWriter w = new StreamWriter(fous, Encoding.ASCII))
{
w.Write(root.ToGnuStepASCIIPropertyList()); w.Write(root.ToGnuStepASCIIPropertyList());
w.Close(); }
fous.Close();
} }
/// <summary> /// <summary>
@@ -368,11 +382,11 @@ namespace Claunia.PropertyList
string parent = outFile.DirectoryName; string parent = outFile.DirectoryName;
if (!Directory.Exists(parent)) if (!Directory.Exists(parent))
Directory.CreateDirectory(parent); Directory.CreateDirectory(parent);
Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite); using (Stream fous = outFile.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite))
StreamWriter w = new StreamWriter(fous, Encoding.ASCII); using (StreamWriter w = new StreamWriter(fous, Encoding.ASCII))
{
w.Write(root.ToGnuStepASCIIPropertyList()); w.Write(root.ToGnuStepASCIIPropertyList());
w.Close(); }
fous.Close();
} }
/// <summary> /// <summary>

View File

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