Class SftpFileStream
Exposes a Stream around a remote SFTP file, supporting both synchronous and asynchronous read and write operations.
public class SftpFileStream : Stream, IDisposable, IAsyncDisposable
- Inheritance
-
SftpFileStream
- Implements
- Inherited Members
Properties
CanRead
Gets a value indicating whether the current stream supports reading.
public override bool CanRead { get; }
Property Value
CanSeek
Gets a value indicating whether the current stream supports seeking.
public override bool CanSeek { get; }
Property Value
CanTimeout
Gets a value indicating whether timeout properties are usable for SftpFileStream.
public override bool CanTimeout { get; }
Property Value
CanWrite
Gets a value indicating whether the current stream supports writing.
public override bool CanWrite { get; }
Property Value
Handle
Gets the operating system file handle for the file that the current SftpFileStream encapsulates.
public virtual byte[] Handle { get; }
Property Value
- byte[]
The operating system file handle for the file that the current SftpFileStream encapsulates.
Length
Gets the length in bytes of the stream.
public override long Length { get; }
Property Value
- long
A long value representing the length of the stream in bytes.
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 SftpFileStream.
public string Name { get; }
Property Value
- string
The name of the path that was used to construct the current SftpFileStream.
Position
Gets or sets the position within the current stream.
public override long Position { get; set; }
Property Value
- long
The current position within the stream.
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
The timeout.
Methods
Dispose(bool)
Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
protected override void Dispose(bool disposing)
Parameters
disposing
booltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
~SftpFileStream()
Finalizes an instance of the SftpFileStream class.
protected ~SftpFileStream()
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
CancellationTokenThe CancellationToken to observe.
Returns
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
intThe zero-based byte offset in
buffer
at which to begin storing the data read from the current stream.count
intThe 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 Read(byte[], int, int) or ReadByte() invocation.
Exceptions
- ArgumentException
The sum of
offset
andcount
is larger than the buffer length.- ArgumentNullException
buffer
is null.- ArgumentOutOfRangeException
offset
orcount
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
intThe zero-based byte offset in
buffer
at which to begin storing the data read from the current stream.count
intThe maximum number of bytes to be read from the current stream.
cancellationToken
CancellationTokenThe CancellationToken to observe.
Returns
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
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
longA byte offset relative to the
origin
parameter.origin
SeekOriginA value of type 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
longThe 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 frombuffer
to the current stream.offset
intThe zero-based byte offset in
buffer
at which to begin copying bytes to the current stream.count
intThe number of bytes to be written to the current stream.
Exceptions
- ArgumentException
The sum of
offset
andcount
is greater than the buffer length.- ArgumentNullException
buffer
is null.- ArgumentOutOfRangeException
offset
orcount
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 frombuffer
to the current stream.offset
intThe zero-based byte offset in
buffer
at which to begin copying bytes to the current stream.count
intThe number of bytes to be written to the current stream.
cancellationToken
CancellationTokenThe CancellationToken to observe.
Returns
Exceptions
- ArgumentException
The sum of
offset
andcount
is greater than the buffer length.- ArgumentNullException
buffer
is null.- ArgumentOutOfRangeException
offset
orcount
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
byteThe 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.