-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathScpStream.cs
149 lines (134 loc) · 4.95 KB
/
ScpStream.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Tamir.SharpSsh.jsch;
namespace Tamir.SharpSsh
{
/// <summary>
/// Provides a stream to access files on a remote system using SCP
/// </summary>
public class ScpStream : Stream
{
private Channel channel;
private Stream stream;
bool writeStream;
/// <summary>
/// Creates a <see cref="ScpStream"/> from a given <see cref="Channel"/> and exisiting stream
/// </summary>
/// <param name="channel">The <see cref="Channel"/> that is used by the base stream</param>
/// <param name="stream">The base stream of the <see cref="ScpStream"/></param>
/// <param name="writeStream">A flag indicating whether this is a write stream or a read stream</param>
internal ScpStream(Channel channel, Stream stream, bool writeStream)
{
this.channel = channel;
this.stream = stream;
this.writeStream = writeStream;
}
/// <summary>
/// Gets a value indicating whether the current stream supports reading.
/// </summary>
public override bool CanRead
{
get { return stream.CanRead && !writeStream; }
}
/// <summary>
/// Gets a value indicating whether the current stream supports seeking.
/// </summary>
public override bool CanSeek
{
get { return stream.CanSeek; }
}
/// <summary>
/// Gets a value indicating whether the current stream supports writing.
/// </summary>
public override bool CanWrite
{
get { return stream.CanWrite && writeStream; }
}
/// <summary>
/// Gets the length in bytes of the stream.
/// </summary>
public override long Length
{
get { return stream.Length; }
}
/// <summary>
/// Gets or sets the position within the current stream.
/// </summary>
public override long Position
{
get
{
return stream.Position;
}
set
{
stream.Position = value;
}
}
/// <summary>
/// Closes the <see cref="ScpStream"/> and its associated <see cref="Channel"/>
/// Also writes the file completion byte if the stream is a write stream
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
if (writeStream)
{
byte[] buf = Encoding.Default.GetBytes("\0");
stream.Write(buf, 0, buf.Length);
stream.Flush();
}
channel.disconnect();
stream.Dispose();
base.Dispose(disposing);
}
/// <summary>
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// </summary>
public override void Flush()
{
stream.Flush();
}
/// <summary>
/// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
/// </summary>
/// <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>
/// <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 (0) if the end of the stream has been reached.</returns>
public override int Read(byte[] buffer, int offset, int count)
{
return stream.Read(buffer, offset, count);
}
/// <summary>
/// Sets the position within the current stream.
/// </summary>
/// <param name="offset">A byte offset relative to the origin parameter. </param>
/// <param name="origin">A value of type SeekOrigin indicating the reference point used to obtain the new position. </param>
/// <returns>The new position within the current stream.</returns>
public override long Seek(long offset, SeekOrigin origin)
{
return stream.Seek(offset, origin);
}
/// <summary>
/// Sets the length of the current stream.
/// </summary>
/// <param name="value">The desired length of the current stream in bytes.</param>
public override void SetLength(long value)
{
stream.SetLength(value);
}
/// <summary>
/// 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>
public override void Write(byte[] buffer, int offset, int count)
{
stream.Write(buffer, offset, count);
}
}
}