|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | + |
| 4 | +using Renci.SshNet.Channels; |
| 5 | + |
| 6 | +namespace Renci.SshNet.Common |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// ChannelInputStream is a one direction stream intended for channel data. |
| 10 | + /// </summary> |
| 11 | + internal sealed class ChannelInputStream : Stream |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Channel to send data to. |
| 15 | + /// </summary> |
| 16 | + private readonly IChannelSession _channel; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Total bytes passed through the stream. |
| 20 | + /// </summary> |
| 21 | + private long _totalPosition; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Indicates whether the current instance was disposed. |
| 25 | + /// </summary> |
| 26 | + private bool _isDisposed; |
| 27 | + |
| 28 | + internal ChannelInputStream(IChannelSession channel) |
| 29 | + { |
| 30 | + _channel = channel; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device. |
| 35 | + /// </summary> |
| 36 | + /// <exception cref="IOException">An I/O error occurs.</exception> |
| 37 | + /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| 38 | + /// <remarks> |
| 39 | + /// Once flushed, any subsequent read operations no longer block until requested bytes are available. Any write operation reactivates blocking |
| 40 | + /// reads. |
| 41 | + /// </remarks> |
| 42 | + public override void Flush() |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// When overridden in a derived class, sets the position within the current stream. |
| 48 | + /// </summary> |
| 49 | + /// <returns> |
| 50 | + /// The new position within the current stream. |
| 51 | + /// </returns> |
| 52 | + /// <param name="offset">A byte offset relative to the origin parameter.</param> |
| 53 | + /// <param name="origin">A value of type <see cref="SeekOrigin"/> indicating the reference point used to obtain the new position.</param> |
| 54 | + /// <exception cref="NotSupportedException">The stream does not support seeking, such as if the stream is constructed from a pipe or console output.</exception> |
| 55 | + public override long Seek(long offset, SeekOrigin origin) |
| 56 | + { |
| 57 | + throw new NotSupportedException(); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// When overridden in a derived class, sets the length of the current stream. |
| 62 | + /// </summary> |
| 63 | + /// <param name="value">The desired length of the current stream in bytes.</param> |
| 64 | + /// <exception cref="NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output.</exception> |
| 65 | + public override void SetLength(long value) |
| 66 | + { |
| 67 | + throw new NotSupportedException(); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read. |
| 72 | + /// </summary> |
| 73 | + /// <returns> |
| 74 | + /// 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 if the stream is closed or end of the stream has been reached. |
| 75 | + /// </returns> |
| 76 | + /// <param name="buffer">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.</param> |
| 77 | + /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param> |
| 78 | + /// <param name="count">The maximum number of bytes to be read from the current stream.</param> |
| 79 | + /// <exception cref="ArgumentException">The sum of offset and count is larger than the buffer length.</exception> |
| 80 | + /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| 81 | + /// <exception cref="NotSupportedException">The stream does not support reading.</exception> |
| 82 | + /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception> |
| 83 | + /// <exception cref="IOException">An I/O error occurs.</exception> |
| 84 | + /// <exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception> |
| 85 | + public override int Read(byte[] buffer, int offset, int count) |
| 86 | + { |
| 87 | + throw new NotSupportedException(); |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written. |
| 92 | + /// </summary> |
| 93 | + /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param> |
| 94 | + /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param> |
| 95 | + /// <param name="count">The number of bytes to be written to the current stream.</param> |
| 96 | + /// <exception cref="IOException">An I/O error occurs.</exception> |
| 97 | + /// <exception cref="NotSupportedException">The stream does not support writing.</exception> |
| 98 | + /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| 99 | + /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <c>null</c>.</exception> |
| 100 | + /// <exception cref="ArgumentException">The sum of offset and count is greater than the buffer length.</exception> |
| 101 | + /// <exception cref="ArgumentOutOfRangeException">offset or count is negative.</exception> |
| 102 | + public override void Write(byte[] buffer, int offset, int count) |
| 103 | + { |
| 104 | + if (buffer == null) |
| 105 | + { |
| 106 | + throw new ArgumentNullException(nameof(buffer)); |
| 107 | + } |
| 108 | + |
| 109 | + if (offset + count > buffer.Length) |
| 110 | + { |
| 111 | + throw new ArgumentException("The sum of offset and count is greater than the buffer length."); |
| 112 | + } |
| 113 | + |
| 114 | + if (offset < 0 || count < 0) |
| 115 | + { |
| 116 | + throw new ArgumentOutOfRangeException(nameof(offset), "offset or count is negative."); |
| 117 | + } |
| 118 | + |
| 119 | + if (_isDisposed) |
| 120 | + { |
| 121 | + throw CreateObjectDisposedException(); |
| 122 | + } |
| 123 | + |
| 124 | + if (count == 0) |
| 125 | + { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + _channel.SendData(buffer, offset, count); |
| 130 | + _totalPosition += count; |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Releases the unmanaged resources used by the Stream and optionally releases the managed resources. |
| 135 | + /// </summary> |
| 136 | + /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> |
| 137 | + protected override void Dispose(bool disposing) |
| 138 | + { |
| 139 | + if (!_isDisposed) |
| 140 | + { |
| 141 | + _isDisposed = true; |
| 142 | + |
| 143 | + // Closing the InputStream requires sending EOF. |
| 144 | + if (disposing && _totalPosition > 0 && _channel?.IsOpen == true) |
| 145 | + { |
| 146 | + _channel.SendEof(); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + base.Dispose(disposing); |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Gets a value indicating whether the current stream supports reading. |
| 155 | + /// </summary> |
| 156 | + /// <returns> |
| 157 | + /// true if the stream supports reading; otherwise, false. |
| 158 | + /// </returns> |
| 159 | + public override bool CanRead |
| 160 | + { |
| 161 | + get { return false; } |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Gets a value indicating whether the current stream supports seeking. |
| 166 | + /// </summary> |
| 167 | + /// <returns> |
| 168 | + /// <c>true</c> if the stream supports seeking; otherwise, <c>false</c>. |
| 169 | + /// </returns> |
| 170 | + public override bool CanSeek |
| 171 | + { |
| 172 | + get { return false; } |
| 173 | + } |
| 174 | + |
| 175 | + /// <summary> |
| 176 | + /// Gets a value indicating whether the current stream supports writing. |
| 177 | + /// </summary> |
| 178 | + /// <returns> |
| 179 | + /// <c>true</c> if the stream supports writing; otherwise, <c>false</c>. |
| 180 | + /// </returns> |
| 181 | + public override bool CanWrite |
| 182 | + { |
| 183 | + get { return true; } |
| 184 | + } |
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Gets the length in bytes of the stream. |
| 188 | + /// </summary> |
| 189 | + /// <returns> |
| 190 | + /// A long value representing the length of the stream in bytes. |
| 191 | + /// </returns> |
| 192 | + /// <exception cref="NotSupportedException">A class derived from Stream does not support seeking.</exception> |
| 193 | + /// <exception cref="ObjectDisposedException">Methods were called after the stream was closed.</exception> |
| 194 | + public override long Length |
| 195 | + { |
| 196 | + get { throw new NotSupportedException(); } |
| 197 | + } |
| 198 | + |
| 199 | + /// <summary> |
| 200 | + /// Gets or sets the position within the current stream. |
| 201 | + /// </summary> |
| 202 | + /// <returns> |
| 203 | + /// The current position within the stream. |
| 204 | + /// </returns> |
| 205 | + /// <exception cref="NotSupportedException">The stream does not support seeking.</exception> |
| 206 | + public override long Position |
| 207 | + { |
| 208 | + get { return _totalPosition; } |
| 209 | + set { throw new NotSupportedException(); } |
| 210 | + } |
| 211 | + |
| 212 | + private ObjectDisposedException CreateObjectDisposedException() |
| 213 | + { |
| 214 | + return new ObjectDisposedException(GetType().FullName); |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments