001: /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
002: /*
003: Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are met:
007:
008: 1. Redistributions of source code must retain the above copyright notice,
009: this list of conditions and the following disclaimer.
010:
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in
013: the documentation and/or other materials provided with the distribution.
014:
015: 3. The names of the authors may not be used to endorse or promote products
016: derived from this software without specific prior written permission.
017:
018: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
019: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
020: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
021: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
022: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
024: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
027: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jcraft.jsch;
031:
032: import java.io.*;
033:
034: public class IO {
035: InputStream in;
036: OutputStream out;
037: OutputStream out_ext;
038:
039: private boolean in_dontclose = false;
040: private boolean out_dontclose = false;
041: private boolean out_ext_dontclose = false;
042:
043: void setOutputStream(OutputStream out) {
044: this .out = out;
045: }
046:
047: void setOutputStream(OutputStream out, boolean dontclose) {
048: this .out_dontclose = dontclose;
049: setOutputStream(out);
050: }
051:
052: void setExtOutputStream(OutputStream out) {
053: this .out_ext = out;
054: }
055:
056: void setExtOutputStream(OutputStream out, boolean dontclose) {
057: this .out_ext_dontclose = dontclose;
058: setExtOutputStream(out);
059: }
060:
061: void setInputStream(InputStream in) {
062: this .in = in;
063: }
064:
065: void setInputStream(InputStream in, boolean dontclose) {
066: this .in_dontclose = dontclose;
067: setInputStream(in);
068: }
069:
070: public void put(Packet p) throws IOException,
071: java.net.SocketException {
072: out.write(p.buffer.buffer, 0, p.buffer.index);
073: out.flush();
074: }
075:
076: void put(byte[] array, int begin, int length) throws IOException {
077: out.write(array, begin, length);
078: out.flush();
079: }
080:
081: void put_ext(byte[] array, int begin, int length)
082: throws IOException {
083: out_ext.write(array, begin, length);
084: out_ext.flush();
085: }
086:
087: int getByte() throws IOException {
088: return in.read();
089: }
090:
091: void getByte(byte[] array) throws IOException {
092: getByte(array, 0, array.length);
093: }
094:
095: void getByte(byte[] array, int begin, int length)
096: throws IOException {
097: do {
098: int completed = in.read(array, begin, length);
099: if (completed < 0) {
100: throw new IOException("End of IO Stream Read");
101: }
102: begin += completed;
103: length -= completed;
104: } while (length > 0);
105: }
106:
107: public void close() {
108: try {
109: if (in != null && !in_dontclose)
110: in.close();
111: in = null;
112: } catch (Exception ee) {
113: }
114: try {
115: if (out != null && !out_dontclose)
116: out.close();
117: out = null;
118: } catch (Exception ee) {
119: }
120: try {
121: if (out_ext != null && !out_ext_dontclose)
122: out_ext.close();
123: out_ext = null;
124: } catch (Exception ee) {
125: }
126: }
127:
128: /*
129: public void finalize() throws Throwable{
130: try{
131: if(in!=null) in.close();
132: }
133: catch(Exception ee){}
134: try{
135: if(out!=null) out.close();
136: }
137: catch(Exception ee){}
138: try{
139: if(out_ext!=null) out_ext.close();
140: }
141: catch(Exception ee){}
142: }
143: */
144: }
|