comparison src/ch/ethz/ssh2/StreamGobbler.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents 91a31873c42a
children
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
34 * 34 *
35 * @author Christian Plattner 35 * @author Christian Plattner
36 * @version 2.50, 03/15/10 36 * @version 2.50, 03/15/10
37 */ 37 */
38 38
39 public class StreamGobbler extends InputStream 39 public class StreamGobbler extends InputStream {
40 { 40 class GobblerThread extends Thread {
41 class GobblerThread extends Thread 41 @Override
42 { 42 public void run() {
43 @Override 43 byte[] buff = new byte[8192];
44 public void run() 44
45 { 45 while (true) {
46 byte[] buff = new byte[8192]; 46 try {
47 47 int avail = is.read(buff);
48 while (true) 48
49 { 49 synchronized (synchronizer) {
50 try 50 if (avail <= 0) {
51 { 51 isEOF = true;
52 int avail = is.read(buff); 52 synchronizer.notifyAll();
53 53 break;
54 synchronized (synchronizer) 54 }
55 { 55
56 if (avail <= 0) 56 int space_available = buffer.length - write_pos;
57 { 57
58 isEOF = true; 58 if (space_available < avail) {
59 synchronizer.notifyAll(); 59 /* compact/resize buffer */
60 break; 60 int unread_size = write_pos - read_pos;
61 } 61 int need_space = unread_size + avail;
62 62 byte[] new_buffer = buffer;
63 int space_available = buffer.length - write_pos; 63
64 64 if (need_space > buffer.length) {
65 if (space_available < avail) 65 int inc = need_space / 3;
66 { 66 inc = (inc < 256) ? 256 : inc;
67 /* compact/resize buffer */ 67 inc = (inc > 8192) ? 8192 : inc;
68 68 new_buffer = new byte[need_space + inc];
69 int unread_size = write_pos - read_pos; 69 }
70 int need_space = unread_size + avail; 70
71 71 if (unread_size > 0)
72 byte[] new_buffer = buffer; 72 System.arraycopy(buffer, read_pos, new_buffer, 0, unread_size);
73 73
74 if (need_space > buffer.length) 74 buffer = new_buffer;
75 { 75 read_pos = 0;
76 int inc = need_space / 3; 76 write_pos = unread_size;
77 inc = (inc < 256) ? 256 : inc; 77 }
78 inc = (inc > 8192) ? 8192 : inc; 78
79 new_buffer = new byte[need_space + inc]; 79 System.arraycopy(buff, 0, buffer, write_pos, avail);
80 } 80 write_pos += avail;
81 81 synchronizer.notifyAll();
82 if (unread_size > 0) 82 }
83 System.arraycopy(buffer, read_pos, new_buffer, 0, unread_size); 83 }
84 84 catch (IOException e) {
85 buffer = new_buffer; 85 synchronized (synchronizer) {
86 86 exception = e;
87 read_pos = 0; 87 synchronizer.notifyAll();
88 write_pos = unread_size; 88 break;
89 } 89 }
90 90 }
91 System.arraycopy(buff, 0, buffer, write_pos, avail); 91 }
92 write_pos += avail; 92 }
93 93 }
94 synchronizer.notifyAll(); 94
95 } 95 private InputStream is;
96 } 96
97 catch (IOException e) 97 private final Object synchronizer = new Object();
98 { 98
99 synchronized (synchronizer) 99 private boolean isEOF = false;
100 { 100 private boolean isClosed = false;
101 exception = e; 101 private IOException exception = null;
102 synchronizer.notifyAll(); 102
103 break; 103 private byte[] buffer = new byte[2048];
104 } 104 private int read_pos = 0;
105 } 105 private int write_pos = 0;
106 } 106
107 } 107 public StreamGobbler(InputStream is) {
108 } 108 this.is = is;
109 109 GobblerThread t = new GobblerThread();
110 private InputStream is; 110 t.setDaemon(true);
111 111 t.start();
112 private final Object synchronizer = new Object(); 112 }
113 113
114 private boolean isEOF = false; 114 @Override
115 private boolean isClosed = false; 115 public int read() throws IOException {
116 private IOException exception = null; 116 synchronized (synchronizer) {
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) 117 if (isClosed)
136 throw new IOException("This StreamGobbler is closed."); 118 throw new IOException("This StreamGobbler is closed.");
137 119
138 while (read_pos == write_pos) 120 while (read_pos == write_pos) {
139 {
140 if (exception != null) 121 if (exception != null)
141 throw exception; 122 throw exception;
142 123
143 if (isEOF) 124 if (isEOF)
144 return -1; 125 return -1;
145 126
146 try 127 try {
147 {
148 synchronizer.wait(); 128 synchronizer.wait();
149 } 129 }
150 catch (InterruptedException e) 130 catch (InterruptedException e) {
151 {
152 throw new InterruptedIOException(); 131 throw new InterruptedIOException();
153 } 132 }
154 } 133 }
134
155 return buffer[read_pos++] & 0xff; 135 return buffer[read_pos++] & 0xff;
156 } 136 }
157 } 137 }
158 138
159 @Override 139 @Override
160 public int available() throws IOException 140 public int available() throws IOException {
161 { 141 synchronized (synchronizer) {
162 synchronized (synchronizer) 142 if (isClosed)
163 { 143 throw new IOException("This StreamGobbler is closed.");
164 if (isClosed) 144
165 throw new IOException("This StreamGobbler is closed."); 145 return write_pos - read_pos;
166 146 }
167 return write_pos - read_pos; 147 }
168 } 148
169 } 149 @Override
170 150 public int read(byte[] b) throws IOException {
171 @Override 151 return read(b, 0, b.length);
172 public int read(byte[] b) throws IOException 152 }
173 { 153
174 return read(b, 0, b.length); 154 @Override
175 } 155 public void close() throws IOException {
176 156 synchronized (synchronizer) {
177 @Override 157 if (isClosed)
178 public void close() throws IOException 158 return;
179 { 159
180 synchronized (synchronizer) 160 isClosed = true;
181 { 161 isEOF = true;
182 if (isClosed) 162 synchronizer.notifyAll();
183 return; 163 is.close();
184 isClosed = true; 164 }
185 isEOF = true; 165 }
186 synchronizer.notifyAll(); 166
187 is.close(); 167 @Override
188 } 168 public int read(byte[] b, int off, int len) throws IOException {
189 } 169 if (b == null)
190 170 throw new NullPointerException();
191 @Override 171
192 public int read(byte[] b, int off, int len) throws IOException 172 if ((off < 0) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0) || (off > b.length))
193 { 173 throw new IndexOutOfBoundsException();
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 174
200 if (len == 0) 175 if (len == 0)
201 return 0; 176 return 0;
202 synchronized (synchronizer) 177
203 { 178 synchronized (synchronizer) {
204 if (isClosed) 179 if (isClosed)
205 throw new IOException("This StreamGobbler is closed."); 180 throw new IOException("This StreamGobbler is closed.");
206 181
207 while (read_pos == write_pos) 182 while (read_pos == write_pos) {
208 {
209 if (exception != null) 183 if (exception != null)
210 throw exception; 184 throw exception;
211 185
212 if (isEOF) 186 if (isEOF)
213 return -1; 187 return -1;
214 188
215 try 189 try {
216 {
217 synchronizer.wait(); 190 synchronizer.wait();
218 } 191 }
219 catch (InterruptedException e) 192 catch (InterruptedException e) {
220 {
221 throw new InterruptedIOException(); 193 throw new InterruptedIOException();
222 } 194 }
223 } 195 }
224 196
225 int avail = write_pos - read_pos; 197 int avail = write_pos - read_pos;
226
227 avail = (avail > len) ? len : avail; 198 avail = (avail > len) ? len : avail;
228
229 System.arraycopy(buffer, read_pos, b, off, avail); 199 System.arraycopy(buffer, read_pos, b, off, avail);
230
231 read_pos += avail; 200 read_pos += avail;
232
233 return avail; 201 return avail;
234 } 202 }
235 } 203 }
236 } 204 }