Skip to content

Commit 9a1b947

Browse files
committed
Check CanSeek in ReadAllBytes
1 parent 83c4587 commit 9a1b947

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/Renci.SshNet/Sftp/SftpFileStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public override bool CanWrite
5555
/// Gets a value indicating whether timeout properties are usable for <see cref="SftpFileStream"/>.
5656
/// </summary>
5757
/// <value>
58-
/// <see langword="true"/> in all cases.
58+
/// <see langword="false"/> in all cases.
5959
/// </value>
6060
public override bool CanTimeout
6161
{

src/Renci.SshNet/SftpClient.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,8 +1714,20 @@ public byte[] ReadAllBytes(string path)
17141714
{
17151715
using (var stream = OpenRead(path))
17161716
{
1717-
var buffer = new byte[stream.Length];
1718-
stream.ReadExactly(buffer, 0, buffer.Length);
1717+
byte[] buffer;
1718+
1719+
if (stream.CanSeek)
1720+
{
1721+
buffer = new byte[stream.Length];
1722+
stream.ReadExactly(buffer, 0, buffer.Length);
1723+
}
1724+
else
1725+
{
1726+
MemoryStream ms = new();
1727+
stream.CopyTo(ms);
1728+
buffer = ms.ToArray();
1729+
}
1730+
17191731
return buffer;
17201732
}
17211733
}

test/Renci.SshNet.IntegrationTests/SftpTests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6360,7 +6360,7 @@ private void DownloadFileRandomMethod(SftpClient client, string path, Stream out
63606360
{
63616361
Console.Write($"Downloading '{path}'");
63626362

6363-
var random = new Random().Next(1, 6);
6363+
var random = new Random().Next(1, 7);
63646364
switch (random)
63656365
{
63666366
case 1:
@@ -6390,15 +6390,23 @@ private void DownloadFileRandomMethod(SftpClient client, string path, Stream out
63906390
}
63916391

63926392
break;
6393-
default:
6394-
Debug.Assert(random == 5);
6393+
case 5:
63956394
Console.WriteLine($" with {nameof(SftpFileStream.CopyToAsync)}");
63966395

63976396
using (var fs = client.OpenAsync(path, FileMode.Open, FileAccess.Read, CancellationToken.None).GetAwaiter().GetResult())
63986397
{
63996398
fs.CopyToAsync(output).GetAwaiter().GetResult();
64006399
}
64016400

6401+
break;
6402+
default:
6403+
Debug.Assert(random == 6);
6404+
Console.WriteLine($" with {nameof(SftpClient.ReadAllBytes)}");
6405+
6406+
byte[] bytes = client.ReadAllBytes(path);
6407+
6408+
output.Write(bytes, 0, bytes.Length);
6409+
64026410
break;
64036411
}
64046412
}

0 commit comments

Comments
 (0)