Mercurial > 510Connectbot
comparison src/com/trilead/ssh2/StreamGobbler.java @ 0:0ce5cc452d02
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Thu, 22 May 2014 10:41:19 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0ce5cc452d02 |
---|---|
1 | |
2 package com.trilead.ssh2; | |
3 | |
4 import java.io.IOException; | |
5 import java.io.InputStream; | |
6 | |
7 /** | |
8 * A <code>StreamGobbler</code> is an InputStream that uses an internal worker | |
9 * thread to constantly consume input from another InputStream. It uses a buffer | |
10 * to store the consumed data. The buffer size is automatically adjusted, if needed. | |
11 * <p> | |
12 * This class is sometimes very convenient - if you wrap a session's STDOUT and STDERR | |
13 * InputStreams with instances of this class, then you don't have to bother about | |
14 * the shared window of STDOUT and STDERR in the low level SSH-2 protocol, | |
15 * since all arriving data will be immediatelly consumed by the worker threads. | |
16 * Also, as a side effect, the streams will be buffered (e.g., single byte | |
17 * read() operations are faster). | |
18 * <p> | |
19 * Other SSH for Java libraries include this functionality by default in | |
20 * their STDOUT and STDERR InputStream implementations, however, please be aware | |
21 * that this approach has also a downside: | |
22 * <p> | |
23 * If you do not call the StreamGobbler's <code>read()</code> method often enough | |
24 * and the peer is constantly sending huge amounts of data, then you will sooner or later | |
25 * encounter a low memory situation due to the aggregated data (well, it also depends on the Java heap size). | |
26 * Joe Average will like this class anyway - a paranoid programmer would never use such an approach. | |
27 * <p> | |
28 * The term "StreamGobbler" was taken from an article called "When Runtime.exec() won't", | |
29 * see http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html. | |
30 * | |
31 * @author Christian Plattner, plattner@trilead.com | |
32 * @version $Id: StreamGobbler.java,v 1.1 2007/10/15 12:49:56 cplattne Exp $ | |
33 */ | |
34 | |
35 public class StreamGobbler extends InputStream { | |
36 class GobblerThread extends Thread { | |
37 public void run() { | |
38 byte[] buff = new byte[8192]; | |
39 | |
40 while (true) { | |
41 try { | |
42 int avail = is.read(buff); | |
43 | |
44 synchronized (synchronizer) { | |
45 if (avail <= 0) { | |
46 isEOF = true; | |
47 synchronizer.notifyAll(); | |
48 break; | |
49 } | |
50 | |
51 int space_available = buffer.length - write_pos; | |
52 | |
53 if (space_available < avail) { | |
54 /* compact/resize buffer */ | |
55 int unread_size = write_pos - read_pos; | |
56 int need_space = unread_size + avail; | |
57 byte[] new_buffer = buffer; | |
58 | |
59 if (need_space > buffer.length) { | |
60 int inc = need_space / 3; | |
61 inc = (inc < 256) ? 256 : inc; | |
62 inc = (inc > 8192) ? 8192 : inc; | |
63 new_buffer = new byte[need_space + inc]; | |
64 } | |
65 | |
66 if (unread_size > 0) | |
67 System.arraycopy(buffer, read_pos, new_buffer, 0, unread_size); | |
68 | |
69 buffer = new_buffer; | |
70 read_pos = 0; | |
71 write_pos = unread_size; | |
72 } | |
73 | |
74 System.arraycopy(buff, 0, buffer, write_pos, avail); | |
75 write_pos += avail; | |
76 synchronizer.notifyAll(); | |
77 } | |
78 } | |
79 catch (IOException e) { | |
80 synchronized (synchronizer) { | |
81 exception = e; | |
82 synchronizer.notifyAll(); | |
83 break; | |
84 } | |
85 } | |
86 } | |
87 } | |
88 } | |
89 | |
90 private InputStream is; | |
91 private GobblerThread t; | |
92 | |
93 private Object synchronizer = new Object(); | |
94 | |
95 private boolean isEOF = false; | |
96 private boolean isClosed = false; | |
97 private IOException exception = null; | |
98 | |
99 private byte[] buffer = new byte[2048]; | |
100 private int read_pos = 0; | |
101 private int write_pos = 0; | |
102 | |
103 public StreamGobbler(InputStream is) { | |
104 this.is = is; | |
105 t = new GobblerThread(); | |
106 t.setDaemon(true); | |
107 t.start(); | |
108 } | |
109 | |
110 public int read() throws IOException { | |
111 synchronized (synchronizer) { | |
112 if (isClosed) | |
113 throw new IOException("This StreamGobbler is closed."); | |
114 | |
115 while (read_pos == write_pos) { | |
116 if (exception != null) | |
117 throw exception; | |
118 | |
119 if (isEOF) | |
120 return -1; | |
121 | |
122 try { | |
123 synchronizer.wait(); | |
124 } | |
125 catch (InterruptedException e) { | |
126 } | |
127 } | |
128 | |
129 int b = buffer[read_pos++] & 0xff; | |
130 return b; | |
131 } | |
132 } | |
133 | |
134 public int available() throws IOException { | |
135 synchronized (synchronizer) { | |
136 if (isClosed) | |
137 throw new IOException("This StreamGobbler is closed."); | |
138 | |
139 return write_pos - read_pos; | |
140 } | |
141 } | |
142 | |
143 public int read(byte[] b) throws IOException { | |
144 return read(b, 0, b.length); | |
145 } | |
146 | |
147 public void close() throws IOException { | |
148 synchronized (synchronizer) { | |
149 if (isClosed) | |
150 return; | |
151 | |
152 isClosed = true; | |
153 isEOF = true; | |
154 synchronizer.notifyAll(); | |
155 is.close(); | |
156 } | |
157 } | |
158 | |
159 public int read(byte[] b, int off, int len) throws IOException { | |
160 if (b == null) | |
161 throw new NullPointerException(); | |
162 | |
163 if ((off < 0) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0) || (off > b.length)) | |
164 throw new IndexOutOfBoundsException(); | |
165 | |
166 if (len == 0) | |
167 return 0; | |
168 | |
169 synchronized (synchronizer) { | |
170 if (isClosed) | |
171 throw new IOException("This StreamGobbler is closed."); | |
172 | |
173 while (read_pos == write_pos) { | |
174 if (exception != null) | |
175 throw exception; | |
176 | |
177 if (isEOF) | |
178 return -1; | |
179 | |
180 try { | |
181 synchronizer.wait(); | |
182 } | |
183 catch (InterruptedException e) { | |
184 } | |
185 } | |
186 | |
187 int avail = write_pos - read_pos; | |
188 avail = (avail > len) ? len : avail; | |
189 System.arraycopy(buffer, read_pos, b, off, avail); | |
190 read_pos += avail; | |
191 return avail; | |
192 } | |
193 } | |
194 } |