Table of Contents

Class SftpFileStream

Namespace
Renci.SshNet.Sftp
Assembly
Renci.SshNet.dll

Exposes a System.IO.Stream around a remote SFTP file, supporting both synchronous and asynchronous read and write operations.

public class SftpFileStream : Stream, IAsyncDisposable, IDisposable

Inheritance

Implements

Inherited Members

Properties

CanRead

Gets a value indicating whether the current stream supports reading.

public override bool CanRead { get; }

Property Value

bool

CanSeek

Gets a value indicating whether the current stream supports seeking.

public override bool CanSeek { get; }

Property Value

bool

CanTimeout

Gets a value indicating whether timeout properties are usable for Renci.SshNet.Sftp.SftpFileStream.

public override bool CanTimeout { get; }

Property Value

bool

CanWrite

Gets a value indicating whether the current stream supports writing.

public override bool CanWrite { get; }

Property Value

bool

Handle

Gets the operating system file handle for the file that the current Renci.SshNet.Sftp.SftpFileStream encapsulates.

public virtual byte[] Handle { get; }

Property Value

byte[]

Length

Gets the length in bytes of the stream.

public override long Length { get; }

Property Value

long

Exceptions

NotSupportedException

A class derived from Stream does not support seeking.

ObjectDisposedException

Methods were called after the stream was closed.

IOException

IO operation failed.

Name

Gets the name of the path that was used to construct the current Renci.SshNet.Sftp.SftpFileStream.

public string Name { get; }

Property Value

string

Position

Gets or sets the position within the current stream.

public override long Position { get; set; }

Property Value

long

Exceptions

IOException

An I/O error occurs.

NotSupportedException

The stream does not support seeking.

ObjectDisposedException

Methods were called after the stream was closed.

Timeout

Gets or sets the operation timeout.

public TimeSpan Timeout { get; set; }

Property Value

TimeSpan

Methods

Dispose(bool)

Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Flush()

Clears all buffers for this stream and causes any buffered data to be written to the file.

public override void Flush()

Exceptions

IOException

An I/O error occurs.

ObjectDisposedException

Stream is closed.

FlushAsync(CancellationToken)

Asynchronously clears all buffers for this stream and causes any buffered data to be written to the file.

public override Task FlushAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The System.Threading.CancellationToken to observe.

Returns

Task

A System.Threading.Tasks.Task that represents the asynchronous flush operation.

Exceptions

IOException

An I/O error occurs.

ObjectDisposedException

Stream is closed.

Read(byte[], int, int)

Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset int

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

count int

The maximum number of bytes to be read from the current stream.

Returns

int

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

Remarks

This method attempts to read up to count bytes. This either from the buffer, from the server (using one or more SSH_FXP_READ requests) or using a combination of both.

The read loop is interrupted when either count bytes are read, the server returns zero bytes (EOF) or less bytes than the read buffer size.

When a server returns less number of bytes than the read buffer size, this may indicate that EOF has been reached. A subsequent (SSH_FXP_READ) server request is necessary to make sure EOF has effectively been reached. Breaking out of the read loop avoids reading from the server twice to determine EOF: once in the read loop, and once upon the next Renci.SshNet.Sftp.SftpFileStream.Read(System.Byte[],System.Int32,System.Int32) or Renci.SshNet.Sftp.SftpFileStream.ReadByte invocation.

Exceptions

ArgumentException

The sum of offset and count is larger than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurs.

NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

ReadAsync(byte[], int, int, CancellationToken)

Asynchronously reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

Parameters

buffer byte[]

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset int

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

count int

The maximum number of bytes to be read from the current stream.

cancellationToken CancellationToken

The System.Threading.CancellationToken to observe.

Returns

Task<int>

A System.Threading.Tasks.Task that represents the asynchronous read operation.

ReadByte()

Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

public override int ReadByte()

Returns

int

The unsigned byte cast to an System.Int32, or -1 if at the end of the stream.

Exceptions

NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

IOException

Read operation failed.

Seek(long, SeekOrigin)

Sets the position within the current stream.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

A byte offset relative to the origin parameter.

origin SeekOrigin

A value of type System.IO.SeekOrigin indicating the reference point used to obtain the new position.

Returns

long

The new position within the current stream.

Exceptions

IOException

An I/O error occurs.

NotSupportedException

The stream does not support seeking, such as if the stream is constructed from a pipe or console output.

ObjectDisposedException

Methods were called after the stream was closed.

SetLength(long)

Sets the length of the current stream.

public override void SetLength(long value)

Parameters

value long

The desired length of the current stream in bytes.

Remarks

Buffers are first flushed.

If the specified value is less than the current length of the stream, the stream is truncated and - if the current position is greater than the new length - the current position is moved to the last byte of the stream.

If the given value is greater than the current length of the stream, the stream is expanded and the current position remains the same.

Exceptions

IOException

An I/O error occurs.

NotSupportedException

The stream does not support both writing and seeking.

ObjectDisposedException

Methods were called after the stream was closed.

ArgumentOutOfRangeException

value must be greater than zero.

Write(byte[], int, int)

Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

public override void Write(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of bytes. This method copies count bytes from buffer to the current stream.

offset int

The zero-based byte offset in buffer at which to begin copying bytes to the current stream.

count int

The number of bytes to be written to the current stream.

Exceptions

ArgumentException

The sum of offset and count is greater than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurs.

NotSupportedException

The stream does not support writing.

ObjectDisposedException

Methods were called after the stream was closed.

WriteAsync(byte[], int, int, CancellationToken)

Asynchronously writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

Parameters

buffer byte[]

An array of bytes. This method copies count bytes from buffer to the current stream.

offset int

The zero-based byte offset in buffer at which to begin copying bytes to the current stream.

count int

The number of bytes to be written to the current stream.

cancellationToken CancellationToken

The System.Threading.CancellationToken to observe.

Returns

Task

A System.Threading.Tasks.Task that represents the asynchronous write operation.

Exceptions

ArgumentException

The sum of offset and count is greater than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurs.

NotSupportedException

The stream does not support writing.

ObjectDisposedException

Methods were called after the stream was closed.

WriteByte(byte)

Writes a byte to the current position in the stream and advances the position within the stream by one byte.

public override void WriteByte(byte value)

Parameters

value byte

The byte to write to the stream.

Exceptions

IOException

An I/O error occurs.

NotSupportedException

The stream does not support writing, or the stream is already closed.

ObjectDisposedException

Methods were called after the stream was closed.