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