KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcraft > jsch > IO


1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002,2003,2004,2005,2006 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 JavaDoc {
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   public void close(){
93     try{
94       if(in!=null && !in_dontclose) in.close();
95       in=null;
96     }
97     catch(Exception JavaDoc ee){}
98     try{
99       if(out!=null && !out_dontclose) out.close();
100       out=null;
101     }
102     catch(Exception JavaDoc ee){}
103     try{
104       if(out_ext!=null && !out_ext_dontclose) out_ext.close();
105       out_ext=null;
106     }
107     catch(Exception JavaDoc ee){}
108   }
109
110   /*
111   public void finalize() throws Throwable{
112     try{
113       if(in!=null) in.close();
114     }
115     catch(Exception ee){}
116     try{
117       if(out!=null) out.close();
118     }
119     catch(Exception ee){}
120     try{
121       if(out_ext!=null) out_ext.close();
122     }
123     catch(Exception ee){}
124   }
125   */

126 }
127
Popular Tags