Skip to content

Commit

Permalink
use file-scoped namespace declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
smdn committed Dec 11, 2021
1 parent d83df4d commit 2542eca
Show file tree
Hide file tree
Showing 166 changed files with 14,015 additions and 14,010 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggesti
[*.cs]
csharp_using_directive_placement = outside_namespace

csharp_style_namespace_declarations = file_scoped:suggestion

csharp_prefer_braces = when_multiline
csharp_prefer_simple_default_expression = true:suggestion
csharp_prefer_simple_using_statement = true:warning
Expand Down
3 changes: 3 additions & 0 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ dotnet_diagnostic.IDE0130.severity = none

dotnet_diagnostic.CA1707.severity = none
dotnet_diagnostic.CA1805.severity = suggestion

csharp_style_namespace_declarations = file_scoped:warning
dotnet_diagnostic.IDE0161.severity = warning
232 changes: 116 additions & 116 deletions src/Smdn.Core.Miscellaneous/Smdn.IO/CachedWebFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,153 +5,153 @@
using System.Net;
using System.Text;

namespace Smdn.IO {
public class CachedWebFile {
public Uri FileUri {
get; private set;
}
namespace Smdn.IO;

public string CachePath {
get; private set;
}
public class CachedWebFile {
public Uri FileUri {
get; private set;
}

public TimeSpan ExpirationInterval {
get; set;
}
public string CachePath {
get; private set;
}

public CachedWebFile(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
if (fileUri == null)
throw new ArgumentNullException(nameof(fileUri));
if (cachePath == null)
throw new ArgumentNullException(nameof(cachePath));
if (expirationInterval < TimeSpan.Zero)
throw ExceptionUtils.CreateArgumentMustBeZeroOrPositive(nameof(expirationInterval), expirationInterval);

this.FileUri = fileUri;
this.CachePath = cachePath;
this.ExpirationInterval = expirationInterval;
}
public TimeSpan ExpirationInterval {
get; set;
}

private void EnsureFileExists()
{
var cacheExists = File.Exists(CachePath);
public CachedWebFile(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
if (fileUri == null)
throw new ArgumentNullException(nameof(fileUri));
if (cachePath == null)
throw new ArgumentNullException(nameof(cachePath));
if (expirationInterval < TimeSpan.Zero)
throw ExceptionUtils.CreateArgumentMustBeZeroOrPositive(nameof(expirationInterval), expirationInterval);

this.FileUri = fileUri;
this.CachePath = cachePath;
this.ExpirationInterval = expirationInterval;
}

if (!cacheExists || (ExpirationInterval <= (DateTime.Now - File.GetLastWriteTime(CachePath)))) {
var dir = Path.GetDirectoryName(CachePath);
private void EnsureFileExists()
{
var cacheExists = File.Exists(CachePath);

if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
if (!cacheExists || (ExpirationInterval <= (DateTime.Now - File.GetLastWriteTime(CachePath)))) {
var dir = Path.GetDirectoryName(CachePath);

using (var client = new WebClient()) {
try {
if (FileUri.Scheme != Uri.UriSchemeHttp)
// ftp or else
client.Credentials = new NetworkCredential("anonymous", string.Empty);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);

File.WriteAllBytes(CachePath, client.DownloadData(FileUri));
}
catch {
if (!cacheExists)
throw;
}
using (var client = new WebClient()) {
try {
if (FileUri.Scheme != Uri.UriSchemeHttp)
// ftp or else
client.Credentials = new NetworkCredential("anonymous", string.Empty);

File.WriteAllBytes(CachePath, client.DownloadData(FileUri));
}
catch {
if (!cacheExists)
throw;
}
}
}
}

public static void EnsureFileExists(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
(new CachedWebFile(fileUri, cachePath, expirationInterval)).EnsureFileExists();
}
public static void EnsureFileExists(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
(new CachedWebFile(fileUri, cachePath, expirationInterval)).EnsureFileExists();
}

public static Stream OpenRead(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).OpenRead();
}
public static Stream OpenRead(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).OpenRead();
}

public Stream OpenRead()
{
EnsureFileExists();
public Stream OpenRead()
{
EnsureFileExists();

return File.OpenRead(CachePath);
}
return File.OpenRead(CachePath);
}

public static byte[] ReadAllBytes(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllBytes();
}
public static byte[] ReadAllBytes(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllBytes();
}

public byte[] ReadAllBytes()
{
EnsureFileExists();
public byte[] ReadAllBytes()
{
EnsureFileExists();

return File.ReadAllBytes(CachePath);
}
return File.ReadAllBytes(CachePath);
}

public static string[] ReadAllLines(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllLines();
}
public static string[] ReadAllLines(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllLines();
}

public static string[] ReadAllLines(Uri fileUri, string cachePath, TimeSpan expirationInterval, Encoding encoding)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllLines(encoding);
}
public static string[] ReadAllLines(Uri fileUri, string cachePath, TimeSpan expirationInterval, Encoding encoding)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllLines(encoding);
}

public string[] ReadAllLines()
{
return ReadAllLinesCore(null);
}
public string[] ReadAllLines()
{
return ReadAllLinesCore(null);
}

public string[] ReadAllLines(Encoding encoding)
{
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
public string[] ReadAllLines(Encoding encoding)
{
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));

return ReadAllLinesCore(encoding);
}
return ReadAllLinesCore(encoding);
}

private string[] ReadAllLinesCore(Encoding encoding)
{
EnsureFileExists();
private string[] ReadAllLinesCore(Encoding encoding)
{
EnsureFileExists();

if (encoding == null)
return File.ReadAllLines(CachePath);
else
return File.ReadAllLines(CachePath, encoding);
}
if (encoding == null)
return File.ReadAllLines(CachePath);
else
return File.ReadAllLines(CachePath, encoding);
}

public static string ReadAllText(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllText();
}
public static string ReadAllText(Uri fileUri, string cachePath, TimeSpan expirationInterval)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllText();
}

public static string ReadAllText(Uri fileUri, string cachePath, TimeSpan expirationInterval, Encoding encoding)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllText(encoding);
}
public static string ReadAllText(Uri fileUri, string cachePath, TimeSpan expirationInterval, Encoding encoding)
{
return (new CachedWebFile(fileUri, cachePath, expirationInterval)).ReadAllText(encoding);
}

public string ReadAllText()
{
return ReadAllTextCore(null);
}
public string ReadAllText()
{
return ReadAllTextCore(null);
}

public string ReadAllText(Encoding encoding)
{
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));
public string ReadAllText(Encoding encoding)
{
if (encoding == null)
throw new ArgumentNullException(nameof(encoding));

return ReadAllTextCore(encoding);
}
return ReadAllTextCore(encoding);
}

private string ReadAllTextCore(Encoding encoding)
{
EnsureFileExists();
private string ReadAllTextCore(Encoding encoding)
{
EnsureFileExists();

if (encoding == null)
return File.ReadAllText(CachePath);
else
return File.ReadAllText(CachePath, encoding);
}
if (encoding == null)
return File.ReadAllText(CachePath);
else
return File.ReadAllText(CachePath, encoding);
}
}
Loading

0 comments on commit 2542eca

Please sign in to comment.