SftFileStream and random remote file access

Coordinator
Jun 8, 2011 at 2:46 PM

Hi,

 

I just wanted to let everybody know that I started to implement SftpFileStream class and first version of it already available.

So those who are intrested in this please download it and let me know what you think.

What this class will let you do is to access remote file without download.

So for example if yuou have 1GB file and you want to append something to it or modify few bytes within the file, you no longer need to download the whole file, modify it and then upload it.

I also added some methods similar to one exists in System.IO.File class, for easy file manipulations.

Some code examples:

            using (var sftpClient = new SftpClient(connectionInfo))
            {
                sftpClient.Connect();

                //  Open file for write
                var stream = sftpClient.OpenWrite("abc.txt");
                //  Set position to 10 KB mark
                stream.Position = 1024 * 10;
                //  Get bytes to write
                var bytes = Encoding.ASCII.GetBytes("New data"); 
                //  Write bytes to the file
                stream.Write(bytes, 0, bytes.Length);
                //  Flush any changes
                stream.Flush();
            }

            using (var sftpClient = new SftpClient(connectionInfo))
            {
                sftpClient.Connect();

                //  Read all text from file
                var text = sftpClient.ReadAllText("rsa_aes128.txt");
            }

            using (var sftpClient = new SftpClient(connectionInfo))
            {
                sftpClient.Connect();

                //  Append text to the end of the file
                var text = sftpClient.AppendAllText("abc.txt", "Text appended to the end of the file");
            }

Please let me know if you need more specific examples or have any questions or comments.

Thanks,
Oleg

Jun 8, 2011 at 9:34 PM

Hi  there ,

first of all thanks for this great code . I can't test it right now so I was wondering is it thread safe and doesit have read ahead (I think I saw it ,or am I wrong) ?

Coordinator
Jun 8, 2011 at 9:56 PM

Hi,

I was trying to make it thread safe, as whole library is thread sage, at least that's what I am trying to achieve here.

What do you mean by read ahead?

If you mean to start reading from specific position then yes, look at my first example where I set Position property to 1024 *10.

I am basically saying here skip 10K and start reading after that.

 

Let me know if you have more questions or comments.

 

Thanks,

Oleg

Jun 8, 2011 at 10:48 PM

Something like when I want to read 2048 bytes it reads the full buffer so I in the next read read from a buffer not the server.

Coordinator
Jun 9, 2011 at 1:57 AM

Yes,

its buffer, the implementation is similar to FileStream as I was using FileStream source code as a refernce.

 

Thanks,

Oleg

Jun 9, 2011 at 6:12 PM

Thats very nice ,thanks. One more question my app is multi-threaded 5-7 threads and its read write heavy especially reading attributes . Should I add more SftpSeassion -s to my app or the one like in SftpClient would be enough?

Coordinator
Jun 9, 2011 at 6:17 PM

In general one sftpClient should be enough as it will handle multiple requests.

Just as a background to what happens behind the scene.

One SSH channel is opened for all SFTP needs and trafic, then one or many SFTP requests are sent and client awaits for response to the request that it sent and handles them appropriately.

I even had an example somewhere and have it in my tests too, where I download and upload multiple files at the same time using one sftpClient instance.

 

Hope it helps,

Oleg