1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.melati.util;
47
48 import java.io.Writer;
49 import java.io.FileWriter;
50 import java.io.File;
51 import java.io.FileReader;
52 import java.io.Reader;
53 import java.io.InputStream;
54 import java.io.OutputStream;
55 import java.io.FileInputStream;
56 import java.io.IOException;
57 import java.net.URL;
58
59
60
61
62 public final class IoUtils {
63
64 private IoUtils() {
65
66
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public static byte[] slurp(InputStream i, int estimate, int limit)
82 throws IOException {
83 try {
84 byte[] b = new byte[estimate];
85 int p = 0;
86
87 for (;;) {
88 int g = i.read(b, p, Math.min(b.length, limit) - p);
89 if (g == -1) break;
90 p += g;
91 if (p >= limit) break;
92 if (p >= b.length) {
93 byte[] c = new byte[2 * b.length];
94 System.arraycopy(b, 0, c, 0, p);
95 b = c;
96 }
97 }
98
99 if (p == b.length)
100 return b;
101 else {
102 byte[] c = new byte[p];
103 System.arraycopy(b, 0, c, 0, p);
104 return c;
105 }
106 }
107 finally {
108 try { i.close(); } catch (Exception e) {
109
110 e = null;
111 }
112 }
113 }
114
115
116
117
118
119
120
121
122
123
124
125 public static byte[] slurp(InputStream i, int estimate) throws IOException {
126 return slurp(i, estimate, Integer.MAX_VALUE);
127 }
128
129
130
131
132
133
134
135
136
137
138
139 public static byte[] slurp(URL url, int estimate) throws IOException {
140 return slurp(url.openStream(), estimate);
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154 public static byte[] slurp(URL url, int estimate, int max)
155 throws IOException {
156 return slurp(url.openStream(), estimate, max);
157 }
158
159
160
161
162
163
164
165
166
167
168
169 public static byte[] slurp(File f, int estimate) throws IOException {
170 return slurp(new FileInputStream(f), estimate);
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184 public static char[] slurp(Reader i, int estimate, int limit)
185 throws IOException {
186 try {
187 char[] b = new char[estimate];
188 int p = 0;
189
190 for (;;) {
191 int g = i.read(b, p, Math.min(b.length, limit) - p);
192 if (g == -1) break;
193 p += g;
194 if (p >= limit) break;
195 if (p >= b.length) {
196 char[] c = new char[2 * b.length];
197 System.arraycopy(b, 0, c, 0, p);
198 b = c;
199 }
200 }
201
202 if (p == b.length)
203 return b;
204 else {
205 char[] c = new char[p];
206 System.arraycopy(b, 0, c, 0, p);
207 return c;
208 }
209 }
210 finally {
211 try { i.close(); } catch (Exception e) {
212
213 e = null;
214 }
215 }
216 }
217
218
219
220
221
222
223
224
225
226
227
228 public static char[] slurp(Reader i, int estimate) throws IOException {
229 return slurp(i, estimate, Integer.MAX_VALUE);
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243
244 public static byte[] slurpOutputOf_bytes(String[] command,
245 int estimate, int limit)
246 throws IOException {
247 Process proc = Runtime.getRuntime().exec(command);
248
249 byte[] output = IoUtils.slurp(proc.getInputStream(), estimate, limit);
250
251 byte[] errors = IoUtils.slurp(proc.getErrorStream(), estimate, limit);
252
253 try {
254 if (proc.waitFor() != 0)
255 throw new ProcessFailedException(command[0] + " failed",
256 new String(errors));
257
258 return output;
259 }
260 catch (InterruptedException e) {
261 throw new IOException("interrupted while waiting for " +
262 command[0] + " to complete");
263 }
264 }
265
266
267
268
269
270
271
272
273
274
275
276
277
278 public static String slurpOutputOf(String[] command, int estimate, int limit)
279 throws IOException {
280 return new String(slurpOutputOf_bytes(command, estimate, limit));
281 }
282
283
284
285
286
287
288
289
290
291
292 public static void copy(InputStream i, int buf, OutputStream o)
293 throws IOException {
294 byte b[] = new byte[buf];
295 for (;;) {
296 int g = i.read(b);
297 if (g == -1) break;
298 o.write(b, 0, g);
299 }
300 }
301
302
303
304
305
306
307
308
309
310
311 public static void copy(Reader i, int buf, Writer o)
312 throws IOException {
313 char b[] = new char[buf];
314 for (;;) {
315 int g = i.read(b);
316 if (g == -1) break;
317 o.write(b, 0, g);
318 }
319 }
320
321
322
323
324
325
326
327
328
329
330 public static void copy(File from, int buf, File to) throws IOException {
331 FileReader i = new FileReader(from);
332 try {
333 FileWriter o = new FileWriter(to);
334 try {
335 copy(i, buf, o);
336 }
337 finally {
338 try { o.close(); } catch (Exception e) {
339
340 e = null;
341 }
342 }
343 }
344 finally {
345 try { i.close(); } catch (Exception e) {
346
347 e = null;
348 }
349 }
350 }
351 }
352
353
354
355
356
357
358
359
360
361
362
363