internal SftpFileStream(SftpSession session, string path, FileMode mode, FileAccess access, int bufferSize, bool useAsync)
{
// Validate the parameters.
if (session == null)
throw new SshConnectionException("Client not connected.");
if (path == null)
{
throw new ArgumentNullException("path");
}
if (bufferSize <= 0)
{
throw new ArgumentOutOfRangeException("bufferSize");
}
if (access < FileAccess.Read || access > FileAccess.ReadWrite)
{
throw new ArgumentOutOfRangeException("access");
}
if (mode < FileMode.CreateNew || mode > FileMode.Append)
{
throw new ArgumentOutOfRangeException("mode");
}
this.Timeout = TimeSpan.FromSeconds(30);
this.Name = path;
// Initialize the object state.
this._session = session;
this._access = access;
this._ownsHandle = true;
this._isAsync = useAsync;
this._bufferPosition = 0;
this._bufferLen = 0;
this._bufferOwnedByWrite = false;
this._canSeek = true;
this._position = 0;
this._serverFilePosition = 0;
this._session.Disconnected += Session_Disconnected;
var flags = Flags.None;
switch (access)
{
case FileAccess.Read:
flags |= Flags.Read;
break;
case FileAccess.Write:
flags |= Flags.Write;
break;
case FileAccess.ReadWrite:
flags |= Flags.Read;
flags |= Flags.Write;
break;
}
switch (mode)
{
case FileMode.Append:
flags |= Flags.Append;
break;
case FileMode.Create:
this._handle = this._session.RequestOpen(path, flags | Flags.Truncate, true);
if (this._handle == null)
{
flags |= Flags.CreateNew;
}
else
{
flags |= Flags.Truncate;
}
break;
case FileMode.CreateNew:
flags |= Flags.CreateNew;
break;
case FileMode.Open:
break;
case FileMode.OpenOrCreate:
flags |= Flags.CreateNewOrOpen;
break;
case FileMode.Truncate:
flags |= Flags.Truncate;
break;
}
if (this._handle == null)
this._handle = this._session.RequestOpen(path, flags);
this._attributes = this._session.RequestFStat(this._handle);
this._readBufferSize = (int)session.CalculateOptimalReadLength((uint)bufferSize);
this._readBuffer = new byte[_readBufferSize];
this._writeBufferSize = (int)session.CalculateOptimalWriteLength((uint)bufferSize, _handle);
this._writeBuffer = new byte[_writeBufferSize];
if (mode == FileMode.Append)
{
this._position = this._attributes.Size;
this._serverFilePosition = (ulong)this._attributes.Size;
}
}