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