comparison app/src/main/java/ch/ethz/ssh2/channel/ChannelInputStream.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/ch/ethz/ssh2/channel/ChannelInputStream.java@071eccdff8ea
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
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.
12 *
13 * @author Christian Plattner
14 * @version 2.50, 03/15/10
15 */
16 public final class ChannelInputStream extends InputStream {
17 Channel c;
18
19 boolean isClosed = false;
20 boolean isEOF = false;
21 boolean extendedFlag = false;
22
23 ChannelInputStream(Channel c, boolean isExtended) {
24 this.c = c;
25 this.extendedFlag = isExtended;
26 }
27
28 @Override
29 public int available() throws IOException {
30 if (isEOF)
31 return 0;
32
33 int avail = c.cm.getAvailable(c, extendedFlag);
34 /* We must not return -1 on EOF */
35 return (avail > 0) ? avail : 0;
36 }
37
38 @Override
39 public void close() throws IOException {
40 isClosed = true;
41 }
42
43 @Override
44 public int read(byte[] b, int off, int len) throws IOException {
45 if (b == null)
46 throw new NullPointerException();
47
48 if ((off < 0) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0) || (off > b.length))
49 throw new IndexOutOfBoundsException();
50
51 if (len == 0)
52 return 0;
53
54 if (isEOF)
55 return -1;
56
57 int ret = c.cm.getChannelData(c, extendedFlag, b, off, len);
58
59 if (ret == -1) {
60 isEOF = true;
61 }
62
63 return ret;
64 }
65
66 @Override
67 public int read(byte[] b) throws IOException {
68 return read(b, 0, b.length);
69 }
70
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);
76
77 if (ret != 1)
78 return -1;
79
80 return b[0] & 0xff;
81 }
82 }