-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf72951
commit 27ed50b
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright (c) Sphere 10 Software. All rights reserved. (https://sphere10.com) | ||
// Author: Herman Schoenfeld | ||
// | ||
// Distributed under the MIT software license, see the accompanying file | ||
// LICENSE or visit http://www.opensource.org/licenses/mit-license.php. | ||
// | ||
// This notice must not be removed when duplicating this file or its contents, in whole or in part. | ||
|
||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Hydrogen; | ||
|
||
/// <summary> | ||
/// TextWriter which writes to a file stream. | ||
/// </summary> | ||
/// <remarks></remarks> | ||
public class FileTextWriter : TextWriterBase { | ||
|
||
/// <summary> | ||
/// This is the default encoding used by StreamWriter, which File.AppendAllText uses internally. | ||
/// </summary> | ||
private static Encoding _swEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); | ||
|
||
private readonly TextWriter _textWriter; | ||
|
||
public FileTextWriter(string filePath, FileMode fileMode) | ||
: this(new FileStream(filePath, fileMode, FileAccess.Write), _swEncoding) { | ||
} | ||
|
||
public FileTextWriter(FileStream fileStream, Encoding encoding) { | ||
_swEncoding = encoding; | ||
FileStream = fileStream; | ||
_textWriter = new StreamWriter(FileStream, encoding); | ||
} | ||
|
||
public FileStream FileStream { get; set; } | ||
|
||
protected override void Dispose(bool disposing) { | ||
_textWriter.Flush(); | ||
FileStream.Flush(); | ||
FileStream.Close(); | ||
FileStream.Dispose(); | ||
base.Dispose(disposing); | ||
} | ||
|
||
protected override void InternalWrite(string value) | ||
=> _textWriter.Write(value); | ||
|
||
protected override Task InternalWriteAsync(string value) | ||
=> _textWriter.WriteAsync(value); | ||
|
||
} |