private void InternalUploadFile(Stream input, string path, SftpUploadAsyncResult asynchResult, Flags flags)
{
if (input == null)
throw new ArgumentNullException("input");
if (path.IsNullOrWhiteSpace())
throw new ArgumentException("path");
// Ensure that connection is established.
this.EnsureConnection();
var fullPath = this._sftpSession.GetCanonicalPath(path);
var handle = this._sftpSession.RequestOpen(fullPath, flags);
ulong offset = 0;
var buffer = new byte[this.BufferSize];
var uploadCompleted = false;
do
{
var bytesRead = input.Read(buffer, 0, buffer.Length);
if (bytesRead < this.BufferSize)
{
var data = new byte[bytesRead];
Buffer.BlockCopy(buffer, 0, data, 0, bytesRead);
using (var wait = new AutoResetEvent(false))
{
this._sftpSession.RequestWrite(handle, offset, data, wait);
}
uploadCompleted = true;
}
else
{
this._sftpSession.RequestWrite(handle, offset, buffer, null);
}
offset += (uint)bytesRead;
// Call callback to report number of bytes read
if (asynchResult != null)
{
asynchResult.Update(offset);
}
} while (!uploadCompleted);
this._sftpSession.RequestClose(handle);
}