Browse Source

Write, CanWrite, Length and Position methods and properties of the Stream returned by Session.GetFile behave as expected for a read-only steam

Source commit: 0c2c935eab20b4988d7e109b049cc0c39317ab8a
Martin Prikryl 5 years ago
parent
commit
f17ecfa8be
2 changed files with 28 additions and 10 deletions
  1. 1 1
      dotnet/internal/ExeSessionProcess.cs
  2. 27 9
      dotnet/internal/PipeStream.cs

+ 1 - 1
dotnet/internal/ExeSessionProcess.cs

@@ -556,7 +556,7 @@ namespace WinSCP
                 int len = (int)e.Len;
                 if (len > 0)
                 {
-                    StdOut.Write(e.Data, 0, len);
+                    StdOut.WriteInternal(e.Data, 0, len);
                     _logger.WriteLine("Data written to the buffer");
                 }
                 else

+ 27 - 9
dotnet/internal/PipeStream.cs

@@ -30,7 +30,7 @@ namespace WinSCP
     ///  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     ///  OTHER DEALINGS IN THE SOFTWARE.
     /// </license>
-    public class PipeStream : Stream
+    internal class PipeStream : Stream
     {
         #region Private members
 
@@ -54,6 +54,8 @@ namespace WinSCP
 
         private bool _closedWrite;
 
+        private long _position;
+
         #endregion
 
         #region Public properties
@@ -173,6 +175,8 @@ namespace WinSCP
                 }
 
                 Monitor.Pulse(_buffer);
+
+                _position += readLength;
             }
 
             return readLength;
@@ -185,7 +189,8 @@ namespace WinSCP
         /// <returns><c>True</c> if data available; otherwise<c>false</c>.</returns>
         public bool ReadAvailable(int count)
         {
-            var length = Length;
+            CheckDisposed();
+            var length = _buffer.Count;
             return (_isFlushed || length >= count);
         }
 
@@ -202,6 +207,11 @@ namespace WinSCP
         ///<exception cref="ArgumentException">The sum of offset and count is greater than the buffer length.</exception>
         ///<exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception>
         public override void Write(byte[] buffer, int offset, int count)
+        {
+            throw new NotSupportedException();
+        }
+
+        public void WriteInternal(byte[] buffer, int offset, int count)
         {
             if (buffer == null)
                 throw new ArgumentNullException("buffer");
@@ -218,7 +228,7 @@ namespace WinSCP
             lock (_buffer)
             {
                 // wait until the buffer isn't full
-                while (Length >= MaxBufferLength)
+                while (_buffer.Count >= MaxBufferLength)
                     Monitor.Wait(_buffer);
 
                 _isFlushed = false; // if it were flushed before, it soon will not be.
@@ -285,7 +295,7 @@ namespace WinSCP
         /// </returns>
         public override bool CanWrite
         {
-            get { return !_isDisposed; }
+            get { return false; }
         }
 
         /// <summary>
@@ -300,9 +310,7 @@ namespace WinSCP
         {
             get
             {
-                CheckDisposed();
-
-                return _buffer.Count;
+                throw new NotSupportedException();
             }
         }
 
@@ -315,8 +323,18 @@ namespace WinSCP
         /// <exception cref="NotSupportedException">The stream does not support seeking.</exception>
         public override long Position
         {
-            get { return 0; }
-            set { throw new NotSupportedException(); }
+            get
+            {
+                lock (_buffer)
+                {
+                    return _position;
+                }
+            }
+
+            set
+            {
+                throw new NotSupportedException();
+            }
         }
 
         #endregion