@@ -28,8 +28,8 @@ public class SshCommand : IDisposable
28
28
private EventWaitHandle _sessionErrorOccuredWaitHandle ;
29
29
private EventWaitHandle _commandCancelledWaitHandle ;
30
30
private Exception _exception ;
31
- private StringBuilder _result ;
32
- private StringBuilder _error ;
31
+ private string _result ;
32
+ private string _error ;
33
33
private bool _hasError ;
34
34
private bool _isDisposed ;
35
35
private bool _isCancelled ;
@@ -109,21 +109,22 @@ public string Result
109
109
{
110
110
get
111
111
{
112
- _result ??= new StringBuilder ( ) ;
112
+ if ( _result is not null )
113
+ {
114
+ return _result ;
115
+ }
113
116
114
- if ( OutputStream != null && OutputStream . Length > 0 )
117
+ if ( OutputStream is null )
115
118
{
116
- using ( var sr = new StreamReader ( OutputStream ,
117
- _encoding ,
118
- detectEncodingFromByteOrderMarks : true ,
119
- bufferSize : 1024 ,
120
- leaveOpen : true ) )
121
- {
122
- _ = _result . Append ( sr . ReadToEnd ( ) ) ;
123
- }
119
+ return string . Empty ;
124
120
}
125
121
126
- return _result . ToString ( ) ;
122
+ using ( var sr = new StreamReader ( OutputStream ,
123
+ _encoding ,
124
+ detectEncodingFromByteOrderMarks : true ) )
125
+ {
126
+ return _result = sr . ReadToEnd ( ) ;
127
+ }
127
128
}
128
129
}
129
130
@@ -134,26 +135,22 @@ public string Error
134
135
{
135
136
get
136
137
{
137
- if ( _hasError )
138
+ if ( _error is not null )
139
+ {
140
+ return _error ;
141
+ }
142
+
143
+ if ( ExtendedOutputStream is null || ! _hasError )
138
144
{
139
- _error ??= new StringBuilder ( ) ;
140
-
141
- if ( ExtendedOutputStream != null && ExtendedOutputStream . Length > 0 )
142
- {
143
- using ( var sr = new StreamReader ( ExtendedOutputStream ,
144
- _encoding ,
145
- detectEncodingFromByteOrderMarks : true ,
146
- bufferSize : 1024 ,
147
- leaveOpen : true ) )
148
- {
149
- _ = _error . Append ( sr . ReadToEnd ( ) ) ;
150
- }
151
- }
152
-
153
- return _error . ToString ( ) ;
145
+ return string . Empty ;
154
146
}
155
147
156
- return string . Empty ;
148
+ using ( var sr = new StreamReader ( ExtendedOutputStream ,
149
+ _encoding ,
150
+ detectEncodingFromByteOrderMarks : true ) )
151
+ {
152
+ return _error = sr . ReadToEnd ( ) ;
153
+ }
157
154
}
158
155
}
159
156
@@ -265,26 +262,16 @@ public IAsyncResult BeginExecute(AsyncCallback callback, object state)
265
262
throw new ArgumentException ( "CommandText property is empty." ) ;
266
263
}
267
264
268
- var outputStream = OutputStream ;
269
- if ( outputStream is not null )
270
- {
271
- outputStream . Dispose ( ) ;
272
- OutputStream = null ;
273
- }
274
-
275
- var extendedOutputStream = ExtendedOutputStream ;
276
- if ( extendedOutputStream is not null )
277
- {
278
- extendedOutputStream . Dispose ( ) ;
279
- ExtendedOutputStream = null ;
280
- }
265
+ OutputStream ? . Dispose ( ) ;
266
+ ExtendedOutputStream ? . Dispose ( ) ;
281
267
282
268
// Initialize output streams
283
269
OutputStream = new PipeStream ( ) ;
284
270
ExtendedOutputStream = new PipeStream ( ) ;
285
271
286
272
_result = null ;
287
273
_error = null ;
274
+ _hasError = false ;
288
275
_callback = callback ;
289
276
290
277
_channel = CreateChannel ( ) ;
@@ -341,13 +328,21 @@ public string EndExecute(IAsyncResult asyncResult)
341
328
342
329
_inputStream ? . Close ( ) ;
343
330
344
- // wait for operation to complete (or time out)
345
- WaitOnHandle ( _asyncResult . AsyncWaitHandle ) ;
331
+ try
332
+ {
333
+ // wait for operation to complete (or time out)
334
+ WaitOnHandle ( _asyncResult . AsyncWaitHandle ) ;
335
+ }
336
+ finally
337
+ {
338
+ UnsubscribeFromEventsAndDisposeChannel ( _channel ) ;
339
+ _channel = null ;
346
340
347
- UnsubscribeFromEventsAndDisposeChannel ( _channel ) ;
348
- _channel = null ;
341
+ OutputStream ? . Dispose ( ) ;
342
+ ExtendedOutputStream ? . Dispose ( ) ;
349
343
350
- commandAsyncResult . EndCalled = true ;
344
+ commandAsyncResult . EndCalled = true ;
345
+ }
351
346
352
347
if ( ! _isCancelled )
353
348
{
@@ -437,8 +432,8 @@ private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
437
432
438
433
private void SetAsyncComplete ( )
439
434
{
440
- OutputStream ? . Flush ( ) ;
441
- ExtendedOutputStream ? . Flush ( ) ;
435
+ OutputStream ? . Dispose ( ) ;
436
+ ExtendedOutputStream ? . Dispose ( ) ;
442
437
443
438
_asyncResult . IsCompleted = true ;
444
439
@@ -480,11 +475,7 @@ private void Channel_RequestReceived(object sender, ChannelRequestEventArgs e)
480
475
481
476
private void Channel_ExtendedDataReceived ( object sender , ChannelExtendedDataEventArgs e )
482
477
{
483
- if ( ExtendedOutputStream != null )
484
- {
485
- ExtendedOutputStream . Write ( e . Data , 0 , e . Data . Length ) ;
486
- ExtendedOutputStream . Flush ( ) ;
487
- }
478
+ ExtendedOutputStream ? . Write ( e . Data , 0 , e . Data . Length ) ;
488
479
489
480
if ( e . DataTypeCode == 1 )
490
481
{
@@ -494,11 +485,7 @@ private void Channel_ExtendedDataReceived(object sender, ChannelExtendedDataEven
494
485
495
486
private void Channel_DataReceived ( object sender , ChannelDataEventArgs e )
496
487
{
497
- if ( OutputStream != null )
498
- {
499
- OutputStream . Write ( e . Data , 0 , e . Data . Length ) ;
500
- OutputStream . Flush ( ) ;
501
- }
488
+ OutputStream ? . Write ( e . Data , 0 , e . Data . Length ) ;
502
489
503
490
if ( _asyncResult != null )
504
491
{
0 commit comments