01: package ch.ethz.ssh2.channel;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05:
06: /**
07: * ChannelInputStream.
08: *
09: * @author Christian Plattner, plattner@inf.ethz.ch
10: * @version $Id: ChannelInputStream.java,v 1.5 2005/12/05 17:13:26 cplattne Exp $
11: */
12: public final class ChannelInputStream extends InputStream {
13: Channel c;
14:
15: boolean isClosed = false;
16: boolean isEOF = false;
17: boolean extendedFlag = false;
18:
19: ChannelInputStream(Channel c, boolean isExtended) {
20: this .c = c;
21: this .extendedFlag = isExtended;
22: }
23:
24: public int available() throws IOException {
25: if (isEOF)
26: return 0;
27:
28: int avail = c.cm.getAvailable(c, extendedFlag);
29:
30: /* We must not return -1 on EOF */
31:
32: return (avail > 0) ? avail : 0;
33: }
34:
35: public void close() throws IOException {
36: isClosed = true;
37: }
38:
39: public int read(byte[] b, int off, int len) throws IOException {
40: if (b == null)
41: throw new NullPointerException();
42:
43: if ((off < 0) || (len < 0) || ((off + len) > b.length)
44: || ((off + len) < 0) || (off > b.length))
45: throw new IndexOutOfBoundsException();
46:
47: if (len == 0)
48: return 0;
49:
50: if (isEOF)
51: return -1;
52:
53: int ret = c.cm.getChannelData(c, extendedFlag, b, off, len);
54:
55: if (ret == -1) {
56: isEOF = true;
57: }
58:
59: return ret;
60: }
61:
62: public int read(byte[] b) throws IOException {
63: return read(b, 0, b.length);
64: }
65:
66: public int read() throws IOException {
67: /* Yes, this stream is pure and unbuffered, a single byte read() is slow */
68:
69: final byte b[] = new byte[1];
70:
71: int ret = read(b, 0, 1);
72:
73: if (ret != 1)
74: return -1;
75:
76: return b[0] & 0xff;
77: }
78: }
|