/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
using System;
using System.IO;
#if AWS_ASYNC_API
using System.Threading;
using System.Threading.Tasks;
#endif
namespace Amazon.Util.Internal
{
///
/// Wrapper class over operations.
/// This change was done for testability.
///
public interface IFile
{
///
bool Exists(string path);
///
string ReadAllText(string path);
///
void WriteAllText(string path, string contents);
#if AWS_ASYNC_API
///
Task ReadAllTextAsync(string path, CancellationToken token = default);
///
Task WriteAllTextAsync(string path, string contents, CancellationToken token = default);
#endif
}
///
public class FileRetriever : IFile
{
public bool Exists(string path) => File.Exists(path);
public string ReadAllText(string path) => File.ReadAllText(path);
public void WriteAllText(string path, string contents) => File.WriteAllText(path, contents);
#if AWS_ASYNC_API
public async Task ReadAllTextAsync(string path, CancellationToken token = default)
{
using (var fs = File.OpenRead(path))
using (var reader = new StreamReader(fs))
return await reader.ReadToEndAsync().ConfigureAwait(false);
}
public async Task WriteAllTextAsync(string path, string contents, CancellationToken token = default)
{
//we use FileMode.Create because we want to truncate the file first if the file exists then write to it.
using (var fs = new FileStream(path, FileMode.Create))
using (var writer = new StreamWriter(fs))
await writer.WriteAsync(contents).ConfigureAwait(false);
}
#endif
}
}