diff --git a/src/Microsoft.Deployment.DotNet.Releases/src/Product.cs b/src/Microsoft.Deployment.DotNet.Releases/src/Product.cs index 5a2aced640..f05bec8353 100644 --- a/src/Microsoft.Deployment.DotNet.Releases/src/Product.cs +++ b/src/Microsoft.Deployment.DotNet.Releases/src/Product.cs @@ -182,9 +182,9 @@ public async Task> GetReleasesAsync(string pa { await Utils.GetLatestFileAsync(path, downloadLatest, ReleasesJson).ConfigureAwait(false); - using TextReader reader = File.OpenText(path); + using FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true); - return await GetReleasesAsync(reader, this).ConfigureAwait(false); + return await GetReleasesAsync(stream, this).ConfigureAwait(false); } /// @@ -201,9 +201,8 @@ public async Task> GetReleasesAsync(Uri addre } using var stream = new MemoryStream(await Utils.s_httpClient.GetByteArrayAsync(address).ConfigureAwait(false)); - using var reader = new StreamReader(stream); - return await GetReleasesAsync(reader, this).ConfigureAwait(false); + return await GetReleasesAsync(stream, this).ConfigureAwait(false); } /// @@ -224,19 +223,19 @@ public bool IsOutOfSupport() /// A collection of releases. The releases are not linked to a specific . public static async Task> GetReleasesAsync(string path) { - using TextReader reader = File.OpenText(path); + using FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true); - return await GetReleasesAsync(reader, null).ConfigureAwait(false); + return await GetReleasesAsync(stream, null).ConfigureAwait(false); } - private static async Task> GetReleasesAsync(TextReader reader, Product product) + private static async Task> GetReleasesAsync(Stream stream, Product product) { - if (reader == null) + if (stream == null) { - throw new ArgumentNullException(nameof(reader)); + throw new ArgumentNullException(nameof(stream)); } - using var releasesDocument = JsonDocument.Parse(await reader.ReadToEndAsync().ConfigureAwait(false)); + using var releasesDocument = await JsonDocument.ParseAsync(stream).ConfigureAwait(false); JsonElement root = releasesDocument.RootElement; var releases = new List(); var enumerator = root.GetProperty("releases").EnumerateArray(); diff --git a/src/Microsoft.Deployment.DotNet.Releases/src/ProductCollection.cs b/src/Microsoft.Deployment.DotNet.Releases/src/ProductCollection.cs index aea4d2e62e..19e36c01bd 100644 --- a/src/Microsoft.Deployment.DotNet.Releases/src/ProductCollection.cs +++ b/src/Microsoft.Deployment.DotNet.Releases/src/ProductCollection.cs @@ -79,9 +79,8 @@ public static async Task GetAsync(Uri releasesIndexUrl) } using var stream = new MemoryStream(await Utils.s_httpClient.GetByteArrayAsync(releasesIndexUrl).ConfigureAwait(false)); - using var reader = new StreamReader(stream); - return await GetAsync(reader).ConfigureAwait(false); + return await GetAsync(stream).ConfigureAwait(false); } /// @@ -101,19 +100,19 @@ public static async Task GetFromFileAsync(string path, bool d { await Utils.GetLatestFileAsync(path, downloadLatest, ReleasesIndexDefaultUrl).ConfigureAwait(false); - using TextReader reader = File.OpenText(path); + using FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true); - return await GetAsync(reader).ConfigureAwait(false); + return await GetAsync(stream).ConfigureAwait(false); } - private static async Task GetAsync(TextReader reader) + private static async Task GetAsync(Stream stream) { - if (reader == null) + if (stream == null) { - throw new ArgumentNullException(nameof(reader)); + throw new ArgumentNullException(nameof(stream)); } - using var releasesIndexDocument = JsonDocument.Parse(await reader.ReadToEndAsync().ConfigureAwait(false)); + using var releasesIndexDocument = await JsonDocument.ParseAsync(stream).ConfigureAwait(false); var root = releasesIndexDocument.RootElement.GetProperty("releases-index"); var products = new List(); diff --git a/src/Microsoft.Deployment.DotNet.Releases/src/ReleaseFile.cs b/src/Microsoft.Deployment.DotNet.Releases/src/ReleaseFile.cs index 68d7ae7285..cbfd4c845b 100644 --- a/src/Microsoft.Deployment.DotNet.Releases/src/ReleaseFile.cs +++ b/src/Microsoft.Deployment.DotNet.Releases/src/ReleaseFile.cs @@ -16,13 +16,20 @@ public class ReleaseFile : IEquatable { private static readonly SHA512 s_defaultHashAlgorithm = SHA512.Create(); + private Uri _address; + private string _addressString; + /// /// The URL from where to download the file. /// public Uri Address { - get; - private set; + get => _address ??= _addressString != null ? new Uri(_addressString) : null; + private set + { + _address = value; + _addressString = value?.OriginalString; + } } /// @@ -63,7 +70,7 @@ public string Rid /// The to deserialize. internal ReleaseFile(JsonElement fileElement) { - Address = fileElement.GetUriOrDefault("url"); + _addressString = fileElement.GetStringOrDefault("url"); Hash = fileElement.GetStringOrDefault("hash"); Name = fileElement.GetStringOrDefault("name"); Rid = fileElement.GetStringOrDefault("rid"); @@ -149,7 +156,7 @@ public bool Equals(ReleaseFile other) Name == other.Name && Rid == other.Rid && Hash == other.Hash && - Address == other.Address; + _addressString == other._addressString; } /// @@ -157,7 +164,7 @@ public bool Equals(ReleaseFile other) /// /// A hash code for the current object. public override int GetHashCode() => - Hash.GetHashCode() ^ Name.GetHashCode() ^ Rid.GetHashCode() ^ Address.GetHashCode(); + Hash.GetHashCode(); internal static ReleaseFile Create(string hash, string name, string rid, string address) => new ReleaseFile(new Uri(address), hash, name, rid);