1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.util;
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28 import java.io.PrintWriter;
29 import java.io.Reader;
30 import java.io.StringWriter;
31 import java.io.Writer;
32 import java.nio.charset.Charset;
33
34 import org.eclipse.jetty.util.log.Log;
35 import org.eclipse.jetty.util.log.Logger;
36
37
38
39
40
41
42 public class IO
43 {
44 private static final Logger LOG = Log.getLogger(IO.class);
45
46
47 public final static String
48 CRLF = "\015\012";
49
50
51 public final static byte[]
52 CRLF_BYTES = {(byte)'\015',(byte)'\012'};
53
54
55 public static final int bufferSize = 64*1024;
56
57
58 static class Job implements Runnable
59 {
60 InputStream in;
61 OutputStream out;
62 Reader read;
63 Writer write;
64
65 Job(InputStream in,OutputStream out)
66 {
67 this.in=in;
68 this.out=out;
69 this.read=null;
70 this.write=null;
71 }
72 Job(Reader read,Writer write)
73 {
74 this.in=null;
75 this.out=null;
76 this.read=read;
77 this.write=write;
78 }
79
80
81
82
83
84 public void run()
85 {
86 try {
87 if (in!=null)
88 copy(in,out,-1);
89 else
90 copy(read,write,-1);
91 }
92 catch(IOException e)
93 {
94 LOG.ignore(e);
95 try{
96 if (out!=null)
97 out.close();
98 if (write!=null)
99 write.close();
100 }
101 catch(IOException e2)
102 {
103 LOG.ignore(e2);
104 }
105 }
106 }
107 }
108
109
110
111
112 public static void copy(InputStream in, OutputStream out)
113 throws IOException
114 {
115 copy(in,out,-1);
116 }
117
118
119
120
121 public static void copy(Reader in, Writer out)
122 throws IOException
123 {
124 copy(in,out,-1);
125 }
126
127
128
129
130 public static void copy(InputStream in,
131 OutputStream out,
132 long byteCount)
133 throws IOException
134 {
135 byte buffer[] = new byte[bufferSize];
136 int len=bufferSize;
137
138 if (byteCount>=0)
139 {
140 while (byteCount>0)
141 {
142 int max = byteCount<bufferSize?(int)byteCount:bufferSize;
143 len=in.read(buffer,0,max);
144
145 if (len==-1)
146 break;
147
148 byteCount -= len;
149 out.write(buffer,0,len);
150 }
151 }
152 else
153 {
154 while (true)
155 {
156 len=in.read(buffer,0,bufferSize);
157 if (len<0 )
158 break;
159 out.write(buffer,0,len);
160 }
161 }
162 }
163
164
165
166
167 public static void copy(Reader in,
168 Writer out,
169 long byteCount)
170 throws IOException
171 {
172 char buffer[] = new char[bufferSize];
173 int len=bufferSize;
174
175 if (byteCount>=0)
176 {
177 while (byteCount>0)
178 {
179 if (byteCount<bufferSize)
180 len=in.read(buffer,0,(int)byteCount);
181 else
182 len=in.read(buffer,0,bufferSize);
183
184 if (len==-1)
185 break;
186
187 byteCount -= len;
188 out.write(buffer,0,len);
189 }
190 }
191 else if (out instanceof PrintWriter)
192 {
193 PrintWriter pout=(PrintWriter)out;
194 while (!pout.checkError())
195 {
196 len=in.read(buffer,0,bufferSize);
197 if (len==-1)
198 break;
199 out.write(buffer,0,len);
200 }
201 }
202 else
203 {
204 while (true)
205 {
206 len=in.read(buffer,0,bufferSize);
207 if (len==-1)
208 break;
209 out.write(buffer,0,len);
210 }
211 }
212 }
213
214
215
216
217
218
219
220 public static void copy(File from,File to) throws IOException
221 {
222 if (from.isDirectory())
223 copyDir(from,to);
224 else
225 copyFile(from,to);
226 }
227
228
229 public static void copyDir(File from,File to) throws IOException
230 {
231 if (to.exists())
232 {
233 if (!to.isDirectory())
234 throw new IllegalArgumentException(to.toString());
235 }
236 else
237 to.mkdirs();
238
239 File[] files = from.listFiles();
240 if (files!=null)
241 {
242 for (int i=0;i<files.length;i++)
243 {
244 String name = files[i].getName();
245 if (".".equals(name) || "..".equals(name))
246 continue;
247 copy(files[i],new File(to,name));
248 }
249 }
250 }
251
252
253 public static void copyFile(File from,File to) throws IOException
254 {
255 try (InputStream in=new FileInputStream(from);
256 OutputStream out=new FileOutputStream(to))
257 {
258 copy(in,out);
259 }
260 }
261
262
263
264
265 public static String toString(InputStream in)
266 throws IOException
267 {
268 return toString(in,(Charset)null);
269 }
270
271
272
273
274 public static String toString(InputStream in,String encoding)
275 throws IOException
276 {
277 return toString(in, encoding==null?null:Charset.forName(encoding));
278 }
279
280
281
282 public static String toString(InputStream in, Charset encoding)
283 throws IOException
284 {
285 StringWriter writer=new StringWriter();
286 InputStreamReader reader = encoding==null?new InputStreamReader(in):new InputStreamReader(in,encoding);
287
288 copy(reader,writer);
289 return writer.toString();
290 }
291
292
293
294
295 public static String toString(Reader in)
296 throws IOException
297 {
298 StringWriter writer=new StringWriter();
299 copy(in,writer);
300 return writer.toString();
301 }
302
303
304
305
306
307
308
309 public static boolean delete(File file)
310 {
311 if (!file.exists())
312 return false;
313 if (file.isDirectory())
314 {
315 File[] files = file.listFiles();
316 for (int i=0;files!=null && i<files.length;i++)
317 delete(files[i]);
318 }
319 return file.delete();
320 }
321
322
323
324
325
326
327
328 public static void close(InputStream is)
329 {
330 try
331 {
332 if (is != null)
333 is.close();
334 }
335 catch (IOException e)
336 {
337 LOG.ignore(e);
338 }
339 }
340
341
342
343
344
345
346 public static void close(Reader reader)
347 {
348 try
349 {
350 if (reader != null)
351 reader.close();
352 } catch (IOException e)
353 {
354 LOG.ignore(e);
355 }
356 }
357
358
359
360
361
362
363 public static void close(Writer writer)
364 {
365 try
366 {
367 if (writer != null)
368 writer.close();
369 } catch (IOException e)
370 {
371 LOG.ignore(e);
372 }
373 }
374
375
376 public static byte[] readBytes(InputStream in)
377 throws IOException
378 {
379 ByteArrayOutputStream bout = new ByteArrayOutputStream();
380 copy(in,bout);
381 return bout.toByteArray();
382 }
383
384
385
386
387
388
389
390 public static void close(OutputStream os)
391 {
392 try
393 {
394 if (os != null)
395 os.close();
396 }
397 catch (IOException e)
398 {
399 LOG.ignore(e);
400 }
401 }
402
403
404
405
406
407 public static OutputStream getNullStream()
408 {
409 return __nullStream;
410 }
411
412
413
414
415
416 public static InputStream getClosedStream()
417 {
418 return __closedStream;
419 }
420
421
422
423 private static class NullOS extends OutputStream
424 {
425 @Override
426 public void close(){}
427 @Override
428 public void flush(){}
429 @Override
430 public void write(byte[]b){}
431 @Override
432 public void write(byte[]b,int i,int l){}
433 @Override
434 public void write(int b){}
435 }
436 private static NullOS __nullStream = new NullOS();
437
438
439
440
441 private static class ClosedIS extends InputStream
442 {
443 @Override
444 public int read() throws IOException
445 {
446 return -1;
447 }
448 }
449 private static ClosedIS __closedStream = new ClosedIS();
450
451
452
453
454
455 public static Writer getNullWriter()
456 {
457 return __nullWriter;
458 }
459
460
461
462
463
464 public static PrintWriter getNullPrintWriter()
465 {
466 return __nullPrintWriter;
467 }
468
469
470
471 private static class NullWrite extends Writer
472 {
473 @Override
474 public void close(){}
475 @Override
476 public void flush(){}
477 @Override
478 public void write(char[]b){}
479 @Override
480 public void write(char[]b,int o,int l){}
481 @Override
482 public void write(int b){}
483 @Override
484 public void write(String s){}
485 @Override
486 public void write(String s,int o,int l){}
487 }
488 private static NullWrite __nullWriter = new NullWrite();
489 private static PrintWriter __nullPrintWriter = new PrintWriter(__nullWriter);
490 }
491
492
493
494
495
496
497
498
499