Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Use FileStream's ctor instead of File.Open when all access is synchronous #5680

Merged
merged 1 commit into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private bool TryLoadManagedAssemblyMetadata()
try
{
// Try to load the file using the managed metadata reader
using (FileStream assemblyStream = File.OpenRead(_fileName))
using (FileStream assemblyStream = new FileStream(_fileName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 0x1000, useAsync: false))
using (PEReader peReader = new PEReader(assemblyStream))
{
if (peReader.HasMetadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static ZipArchive Open(String archiveFileName, ZipArchiveMode mode)
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] // See comment in the body.
public static ZipArchive Open(String archiveFileName, ZipArchiveMode mode, Encoding entryNameEncoding)
{
// Relies on File.Open for checking of archiveFileName
// Relies on FileStream's ctor for checking of archiveFileName

FileMode fileMode;
FileAccess access;
Expand Down Expand Up @@ -193,7 +193,7 @@ public static ZipArchive Open(String archiveFileName, ZipArchiveMode mode, Encod
// If the ctor completes without throwing, we know fs has been successfully stores in the archive;
// If the ctor throws, we need to close it here.

FileStream fs = File.Open(archiveFileName, fileMode, access, fileShare);
FileStream fs = new FileStream(archiveFileName, fileMode, access, fileShare, bufferSize: 0x1000, useAsync: false);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ internal static ZipArchiveEntry DoCreateEntryFromFile(ZipArchive destination,
// Checking of compressionLevel is passed down to DeflateStream and the IDeflater implementation
// as it is a pugable component that completely encapsulates the meaning of compressionLevel.

// Argument checking gets passed down to File.Open and CreateEntry
// Argument checking gets passed down to FileStream's ctor and CreateEntry
Contract.Ensures(Contract.Result<ZipArchiveEntry>() != null);
Contract.EndContractBlock();

using (Stream fs = File.Open(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (Stream fs = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 0x1000, useAsync: false))
{
ZipArchiveEntry entry = compressionLevel.HasValue
? destination.CreateEntry(entryName, compressionLevel.Value)
Expand Down Expand Up @@ -276,13 +276,13 @@ public static void ExtractToFile(this ZipArchiveEntry source, String destination
if (destinationFileName == null)
throw new ArgumentNullException("destinationFileName");

// Rely on File.Open for further checking destinationFileName parameter
// Rely on FileStream's ctor for further checking destinationFileName parameter

Contract.EndContractBlock();

FileMode fMode = overwrite ? FileMode.Create : FileMode.CreateNew;

using (Stream fs = File.Open(destinationFileName, fMode, FileAccess.Write, FileShare.None))
using (Stream fs = new FileStream(destinationFileName, fMode, FileAccess.Write, FileShare.None, bufferSize: 0x1000, useAsync: false))
{
using (Stream es = source.Open())
es.CopyTo(fs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ internal static UdpGlobalStatisticsTable ParseUdpv6GlobalStatisticsFromSnmp6File

internal static IPInterfaceStatisticsTable ParseInterfaceStatisticsTableFromFile(string filePath, string name)
{
using (StreamReader sr = File.OpenText(filePath))
using (StreamReader sr = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 0x1000, useAsync: false)))
{
sr.ReadLine();
sr.ReadLine();
Expand Down