Skip to content

Commit 1b21b6d

Browse files
committed
To improve compatibility of SftpClient with System.IO.File, the following methods now use UTF-8 encoding without a Byte-Order Mark (BOM):
void AppendAllLines(string path, IEnumerable<string> contents) void AppendAllText(string path, string contents) StreamWriter AppendText(string path) StreamWriter CreateText(string path) void WriteAllLines(string path, IEnumerable<string> contents) void WriteAllLines(string path, string[] contents) void WriteAllText(string path, string contents)
1 parent 79c9a4b commit 1b21b6d

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/Renci.SshNet/SftpClient.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace Renci.SshNet
1818
/// </summary>
1919
public class SftpClient : BaseClient
2020
{
21+
private static readonly Encoding Utf8NoBOM = new UTF8Encoding(false, true);
22+
2123
/// <summary>
2224
/// Holds the <see cref="ISftpSession"/> instance that is used to communicate to the
2325
/// SFTP server.
@@ -1006,6 +1008,9 @@ public SftpFileSytemInformation GetStatus(string path)
10061008
/// <exception cref="SshConnectionException">Client is not connected.</exception>
10071009
/// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
10081010
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
1011+
/// <remarks>
1012+
/// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM)
1013+
/// </remarks>
10091014
public void AppendAllLines(string path, IEnumerable<string> contents)
10101015
{
10111016
CheckDisposed();
@@ -1057,6 +1062,9 @@ public void AppendAllLines(string path, IEnumerable<string> contents, Encoding e
10571062
/// <exception cref="SshConnectionException">Client is not connected.</exception>
10581063
/// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
10591064
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
1065+
/// <remarks>
1066+
/// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).
1067+
/// </remarks>
10601068
public void AppendAllText(string path, string contents)
10611069
{
10621070
using (var stream = AppendText(path))
@@ -1088,15 +1096,16 @@ public void AppendAllText(string path, string contents, Encoding encoding)
10881096
/// </summary>
10891097
/// <param name="path">The path to the file to append to.</param>
10901098
/// <returns>
1091-
/// A <see cref="StreamWriter"/> that appends UTF-8 encoded text to an existing file.
1099+
/// A <see cref="StreamWriter"/> that appends text to a file using UTF-8 encoding without a
1100+
/// Byte-Order Mark (BOM).
10921101
/// </returns>
10931102
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
10941103
/// <exception cref="SshConnectionException">Client is not connected.</exception>
10951104
/// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
10961105
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
10971106
public StreamWriter AppendText(string path)
10981107
{
1099-
return AppendText(path, Encoding.UTF8);
1108+
return AppendText(path, Utf8NoBOM);
11001109
}
11011110

11021111
/// <summary>
@@ -1167,10 +1176,12 @@ public SftpFileStream Create(string path, int bufferSize)
11671176
/// </summary>
11681177
/// <param name="path">The file to be opened for writing.</param>
11691178
/// <returns>
1170-
/// A <see cref="StreamWriter"/> that writes to the specified file using UTF-8 encoding.
1179+
/// A <see cref="StreamWriter"/> that writes text to a file using UTF-8 encoding without
1180+
/// a Byte-Order Mark (BOM).
11711181
/// </returns>
11721182
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
11731183
/// <exception cref="SshConnectionException">Client is not connected.</exception>
1184+
/// <exception cref="SftpPathNotFoundException">The specified path is invalid, or its directory was not found on the remote host.</exception>
11741185
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
11751186
/// <remarks>
11761187
/// <para>
@@ -1182,7 +1193,7 @@ public SftpFileStream Create(string path, int bufferSize)
11821193
/// </remarks>
11831194
public StreamWriter CreateText(string path)
11841195
{
1185-
return CreateText(path, Encoding.UTF8);
1196+
return CreateText(path, Utf8NoBOM);
11861197
}
11871198

11881199
/// <summary>
@@ -1586,6 +1597,9 @@ public void WriteAllBytes(string path, byte[] bytes)
15861597
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
15871598
/// <remarks>
15881599
/// <para>
1600+
/// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).
1601+
/// </para>
1602+
/// <para>
15891603
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
15901604
/// </para>
15911605
/// <para>
@@ -1594,7 +1608,7 @@ public void WriteAllBytes(string path, byte[] bytes)
15941608
/// </remarks>
15951609
public void WriteAllLines(string path, IEnumerable<string> contents)
15961610
{
1597-
WriteAllLines(path, contents, Encoding.UTF8);
1611+
WriteAllLines(path, contents, Utf8NoBOM);
15981612
}
15991613

16001614
/// <summary>
@@ -1607,6 +1621,9 @@ public void WriteAllLines(string path, IEnumerable<string> contents)
16071621
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
16081622
/// <remarks>
16091623
/// <para>
1624+
/// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).
1625+
/// </para>
1626+
/// <para>
16101627
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
16111628
/// </para>
16121629
/// <para>
@@ -1615,7 +1632,7 @@ public void WriteAllLines(string path, IEnumerable<string> contents)
16151632
/// </remarks>
16161633
public void WriteAllLines(string path, string[] contents)
16171634
{
1618-
WriteAllLines(path, contents, Encoding.UTF8);
1635+
WriteAllLines(path, contents, Utf8NoBOM);
16191636
}
16201637

16211638
/// <summary>
@@ -1682,10 +1699,10 @@ public void WriteAllLines(string path, string[] contents, Encoding encoding)
16821699
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <b>null</b>.</exception>
16831700
/// <exception cref="SshConnectionException">Client is not connected.</exception>
16841701
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
1702+
/// <remarks>
16851703
/// <para>
1686-
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
1704+
/// The characters are written to the file using UTF-8 encoding without a Byte-Order Mark (BOM).
16871705
/// </para>
1688-
/// <remarks>
16891706
/// <para>
16901707
/// If the target file already exists, it is overwritten. It is not first truncated to zero bytes.
16911708
/// </para>

0 commit comments

Comments
 (0)