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