comparison src/ch/ethz/ssh2/StreamGobbler.java @ 273:91a31873c42a ganymed

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