IO.java

Go to the documentation of this file.
00001 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
00002 /*
00003 Copyright (c) 2002-2011 ymnk, JCraft,Inc. All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008   1. Redistributions of source code must retain the above copyright notice,
00009      this list of conditions and the following disclaimer.
00010 
00011   2. Redistributions in binary form must reproduce the above copyright 
00012      notice, this list of conditions and the following disclaimer in 
00013      the documentation and/or other materials provided with the distribution.
00014 
00015   3. The names of the authors may not be used to endorse or promote products
00016      derived from this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00019 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
00021 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
00022 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 package com.jcraft.jsch;
00031 
00032 import java.io.*;
00033 
00043 public class IO{
00044   InputStream in;
00045   OutputStream out;
00046   OutputStream out_ext;
00047 
00048   private boolean in_dontclose=false;
00049   private boolean out_dontclose=false;
00050   private boolean out_ext_dontclose=false;
00051 
00052   void setOutputStream(OutputStream out){ this.out=out; }
00053   void setOutputStream(OutputStream out, boolean dontclose){
00054     this.out_dontclose=dontclose;
00055     setOutputStream(out);
00056   }
00057   void setExtOutputStream(OutputStream out){ this.out_ext=out; }
00058   void setExtOutputStream(OutputStream out, boolean dontclose){
00059     this.out_ext_dontclose=dontclose;
00060     setExtOutputStream(out);
00061   }
00062   void setInputStream(InputStream in){ this.in=in; }
00063   void setInputStream(InputStream in, boolean dontclose){
00064     this.in_dontclose=dontclose;
00065     setInputStream(in);
00066   }
00067 
00068   public void put(Packet p) throws IOException, java.net.SocketException {
00069     out.write(p.buffer.buffer, 0, p.buffer.index);
00070     out.flush();
00071   }
00072   void put(byte[] array, int begin, int length) throws IOException {
00073     out.write(array, begin, length);
00074     out.flush();
00075   }
00076   void put_ext(byte[] array, int begin, int length) throws IOException {
00077     out_ext.write(array, begin, length);
00078     out_ext.flush();
00079   }
00080 
00081   int getByte() throws IOException {
00082     return in.read();
00083   }
00084 
00085   void getByte(byte[] array) throws IOException {
00086     getByte(array, 0, array.length);
00087   }
00088 
00089   void getByte(byte[] array, int begin, int length) throws IOException {
00090     do{
00091       int completed = in.read(array, begin, length);
00092       if(completed<0){
00093     throw new IOException("End of IO Stream Read");
00094       }
00095       begin+=completed;
00096       length-=completed;
00097     }
00098     while (length>0);
00099   }
00100 
00101   void out_close(){
00102     try{
00103       if(out!=null && !out_dontclose) out.close();
00104       out=null;
00105     }
00106     catch(Exception ee){}
00107   }
00108 
00109   public void close(){
00110     try{
00111       if(in!=null && !in_dontclose) in.close();
00112       in=null;
00113     }
00114     catch(Exception ee){}
00115 
00116     out_close();
00117 
00118     try{
00119       if(out_ext!=null && !out_ext_dontclose) out_ext.close();
00120       out_ext=null;
00121     }
00122     catch(Exception ee){}
00123   }
00124 
00125   /*
00126   public void finalize() throws Throwable{
00127     try{
00128       if(in!=null) in.close();
00129     }
00130     catch(Exception ee){}
00131     try{
00132       if(out!=null) out.close();
00133     }
00134     catch(Exception ee){}
00135     try{
00136       if(out_ext!=null) out_ext.close();
00137     }
00138     catch(Exception ee){}
00139   }
00140   */
00141 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1