Skip to content
Open
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
19 changes: 9 additions & 10 deletions src/Microsoft.Deployment.DotNet.Releases/src/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ public async Task<ReadOnlyCollection<ProductRelease>> 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);
}

/// <summary>
Expand All @@ -201,9 +201,8 @@ public async Task<ReadOnlyCollection<ProductRelease>> 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);
}

/// <summary>
Expand All @@ -224,19 +223,19 @@ public bool IsOutOfSupport()
/// <returns>A collection of releases. The releases are not linked to a specific <see cref="Product"/>.</returns>
public static async Task<ReadOnlyCollection<ProductRelease>> 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<ReadOnlyCollection<ProductRelease>> GetReleasesAsync(TextReader reader, Product product)
private static async Task<ReadOnlyCollection<ProductRelease>> 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<ProductRelease>();
var enumerator = root.GetProperty("releases").EnumerateArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ public static async Task<ProductCollection> 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);
}

/// <summary>
Expand All @@ -101,19 +100,19 @@ public static async Task<ProductCollection> 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<ProductCollection> GetAsync(TextReader reader)
private static async Task<ProductCollection> 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<Product>();

Expand Down
17 changes: 12 additions & 5 deletions src/Microsoft.Deployment.DotNet.Releases/src/ReleaseFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ public class ReleaseFile : IEquatable<ReleaseFile>
{
private static readonly SHA512 s_defaultHashAlgorithm = SHA512.Create();

private Uri _address;
private string _addressString;

/// <summary>
/// The URL from where to download the file.
/// </summary>
public Uri Address
{
get;
private set;
get => _address ??= _addressString != null ? new Uri(_addressString) : null;
private set
{
_address = value;
_addressString = value?.OriginalString;
}
}

/// <summary>
Expand Down Expand Up @@ -63,7 +70,7 @@ public string Rid
/// <param name="fileElement">The <see cref="JsonElement"/> to deserialize.</param>
internal ReleaseFile(JsonElement fileElement)
{
Address = fileElement.GetUriOrDefault("url");
_addressString = fileElement.GetStringOrDefault("url");
Hash = fileElement.GetStringOrDefault("hash");
Name = fileElement.GetStringOrDefault("name");
Rid = fileElement.GetStringOrDefault("rid");
Expand Down Expand Up @@ -149,15 +156,15 @@ public bool Equals(ReleaseFile other)
Name == other.Name &&
Rid == other.Rid &&
Hash == other.Hash &&
Address == other.Address;
_addressString == other._addressString;
}

/// <summary>
/// Returns the hash code for this release file.
/// </summary>
/// <returns>A hash code for the current object.</returns>
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);
Expand Down