~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NSch/NSch/IO.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2006-2010 ymnk, JCraft,Inc. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, with or without
 
5
modification, are permitted provided that the following conditions are met:
 
6
 
 
7
  1. Redistributions of source code must retain the above copyright notice,
 
8
     this list of conditions and the following disclaimer.
 
9
 
 
10
  2. Redistributions in binary form must reproduce the above copyright 
 
11
     notice, this list of conditions and the following disclaimer in 
 
12
     the documentation and/or other materials provided with the distribution.
 
13
 
 
14
  3. The names of the authors may not be used to endorse or promote products
 
15
     derived from this software without specific prior written permission.
 
16
 
 
17
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
 
18
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 
19
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
 
20
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
 
21
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
22
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 
23
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
24
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 
25
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 
26
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
27
 
 
28
This code is based on jsch (http://www.jcraft.com/jsch).
 
29
All credit should go to the authors of jsch.
 
30
*/
 
31
 
 
32
using System;
 
33
using System.IO;
 
34
using NSch;
 
35
using Sharpen;
 
36
 
 
37
namespace NSch
 
38
{
 
39
        public class IO
 
40
        {
 
41
                internal InputStream @in;
 
42
 
 
43
                internal OutputStream @out;
 
44
 
 
45
                internal OutputStream out_ext;
 
46
 
 
47
                private bool in_dontclose = false;
 
48
 
 
49
                private bool out_dontclose = false;
 
50
 
 
51
                private bool out_ext_dontclose = false;
 
52
 
 
53
                internal virtual void SetOutputStream(OutputStream @out)
 
54
                {
 
55
                        this.@out = @out;
 
56
                }
 
57
 
 
58
                internal virtual void SetOutputStream(OutputStream @out, bool dontclose)
 
59
                {
 
60
                        this.out_dontclose = dontclose;
 
61
                        SetOutputStream(@out);
 
62
                }
 
63
 
 
64
                internal virtual void SetExtOutputStream(OutputStream @out)
 
65
                {
 
66
                        this.out_ext = @out;
 
67
                }
 
68
 
 
69
                internal virtual void SetExtOutputStream(OutputStream @out, bool dontclose)
 
70
                {
 
71
                        this.out_ext_dontclose = dontclose;
 
72
                        SetExtOutputStream(@out);
 
73
                }
 
74
 
 
75
                internal virtual void SetInputStream(InputStream @in)
 
76
                {
 
77
                        this.@in = @in;
 
78
                }
 
79
 
 
80
                internal virtual void SetInputStream(InputStream @in, bool dontclose)
 
81
                {
 
82
                        this.in_dontclose = dontclose;
 
83
                        SetInputStream(@in);
 
84
                }
 
85
 
 
86
                /// <exception cref="System.IO.IOException"></exception>
 
87
                /// <exception cref="System.Net.Sockets.SocketException"></exception>
 
88
                public virtual void Put(Packet p)
 
89
                {
 
90
                        @out.Write(p.buffer.buffer, 0, p.buffer.index);
 
91
                        @out.Flush();
 
92
                }
 
93
 
 
94
                /// <exception cref="System.IO.IOException"></exception>
 
95
                internal virtual void Put(byte[] array, int begin, int length)
 
96
                {
 
97
                        @out.Write(array, begin, length);
 
98
                        @out.Flush();
 
99
                }
 
100
 
 
101
                /// <exception cref="System.IO.IOException"></exception>
 
102
                internal virtual void Put_ext(byte[] array, int begin, int length)
 
103
                {
 
104
                        out_ext.Write(array, begin, length);
 
105
                        out_ext.Flush();
 
106
                }
 
107
 
 
108
                /// <exception cref="System.IO.IOException"></exception>
 
109
                internal virtual int GetByte()
 
110
                {
 
111
                        return @in.Read();
 
112
                }
 
113
 
 
114
                /// <exception cref="System.IO.IOException"></exception>
 
115
                internal virtual void GetByte(byte[] array)
 
116
                {
 
117
                        GetByte(array, 0, array.Length);
 
118
                }
 
119
 
 
120
                /// <exception cref="System.IO.IOException"></exception>
 
121
                internal virtual void GetByte(byte[] array, int begin, int length)
 
122
                {
 
123
                        do
 
124
                        {
 
125
                                int completed = @in.Read(array, begin, length);
 
126
                                if (completed < 0)
 
127
                                {
 
128
                                        throw new IOException("End of IO Stream Read");
 
129
                                }
 
130
                                begin += completed;
 
131
                                length -= completed;
 
132
                        }
 
133
                        while (length > 0);
 
134
                }
 
135
 
 
136
                internal virtual void Out_close()
 
137
                {
 
138
                        try
 
139
                        {
 
140
                                if (@out != null && !out_dontclose)
 
141
                                {
 
142
                                        @out.Close();
 
143
                                }
 
144
                                @out = null;
 
145
                        }
 
146
                        catch (Exception)
 
147
                        {
 
148
                        }
 
149
                }
 
150
 
 
151
                public virtual void Close()
 
152
                {
 
153
                        try
 
154
                        {
 
155
                                if (@in != null && !in_dontclose)
 
156
                                {
 
157
                                        @in.Close();
 
158
                                }
 
159
                                @in = null;
 
160
                        }
 
161
                        catch (Exception)
 
162
                        {
 
163
                        }
 
164
                        Out_close();
 
165
                        try
 
166
                        {
 
167
                                if (out_ext != null && !out_ext_dontclose)
 
168
                                {
 
169
                                        out_ext.Close();
 
170
                                }
 
171
                                out_ext = null;
 
172
                        }
 
173
                        catch (Exception)
 
174
                        {
 
175
                        }
 
176
                }
 
177
        }
 
178
}