1 2 package ch.ethz.ssh2.channel; 3 4 import java.io.IOException ; 5 import java.io.InputStream ; 6 7 13 public final class ChannelInputStream extends InputStream 14 { 15 Channel c; 16 17 boolean isClosed = false; 18 boolean isEOF = false; 19 boolean extendedFlag = false; 20 21 ChannelInputStream(Channel c, boolean isExtended) 22 { 23 this.c = c; 24 this.extendedFlag = isExtended; 25 } 26 27 public int available() throws IOException 28 { 29 if (isEOF) 30 return 0; 31 32 int avail = c.cm.getAvailable(c, extendedFlag); 33 34 35 36 return (avail > 0) ? avail : 0; 37 } 38 39 public void close() throws IOException 40 { 41 isClosed = true; 42 } 43 44 public int read(byte[] b, int off, int len) throws IOException 45 { 46 if (b == null) 47 throw new NullPointerException (); 48 49 if ((off < 0) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0) || (off > b.length)) 50 throw new IndexOutOfBoundsException (); 51 52 if (len == 0) 53 return 0; 54 55 if (isEOF) 56 return -1; 57 58 int ret = c.cm.getChannelData(c, extendedFlag, b, off, len); 59 60 if (ret == -1) 61 { 62 isEOF = true; 63 } 64 65 return ret; 66 } 67 68 public int read(byte[] b) throws IOException 69 { 70 return read(b, 0, b.length); 71 } 72 73 public int read() throws IOException 74 { 75 76 77 final byte b[] = new byte[1]; 78 79 int ret = read(b, 0, 1); 80 81 if (ret != 1) 82 return -1; 83 84 return b[0] & 0xff; 85 } 86 } 87 | Popular Tags |