]>
Commit | Line | Data |
---|---|---|
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ | |
2 | /* | |
3 | Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are met: | |
7 | ||
8 | 1. Redistributions of source code must retain the above copyright notice, | |
9 | this list of conditions and the following disclaimer. | |
10 | ||
11 | 2. Redistributions in binary form must reproduce the above copyright | |
12 | notice, this list of conditions and the following disclaimer in | |
13 | the documentation and/or other materials provided with the distribution. | |
14 | ||
15 | 3. The names of the authors may not be used to endorse or promote products | |
16 | derived from this software without specific prior written permission. | |
17 | ||
18 | THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, | |
19 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, | |
21 | INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, | |
22 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
24 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
27 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | package com.jcraft.jsch; | |
31 | ||
32 | import java.io.*; | |
33 | ||
34 | public class IO{ | |
35 | InputStream in; | |
36 | OutputStream out; | |
37 | OutputStream out_ext; | |
38 | ||
39 | private boolean in_dontclose=false; | |
40 | private boolean out_dontclose=false; | |
41 | private boolean out_ext_dontclose=false; | |
42 | ||
43 | void setOutputStream(OutputStream out){ this.out=out; } | |
44 | void setOutputStream(OutputStream out, boolean dontclose){ | |
45 | this.out_dontclose=dontclose; | |
46 | setOutputStream(out); | |
47 | } | |
48 | void setExtOutputStream(OutputStream out){ this.out_ext=out; } | |
49 | void setExtOutputStream(OutputStream out, boolean dontclose){ | |
50 | this.out_ext_dontclose=dontclose; | |
51 | setExtOutputStream(out); | |
52 | } | |
53 | void setInputStream(InputStream in){ this.in=in; } | |
54 | void setInputStream(InputStream in, boolean dontclose){ | |
55 | this.in_dontclose=dontclose; | |
56 | setInputStream(in); | |
57 | } | |
58 | ||
59 | public void put(Packet p) throws IOException, java.net.SocketException { | |
60 | out.write(p.buffer.buffer, 0, p.buffer.index); | |
61 | out.flush(); | |
62 | } | |
63 | void put(byte[] array, int begin, int length) throws IOException { | |
64 | out.write(array, begin, length); | |
65 | out.flush(); | |
66 | } | |
67 | void put_ext(byte[] array, int begin, int length) throws IOException { | |
68 | out_ext.write(array, begin, length); | |
69 | out_ext.flush(); | |
70 | } | |
71 | ||
72 | int getByte() throws IOException { | |
73 | return in.read(); | |
74 | } | |
75 | ||
76 | void getByte(byte[] array) throws IOException { | |
77 | getByte(array, 0, array.length); | |
78 | } | |
79 | ||
80 | void getByte(byte[] array, int begin, int length) throws IOException { | |
81 | do{ | |
82 | int completed = in.read(array, begin, length); | |
83 | if(completed<0){ | |
84 | throw new IOException("End of IO Stream Read"); | |
85 | } | |
86 | begin+=completed; | |
87 | length-=completed; | |
88 | } | |
89 | while (length>0); | |
90 | } | |
91 | ||
92 | void out_close(){ | |
93 | try{ | |
94 | if(out!=null && !out_dontclose) out.close(); | |
95 | out=null; | |
96 | } | |
97 | catch(Exception ee){} | |
98 | } | |
99 | ||
100 | public void close(){ | |
101 | try{ | |
102 | if(in!=null && !in_dontclose) in.close(); | |
103 | in=null; | |
104 | } | |
105 | catch(Exception ee){} | |
106 | ||
107 | out_close(); | |
108 | ||
109 | try{ | |
110 | if(out_ext!=null && !out_ext_dontclose) out_ext.close(); | |
111 | out_ext=null; | |
112 | } | |
113 | catch(Exception ee){} | |
114 | } | |
115 | ||
116 | /* | |
117 | public void finalize() throws Throwable{ | |
118 | try{ | |
119 | if(in!=null) in.close(); | |
120 | } | |
121 | catch(Exception ee){} | |
122 | try{ | |
123 | if(out!=null) out.close(); | |
124 | } | |
125 | catch(Exception ee){} | |
126 | try{ | |
127 | if(out_ext!=null) out_ext.close(); | |
128 | } | |
129 | catch(Exception ee){} | |
130 | } | |
131 | */ | |
132 | } |